我正在尝试通过Raspberry Pi上的串口与Simplebgc板通信。我正在向板子写命令,这似乎正在工作,但是我需要解码二进制响应的帮助。当指南似乎增加到18个时,为什么会出现23个再见?我是C语言和二进制语言的新手。
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 motor_on[6] = {0x3E, 0x4D, 0x01, 0x4E,0x01, 0x01}; //MOTOR ON
unsigned char motor_off[6] = {0x3E, 0x6D, 0x01, 0x6E,0x01, 0x01}; //MOTOR OFF
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;
}
输出:
pi @ raspberrypi:〜/ myPrograms / SerialGPIOExamples / c ++ / SBGC_board $ ./serialSBGCTest 62 86 18 104 30 70 10 0 27 0 0 0 0 0 0 0 0 0 0 0 0 0 137 counter = 23
答案 0 :(得分:2)
响应是带有标题的消息:
消息格式
每个命令都由标头和正文组成,都带有校验和。标头错误的命令 或身体校验和,或身体大小与预期不同,则应忽略。解析器应扫描 下一个起始字符的传入数据流,并尝试从中恢复同步。
Header:
Start Character 1u
Command ID 1u
Payload Size 1u
Header Checksum 1u
Body
18 bytes as defined by you.
Body Checksum 1u
这将为您提供23个字节。 4个字节的标头。身体。 1字节的正文校验和。