Question

Playing with a MCU, I'm trying to communicate through a RS232 link to a PC desktop.

I've made a test program really simple: send "Hello world!\n" every n ms.

Most relevant link connection parameters are:

  • Port: /dev/ttyUSB0
  • Baudrate: 57600 bps
  • 8 data bits
  • 1 bit stop
  • No parity and hardware control

To check that I receive data from MCU to PC, I set /dev/ttyUSB0 with:

%> stty -F /dev/ttyUSB0 57600 cooked

and then,

%> cat /dev/ttyUSB0
Hello world!
Hello world!
Hello world!
...

So, I receive data from the MCU properly.

But, here the problem, when I want use Python with pySerial module, data are absolutely wrong:

>>> s = serial.Serial('/dev/ttyUSB0', 57600)
>>> print s
Serial<id=0x195fed0, open=True>( 
    port='/dev/ttyUSB0', baudrate=57600, 
    bytesize=8, parity='N',  
    stopbits=1, timeout=None, 
    xonxoff=False, rtscts=False, 
    dsrdtr=False)

>>> s.read(14)
'\xc8\xe5\xec\xec\xef\xa0\xf7\xef\xf2\xec\xe4\xa1\x8a\x80'

At this point, I've changed "Hello world!\n" output string (MCU side) for this string: 0xde, 0xad, 0xbe, 0xef. And, what I get with pySerial is:

>>> s.read(4)
'\xde\xad\xbe\xef'

It works! I've changed another time, now for this: 0x00, 0x01, 0x02, 0x03, 0x80, 0x81, 0x82, 0x83. Result:

>>> s.read(8)
'\x80\x81\x82\x83\x80\x81\x82\x83' 

Conclusion: pySerial changes bytes value smaller than 128 for same value plus 128.

I don't know why this is happening. I used pySerial module in other occasions (version 2.4) and all worked fine. I've tried with 2.5 and 2.6 versions in Ubuntu 11.10 and Ubuntu 10.04 but no works. Any clue?

PD: Be sympathetic with my English writing. I'm not proficient in English as I should be.

Was it helpful?

Solution

Ok, my fault! PySerial works perfect. Revising code in MCU, I found one mistake unsetting a bit that should be set (I was sending 7-bit character length). Then, frame received was, obviously, partially well, partially wrong.

Now, I can understand why pySerial show me bad results. Nevertheless, is a mystery for me how my PC reconstruct from 7-bit data, characters of 8-bit length. I was absolutely convinced that MCU's code was fine because data from terminal was correct :S

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top