c++ - 将有符号和无符号字符都写入 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++ - 就 SFINAE 而言,访问不存在的成员是否不被视为“错误”?
- php - auth laravel 总是返回 false
- javascript - 如何添加变量饼图以响应项目?
- flutter - 同时在多个连接的设备/模拟器上运行颤振应用程序
- c# - 如何使用密码修复更新语句
- mysql - 如何为mysql中的每个组赋予相同的值
- html - 有人可以解释一下 flex-basis 属性吗?
- jboss-eap-6 - jboss-eap-6 支持 postgres 9.6 BDR
- javascript - JavaScript“this”不适用于 HTML 元素
- c++ - 将功能与“int main”问题分离(初学者)