首页 > 解决方案 > 将有符号和无符号字符都写入 write()

问题描述

我正在尝试通过 Raspberry Pi 上的串口与 Simplebgc 板通信。我正在使用一组 unsigned char 来向董事会写入命令来表示再见。以下命令只需要无符号字节,因此以下命令有效,但如果我想发送一个需要无符号字节和有符号字节混合的命令,我该怎么做?

void sendCommand() {
    int fd;

    if ((fd = serialOpen ("/dev/ttyS0", 115200)) < 0) {
        //fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno));
        cout<<"Unable to open serial device"<<endl;
        return;
    }

    unsigned char board_info[6] = {0x3E, 0x56, 0x01, 0x57, 0x01, 0x01};  //BOARD_INFO

    serialFlush(fd);

    // Send command to grab board info
    write(fd, board_info, 6);
    sleep(2);

    // Read board response and print it
    char c;
    int counter = 0;
    while (read(fd, &c, 1) == 1) {
        //putchar(c);  // print out char    
        printf("%d ",c);
        counter++;
    }
    cout<<"\ncounter="<<counter<<endl;
    sleep(5);


}

int main() {
    sendCommand();
    return 0;
}

标签: c++binarysigned

解决方案


推荐阅读