1

我必须使用 UARTSerial 类在 nucleo f446re 和带有 ubuntu 的 pc 之间发送数据数组。

我在 mbed 上使用的代码如下:

int main() {
    UARTSerial pc(USBTX, USBRX, 921600);
    uint8_t buff[256] = {
        5, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4
    };

    pc.sync();

    while(true) {
        pc.write(buff, 23);
        pc.sync();
        wait(1);
    }

    return 0;
}

我在电脑上运行的代码是:

int main() {
    struct termios tattr{0};

    // open the device in read/write sync
    int com = open("/dev/ttyACM0", O_RDWR | O_NOCTTY | O_SYNC );

    if (com == -1)
        throw std::runtime_error("ERROR: can't open the serial");

    tcgetattr(com, &tattr);

    tattr.c_iflag &= ~(INLCR|IGNCR|ICRNL|IXON);

    tattr.c_oflag &= ~(OPOST|ONLCR|OCRNL|ONLRET); 

    tattr.c_cflag = CS8 | CREAD | CLOCAL; 

    tattr.c_lflag &= ~(ICANON|ECHO);    

    tattr.c_cc[VMIN] = 1;

    tattr.c_cc[VTIME] = 0;

    tattr.c_ispeed = 921600;
    tattr.c_ospeed = 921600;

    tcsetattr (com, TCSAFLUSH, &tattr);

    while (true) {
        usleep(1000);
        tcflush(com, TCIOFLUSH);
        uint8_t buff[24];
        ::read(com, buff, 23);

        printf("reading frame... ");
        for (auto b : buff) {
            printf("%02X ", b);
        }
        puts("\n");
    }
}

我在电脑上收到的输出是:

[...]
reading frame... 00 00 8D 9C 1E 7F 00 00 00 00 00 00 00 00 00 00 70 5B C7 01 AD 55 00 00 

reading frame... 00 00 8D 9C 1E 7F 00 00 00 00 00 00 00 00 00 00 70 5B C7 01 AD 55 00 00  
[...]

如您所见,结果与我期望的不一样。

我已经尝试使用循环一次发送一个字节,但结果是一样的。

我不明白为什么我无法读取我试图在 pc 和 nucleo 板上刷新 USB 的 USB。

4

2 回答 2

0

您必须使用解码器来解码来自串行端口的字节,请参阅以下链接: https ://codereview.stackexchange.com/questions/200846/a-simple-and-efficient-packet-frame-encoder-decoder

于 2019-07-08T13:55:25.273 回答
0

我发现了问题。这是波特率的设置,我必须使用以下几行:

// receive speed
cfsetispeed (&tattr, B921600);
// transmit speed
cfsetospeed (&tattr, B921600);

而不是这个:

// receive speed
tattr.c_ispeed = 921600;
tattr.c_ospeed = 921600;
于 2019-07-08T15:36:30.893 回答