2

Arduinoボードを持っていて、カスタムボーレートでUSBを使用して吐き出されたデータを読み取りたいと思っています。Arduinoが提案するコードのいくつかをハックして、私はこのCコードを取得します:

int serialport_init(const char* serialport, int baud)
{
    struct termios toptions;
    int fd;

    printf("init_serialport: opening port %s @ %d bps\n", serialport,baud);

    fd = open(serialport, O_RDWR | O_NOCTTY | O_NDELAY);
    serialPortPointer = fd;

    if (fd == -1)
    {
        printf("Unable to open port when initialising hardware'n");
        return -1;
    }

    if (tcgetattr(fd, &toptions) < 0)
    {
        printf("Couldn't get term attributes when initialising hardware\n");
        return -1;
    }
    speed_t brate = baud; // let you override switch below if needed
    switch(baud) {
        case 4800:   brate=B4800;   break;
        case 9600:   brate=B9600;   break;
        case 14400:  brate=B14400;  break;
        case 19200:  brate=B19200;  break;
        case 28800:  brate=B28800;  break;
        case 38400:  brate=B38400;  break;
        case 57600:  brate=B57600;  break;
        case 115200: brate=B115200; break;
    }
    cfsetispeed(&toptions, EXTA);
    cfsetospeed(&toptions, EXTA);

    // 8N1
    toptions.c_cflag &= ~PARENB;
    toptions.c_cflag &= ~CSTOPB;
    toptions.c_cflag &= ~CSIZE;
    toptions.c_cflag |= CS8;
    // no flow control
    toptions.c_cflag &= ~CRTSCTS;

    toptions.c_cflag |= CREAD | CLOCAL;  // turn on READ & ignore ctrl lines
    toptions.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl

    toptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // make raw
    toptions.c_oflag &= ~OPOST; // make raw

    // see: http://unixwiz.net/techtips/termios-vmin-vtime.html
    toptions.c_cc[VMIN]  = 0;
    toptions.c_cc[VTIME] = 20;

    if(tcsetattr(fd, TCSANOW, &toptions) < 0)
    {
        printf("Couldn't set term attributes when initialising hardware\n");
        return -1;
    }

    return fd;
}

問題は、termios.hファイルが31250(MIDI)ボーレートをサポートしていないことです...ボーレートとして31250を入力しようとすると、この関数は-1を返し、「ハードウェアの初期化時に用語属性を設定できませんでした」と表示されます(失敗します)最後に)。

では、Cやその他の言語で、必要なボーレートでデータを読み取るプログラムを作成するにはどうすればよいでしょうか。termios.hはカスタムボーレートをサポートしていますか?

私は文字通りシリアルポートのデータを読みたいだけです-他には何もありません。

4

2 に答える 2

1

これは、Arduino シリアル ポートで MIDI I/O 通信を可能にするライブラリです。少なくとも 2 つのシリアル ポートが可能な Arduino が必要です (このようなもの)。1 つのシリアルは MIDI デバイス (31250bps) との通信に使用され、もう 1 つのシリアルは PC (たとえば 115200bps) との通信に使用されます。Arduino ボードにシリアル ポートが 1 つしかない場合は、このようなソフトウェア シリアル ライブラリを試すこともできます。

于 2011-09-20T09:59:12.877 に答える
0

API には、termios.hユーザー定義のボー レートを表現する方法がまったくありませんが、オペレーティング システムによっては、そうするための拡張機能がある場合があります。代わりに、より標準的な 38400 を使用するように Arduino をセットアップしてみてください。

于 2011-09-20T00:22:31.927 に答える