繁体   English   中英

仅接收7位读取串行端口Midi字节C ++

[英]Receiving only 7 bits reading serial port midi bytes c++

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

我将数据存储在一个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之间的数字。

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

我之前在所有行中都设置了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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM