2013-03-20 33 views
2

G'day! 我想通過串行發送midi字節到C++。我已經收到數據。唯一的問題是,我只收到7位,如果我嘗試獲得2個字節,這些位沒有意義。我收到的範圍在0到127之間,我應該能夠看到0到25​​5之間的數字。只接收7位讀取串行端口的MIDI字節C++

我將數據存儲在char(1字節)中。我已經嘗試過int和wchar_t,但仍然無法正常工作。

#include <stdio.h> 
#include <string.h> 
#include <iostream> 
#include <fcntl.h> 
#include <errno.h> 
#include <termios.h> 
#include <unistd.h> 

unsigned char data_in[0]; 
int data_in_int; 

int main(void){ 

int fd_serialport; 
struct termios options; 

fd_serialport = open("/dev/ttyACM0", O_RDONLY | O_NOCTTY); 
//fd_serialport = open("/dev/ttyACM0", O_RDONLY | O_NOCTTY | O_NDELAY); 


if(fd_serialport == -1){ 
    perror("Unable to open /dev/ttyACM0"); 
} 

tcgetattr(fd_serialport, &options); 
cfsetispeed(&options, B38400); 
cfsetospeed(&options, B38400); 
options.c_cflag |= (CLOCAL | CREAD);  
//options.c_cflag |= PARENB; 
//options.c_cflag |= PARODD; 
//options.c_cflag &= ~CSTOPB; 
options.c_cflag &= ~CSIZE; 
options.c_cflag |= CS8; 
//options.c_iflag |= (INPCK | ISTRIP); 
tcsetattr(fd_serialport, TCSANOW, &options); 

fcntl(fd_serialport, F_SETFL, 0); 
usleep(1000000); 

while(read(fd_serialport, &data_in[0], 1)){ 

    data_in_int=(int)data_in[0]; 
    printf("%i\n", data_in_int); 

    } 
    usleep(2000); 
} 
return(0); 

我使用teensy(類似於arduino)發送MIDI數據。目前我只是在測試,看看我能否得到這個數字。我知道teensy的作品,因爲其他程序正確解釋數據(純數據)。我正在使用Ubuntu 12.04 LTS。 我試着-255和255之間發送號碼teensy代碼:

int indx=-256; 

void setup(){ 
    Serial.begin(38400); 
} 

void loop(){ 

    Serial.print(indx,BYTE); 
    delay(200); 
    indx++; 
    if (indx==256) indx=-256; 
} 

你知道爲什麼我不能得到整個字節?我已經嘗試了termios.options和fcntl的不同配置。你認爲嘗試其他圖書館更好嗎?

謝謝!

pd:我也意識到0到127之間的一些數字不能被程序解釋。這個數字是3,17,19,還有一些我不記得了。

+2

你正確設置:奇偶校驗,數據位,停止位,握手?對於這些teensy可能有不同的默認值。 – Serdalis 2013-03-20 02:42:45

+0

如果消息沒有奇偶校驗,那麼奇偶校驗會佔用該字節的一位,但是你會這麼說。 – Serdalis 2013-03-20 13:49:50

回答

1

我以前用所有的行來設置termios.options。我稍後將它們寫作評論。因爲我沒有更改termios的狀態,所以評論並沒有改變端口的設置。看起來像是其中一行,或者其中一些使用了1位的字節。重新啓動計算機後,它的工作,我可以得到0到25​​5之間的數字!

//options.c_cflag |= PARENB; 
//options.c_cflag |= PARODD; 
//options.c_cflag &= ~CSTOPB; 

//options.c_iflag |= (INPCK | ISTRIP); 

我仍然有一個問題,3,17和19.我不能讀取這些數字和程序似乎並沒有收到任何東西。有誰知道爲什麼會發生這種情況?

我會在二進制發佈的數字,也許有人有一個想法:

00000011 -- 3 
00010001 -- 17 
00010011 -- 19 
相關問題