Help with pyserial and sending binary data?

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Fri May 2 17:29:13 EDT 2008


En Fri, 02 May 2008 16:50:46 -0300, Rich <richietommy at yahoo.com> escribió:

> I am working on a python library for sending and receiving data from a  
> Subaru's ECU (the fuel injection computer) via the OBD-II port and an  
> OBD to USB cable, with the Subaru Select Monitor protocol. There are a  
> #--------------------------
> import serial, string
> output = " "
> ser = serial.Serial('/dev/ttyUSB0', 4800, 8, 'N', 1, timeout=1)
> ser.write(chr(0x80)+chr(0x10)+chr(0xF0)+chr(0x01)+chr(0xBF)+chr(0x40))
> while output != "":
>    output = ser.read()
>    print hex(ord(output))
> #--------------------------
>
> The only problem is that when I send the ECU init command, it just echos  
> back the data I sent it, which, from my understanding, means that I sent  
> an invalid request packet.[...]
> My best guess I have heard is that chr() isn't encoding the hex value  
> properly to a byte value the ECU understands (wrong endianness?), or  
> chr() encoding isn't always representing hex as an 8-bit byte. chr()  
> should always encode to an 8-bit byte (not 7-bit ASCII or anything,  
> correct)? Is there any way to switch byte endianness, or ensure I am  
> encoding 8-bit bytes? Any help is much appreciated. Thanks!

No, chr works as it should. The same thing can be written as  
ser.write("\x80\x10\xF0\x01\xBF\x40")
Are you sure you're talking to the right device, /dev/ttyUSB0? Are the  
comm parameters correct?
(BTW, if you get more than a single character per loop, the print  
statement will fail - try with ser.read(1))

-- 
Gabriel Genellina




More information about the Python-list mailing list