简体   繁体   中英

Serial communication with bytes (0x00 problem)

I have a problem while using serial communication for USB CDC. I'm working on embedded STM microcontrollers and i wanted to pass binary data with pySerial.

I wanted to send binary codes directly but I am blocked by this 0x00.. My problem is when i want to send 0x00 byte, the next bytes seems to be ignored..

Here's the python script:

towrite = [ 0xFF, 0xx00 \xFF ] 
nbSend = s.write(b'\xFF\x00\xFF')
print(nbSend)                                 #Displaying 3 here, that means 3 bytes are successfully sended

Then the embedded script:

//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 */
}

As you can see, the second and ultimate characters (0x00, 0xFF) are not taken into account even if the number of bytes received is good... Any advice?

It's the debugging that's not working, but the data transfer is just fine.

sprintf(buffer,"Length=%lu / Word=%c%c%c ",(unsigned long)*Len,Buf[0],Buf[1],Buf[2]);

The second %c puts an actual NUL byte in the buffer, so everything else after it is ignored by the debugPrintln .

For testing, change to:

sprintf(buffer,"Length=%lu / Word=%02X %02X %02X",(unsigned long)*Len,Buf[0],Buf[1],Buf[2]);

and see if it shows you all the bytes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM