G'day! 我想通過串行發送midi字節到C++。我已經收到數據。唯一的問題是,我只收到7位,如果我嘗試獲得2個字節,這些位沒有意義。我收到的範圍在0到127之間,我應該能夠看到0到255之間的數字。只接收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,還有一些我不記得了。
你正確設置:奇偶校驗,數據位,停止位,握手?對於這些teensy可能有不同的默認值。 – Serdalis 2013-03-20 02:42:45
如果消息沒有奇偶校驗,那麼奇偶校驗會佔用該字節的一位,但是你會這麼說。 – Serdalis 2013-03-20 13:49:50