将有符号和无符号字符都写入write()

时间:2018-12-20 03:22:43

标签: c++ binary signed

我正在尝试通过Raspberry Pi上的串口与Simplebgc板通信。我正在使用未签名的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;
}

0 个答案:

没有答案