簡體   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