0

I'm trying to send some data from Python 3.5 program on Windows 10 PC to TM4C microcontroller over USB and I'm using PyUSB for that.

The issue is whenever byte value exceeds 127 (0x7f) PyUSB is adds one extra byte before that byte and sometimes changes original value too. Here is the section of code I'm using to send data

def send_data(data):                        # data is list of integers
    message = ''.join(chr(i) for i in data)
    TivaC.epOut.write(message)              #TiVaC is USB object

Some of the data packets are as follows:

sending ..0x66 0x12 0x0 0x0 0x0 0x6 
Receiving 0x66 0x12 0x0 0x0 0x0 0x6 
New Message 

sending ..0x66 0x12 0x0 0x7f 0x0 0x6 
Receiving 0x66 0x12 0x0 0x7f 0x0 0x6 
New Message 

sending ..0x66 0x12 0x0 0x80 0x0 0x6 
Receiving 0x66 0x12 0x0 0xc2 0x80 0x0 0x6 
Corrupt Packet 

sending ..0x66 0x12 0x0 0xbf 0x0 0x6 
Receiving 0x66 0x12 0x0 0xc2 0xbf 0x0 0x6 
Corrupt Packet 

sending ..0x66 0x12 0x0 0xc0 0x0 0x6 
Receiving 0x66 0x12 0x0 0xc3 0x80 0x0 0x6 
Corrupt Packet 

sending ..0x66 0x12 0x1 0xf4 0x0 0x6 
Receiving 0x66 0x12 0x1 0xc3 0xb4 0x0 0x6 
Corrupt Packet 

sending ..0x66 0x12 0x3 0xe8 0x0 0x6 
Receiving 0x66 0x12 0x3 0xc3 0xa8 0x0 0x6 
Corrupt Packet 

The problem is only in the sending from PC to microcontroller. Microcontroller always sends all bytes correctly. I have checked it.

martineau
  • 119,623
  • 25
  • 170
  • 301
Yogesh
  • 33
  • 5
  • 1
    My guess would be that it is encoding the string as UTF8 - hence the two bytes for values over 127. However, I'm not sure how to sort it (something with bytes?) so this isn't an answer. – neil Feb 27 '17 at 20:02
  • 3
    You should be using bytes objects (``b''``) here, not strings. – jasonharper Feb 27 '17 at 20:33
  • Adding on @jasonharper: If you received a `list` of `int`, you probably want to change `message = ''.join(chr(i) for i in data)` to just `message = bytes(data)` – ShadowRanger Feb 27 '17 at 20:38
  • It worked with byte objects. Thank you guys! – Yogesh Feb 27 '17 at 20:49

0 Answers0