[英]Serial communication with bytes (0x00 problem)
我在為 USB CDC 使用串行通信時遇到問題。 我正在研究嵌入式 STM 微控制器,我想用 pySerial 傳遞二進制數據。
我想直接發送二進制代碼,但我被這個 0x00 阻止了。我的問題是當我想發送 0x00 字節時,下一個字節似乎被忽略了。
這是 python 腳本:
towrite = [ 0xFF, 0xx00 \xFF ]
nbSend = s.write(b'\xFF\x00\xFF')
print(nbSend) #Displaying 3 here, that means 3 bytes are successfully sended
然后嵌入腳本:
//Interrupt function that receive the data from serial com
static int8_t CDC_Receive_FS(uint8_t* Buf, uint32_t *Len)
{
/* USER CODE BEGIN 6 */
USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &Buf[0]);
USBD_CDC_ReceivePacket(&hUsbDeviceFS);
char buffer[64];
sprintf(buffer,"Length=%lu / Word=%c%c%c ",(unsigned long)*Len,Buf[0],Buf[1],Buf[2]);
debugPrintln(&huart2, buffer) //Function to print on serial to debug
//Here that print "Length=3 / Word=ÿ "
return (USBD_OK);
/* USER CODE END 6 */
}
如您所見,即使接收到的字節數很好,也不考慮第二個和最后一個字符(0x00,0xFF)......有什么建議嗎?
是調試不起作用,但數據傳輸很好。
sprintf(buffer,"Length=%lu / Word=%c%c%c ",(unsigned long)*Len,Buf[0],Buf[1],Buf[2]);
第二個%c
將一個實際的 NUL 字節放入緩沖區,因此debugPrintln
忽略它之后的所有其他內容。
對於測試,更改為:
sprintf(buffer,"Length=%lu / Word=%02X %02X %02X",(unsigned long)*Len,Buf[0],Buf[1],Buf[2]);
看看它是否顯示了所有字節。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.