pySerial works in miniterm but not in my app

Dario d.corti at gmail.com
Sat Nov 1 16:30:43 CET 2014


Il giorno sabato 1 novembre 2014 16:04:06 UTC+1, Dario ha scritto:

> BUT.. plot twist: in Windows XP, the very same python code and usb adapter are working just right (python 2.7 and pySerial 2.7). Also with c#, no issues.

I compared the behaviour of mono and python (2.7 and 3.3) on the same hw and os, I guess something is wrong with pySerial implementation...

Mono code on Mint:

----
SerialPort s = new SerialPort("/dev/ttyUSB0", 19200, Parity.None, 8, StopBits.One);
s.Open();

s.Write("sw o01 +\r");

while (true)
    Console.Write(Convert.ToChar(s.ReadByte()));
----

Device reacts correctly and I get back what I expect (the first line is because I sent the command via com port, the second is because I pushed a button on the device):

dario at ivymint ~ $ sudo ./Test1.exe 
sw o01 + Command OK
Button 1 pushed


Now equivalent Python 3.3 code on Mint:

---
import serial

s = serial.serial_for_url('/dev/ttyUSB0', 19200, bytesize = 8, parity = 'N', stopbits = 1)

s.close()
s.open()
s.write(bytearray('sw o01 +\r','ascii'))

while True:
	print(s.read())
---

In this case, I don't receive anything for my command, and when I press I receive garbage instead of "Button 1 pushed"

dario at ivymint ~ $ sudo python3 ./test2.py 
b'\xfc'
b'\x8f'
b'\r'
b'\x85'
b'1'
b'+'
b'\xfe'



More information about the Python-list mailing list