2

こんにちは!シリアル経由で midi バイトを C++ に送信したい。すでにデータを受信して​​います。唯一の問題は、7 ビットしか受信できず、2 バイトを取得しようとするとビットが意味をなさないことです。受信している範囲は 0 から 127 の間で、0 から 255 の間の数値を表示できるはずです。

データを 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);

midiデータを送信するために、(arduinoに似た)十代を使用しています。現時点では、番号を取得できるかどうかをテストしています。他のプログラムがデータを正しく解釈するため(純粋なデータ)、ティーンエイジャーが機能することを私は知っています。私は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