4

非標準のボー レート 625000 でのみ動作するハードウェア デバイスを使用しています。

USB ポート経由でこのデバイスに接続し、データを読み書きする必要があります。したがって、これを可能にする小さなCプログラムを開発しようとしています。ただし、このコードは Linux 環境 ( Debian / Raspian ) で動作する必要があり、残念ながら私の Linux スキルは初歩的なものにすぎません。

したがって、Linux で非標準のボーレート 625000 を設定し、ハードウェア デバイス (ttyUSB0) に接続し、ビット ストリームをデバイス (0x02 0x01) に送信し、デバイスから返された 7 バイトを読み取ります。

次のスタック オーバーフローの質問を見てきました。

その他...

しかし、私の Linux 知識の穴は大きすぎて、必要なつながりを作ることができません。どうすればいいですか?

4

1 に答える 1

8

それで、もう少し検索した後、次のコードに出くわしました:

https://jim.sh/ftx/files/linux-custom-baudrate.c

以下は、上記のコードのコピーで、私の目的のためにかなり簡略化されていますが、今では実装が非常に簡単になっているはずです。

#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<errno.h>
#include<unistd.h>
#include<fcntl.h>
#include<termio.h>
#include <linux/serial.h>

static int rate_to_constant(int baudrate) {
#define B(x) case x: return B##x
        switch(baudrate) {
        B(50);     B(75);     B(110);    B(134);    B(150);
        B(200);    B(300);    B(600);    B(1200);   B(1800);
        B(2400);   B(4800);   B(9600);   B(19200);  B(38400);
        B(57600);  B(115200); B(230400); B(460800); B(500000);
        B(576000); B(921600); B(1000000);B(1152000);B(1500000);
    default: return 0;
    }
#undef B
}

int main() {

    struct termios options;
    struct serial_struct serinfo;
    int fd;
    int speed = 0;
    int rate = 625000;

    /* Open and configure serial port */
    if ((fd = open("/dev/ttyUSB0",O_RDWR|O_NOCTTY)) == -1)
    {
        return -1;
    }

    // if you've entered a standard baud the function below will return it
    speed = rate_to_constant(rate);

    if (speed == 0) {
        /* Custom divisor */
        serinfo.reserved_char[0] = 0;
        if (ioctl(fd, TIOCGSERIAL, &serinfo) < 0)
            return -1;
        serinfo.flags &= ~ASYNC_SPD_MASK;
        serinfo.flags |= ASYNC_SPD_CUST;
        serinfo.custom_divisor = (serinfo.baud_base + (rate / 2)) / rate;
        if (serinfo.custom_divisor < 1)
            serinfo.custom_divisor = 1;
        if (ioctl(fd, TIOCSSERIAL, &serinfo) < 0)
            return -1;
        if (ioctl(fd, TIOCGSERIAL, &serinfo) < 0)
            return -1;
        if (serinfo.custom_divisor * rate != serinfo.baud_base) {
            warnx("actual baudrate is %d / %d = %f",
                  serinfo.baud_base, serinfo.custom_divisor,
                  (float)serinfo.baud_base / serinfo.custom_divisor);
        }
    }

    fcntl(fd, F_SETFL, 0);
    tcgetattr(fd, &options);
    cfsetispeed(&options, speed ?: B38400);
    cfsetospeed(&options, speed ?: B38400);
    cfmakeraw(&options);
    options.c_cflag |= (CLOCAL | CREAD);
    options.c_cflag &= ~CRTSCTS;
    if (tcsetattr(fd, TCSANOW, &options) != 0)
    {
        //return -1;
    }


    //return fd;

    char ping_cmd[] = {2,1};
    char ping_rec[7];

    write(fd,&ping_cmd,sizeof(ping_cmd));
    read(fd,&ping_rec,sizeof(ping_rec));

    int i;
    for (i = 0; i < sizeof(ping_rec); i++)
    {
        printf("%d ",ping_rec[i]);
    }


    close(fd);
    return 0;
}

頭の切れるコーダーなら気が付くと思いますが、私はこのコードをメインに取り込んだので、これらすべての「return -1」の存在はほぼ間違いなく悪いプログラミング手法ですが、どのようにクリーンアップすればよいかわかりません。したがって、私はあなたの提案を聞きたいです-提案されたとおりに編集します.

それまでの間、私と同様の問題に直面した場合は、上記の方法でうまくいくはずです。

于 2014-05-07T10:55:20.833 に答える