2013-03-20 77 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