以 31250 波特率从 USB 读取数据

发布于 2024-12-05 23:36:17 字数 2226 浏览 2 评论 0原文

我有一块 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 支持自定义波特率吗?

我实际上只是想读取串行端口上的数据 - 没有别的。

I've got an Arduino board and want to read the data it's spitting off using USB at a custom baud rate. Hacking up some of the code Arduino suggest, I get this C code:

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;
}

The problem is that the termios.h file doesn't support 31250 (MIDI) baud rate... If I try entering 31250 as the baud rate, this function returns -1 and says "Couldn't set term attributes when initialising hardware" (it fails right at the end).

So - how can I write a program, in C, or any other language, that reads the data off at the baud rate I want? Does termios.h support custom baud rates?

I literally just want to read the data on the serial port - nothing else.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

失眠症患者 2024-12-12 23:36:17

这是一个库,可在Arduino 串行端口。您需要一个至少具有 2 个串行端口的 Arduino(例如这个 )。一个串口将用于与 MIDI 设备(31250bps)通信,另一个串口用于与 PC(例如 115200bps)通信。如果您的 Arduino 板上只有一个串行端口,那么您还可以尝试使用软件串行库,例如

This is a library that enables MIDI I/O communications on the Arduino serial ports. You need an Arduino capable of minimum 2 serial ports (like this one). One serial will be used for communication to MIDI devices (31250bps), and the other one to PC (for example 115200bps). If you have only one serial port on your Arduino board then you can also experiment with a software serial library like this.

不弃不离 2024-12-12 23:36:17

termios.h API 根本无法表达用户定义的波特率,但根据您的操作系统,可能会有扩展来实现这一点。您可以尝试将 Arduino 设置为使用更标准的 38400 吗?

The termios.h API simply does not have a way to express user-defined baud rates, though depending on your operating system there may be an extension to do so. Can you try setting up the Arduino to use the more standard 38400 instead?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文