【问题标题】:Read data off USB at 31250 baud rate以 31250 波特率从 USB 读取数据
【发布时间】:2011-09-19 23:45:18
【问题描述】:

我有一块 Arduino 板,想以自定义 baud 速率读取它使用 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 是否支持自定义波特率?

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

【问题讨论】:

  • 波特率无所谓,其实不是串口。
  • @Hans,它通常是一个 FTDI USB 连接的 UART,所以即使 RX/TX 线从未离开 Arduino 板,波特率也确实很重要。
  • ...除非它是比我的新板,在微控制器中支持原生 USB。
  • adurino 是直接连接到 USB 还是使用USB-&gt;UART 芯片,例如 FTDI 的 thouse?

标签: c usb arduino baud-rate


【解决方案1】:

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

【讨论】:

  • 啊好东西——我现在不需要 MIDI,但在不久的将来它肯定会变得有用——谢谢。我在板上只有 1 个串行端口(使用迷你 USB 连接) - 我使用的是 Seeeduino 板:skpang.co.uk/catalog/seeeduino-v221-328-board-p-389.html。当我稍后正确查看此内容时,我会告诉您软件串行库是否允许我以 MIDI 波特率读取调试数据 - 谢谢
【解决方案2】:

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

【讨论】:

  • 感谢您的回答。不幸的是,我不能——我使用的是焊接到 MIDI 电缆上的 Arduino,而 MIDI 只能以 31250 的速率工作:arduino.cc/en/Tutorial/Midi
  • 我听说过这里描述的别名 - 但我不太明白:stackoverflow.com/questions/4968529/…
  • 你到底想要构建什么?
  • 一个 midi 设备 - 我想从 USB 读取数据进行调试 - Arduino 可以插入 USB。但是,Arduino 上的波特率设置为 31250 - 我如何以这种速度从 USB 读取数据?
  • 在与 MIDI 相同的端口上发送调试数据将混合两个数据流,除非您只是尝试从 PC 监控 MIDI 数据。在只有一个端口的板上,您也许可以使用SoftwareSerial 作为临时辅助调试端口,但您可能需要将输出从 TTL (+5v/0v) 电平转换为 RS232 (+12v/ -12v) 让您的电脑读取它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-04
  • 1970-01-01
  • 1970-01-01
  • 2013-03-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多