2

天!我想通过串行向 C++ 发送 midi 字节。我已经在接收数据了。唯一的问题是我只收到 7 位,如果我尝试获取 2 个字节,那么这些位没有意义。我收到的范围在 0 到 127 之间,我应该能够看到 0 到 255 之间的数字。

我将数据存储在一个字符(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 之间的数字。小代码:

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,还有一些我不记得了。

4

0 回答 0