473,837 Members | 1,436 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with pyserial and sending binary data?

Hello,

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 few open source programs that do this already (http://romraider.com/ , http://jdash.sourceforge.net/ , http://tari.co.za/downloads/software/source/ ), but they are written in Java or C++. I have never done anything with serial before, and very little with binary data, so this is all new to me.

The SSM protocol is fairly simple - a request packet (all in binary, represented in hex) consists of a header (0x80), destination byte (0x10 for the ECU), source byte (0xf0 for the diagnostic tool - AKA your computer), Payload length in hex, the Payload, which is one of 6 known commands(0xA0 Read memory, 0xA8 Read single address, 0xB0 Write memory, 0xB8 Write single address, 0xBF ECU init) and addresses or address range, then the checksum byte, which is the 8 least-significant bits of the sum of the rest of the request packet.

A returned packet consists of a header, destination byte (your computer), source byte (ECU), payload size (all data between payload size and checksum byte), and the same checksum byte. Further reading (pretty good documentation on the protocol) is here: http://tari.co.za/downloads/documentation/ssm.pdf , but that is the basics to at least say hi to your car from your computer.

So I've been messing with it, and in as few lines as possible, this should in theory, send the init command to the ECU (0x80 0x10 0xF0 0x01 0xBF 0x40), and the ECU should return its ID to let you know that you can now chat (something like 0x80 0xF0 0x10 0x39 0xFF 0xA2 0x10 0x0F 0x1B 0x14 0x40 0x05 0x05 0x73 0xFA 0xEB ......):

#--------------------------
import serial, string
output = " "
ser = serial.Serial('/dev/ttyUSB0', 4800, 8, 'N', 1, timeout=1)
ser.write(chr(0 x80)+chr(0x10)+ chr(0xF0)+chr(0 x01)+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. I'm guessing that there aren't many people on this list with OBD-II subarus and an open port cable ( http://www.tactrix.com/product_info.php?products_id=36 ) that could test this out, but in the off chance there is, perhaps you could help. I've been chatting with the RomRaider developers ( http://www.romraider.com/forum/topic3129.html ) on the subject, but they are Java guys, not Python guys. There is more encompassing code there, but something lies wrong in my basic implementation above.

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!

-rich

_______________ _______________ _______________ _______________ _______________ _________
Be a better friend, newshound, and
know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i...Dypao8Wcj9tAcJ
Jun 27 '08 #1
1 15114
Rich <ri*********@ya hoo.comwrote:
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.
[snip]
So I've been messing with it, and in as few lines as possible, this
should in theory, send the init command to the ECU (0x80 0x10 0xF0
0x01 0xBF 0x40), and the ECU should return its ID to let you know
that you can now chat (something like 0x80 0xF0 0x10 0x39 0xFF 0xA2
0x10 0x0F 0x1B 0x14 0x40 0x05 0x05 0x73 0xFA 0xEB ......):
>
#--------------------------
import serial, string
output = " "
ser = serial.Serial('/dev/ttyUSB0', 4800, 8, 'N', 1, timeout=1)
ser.write(chr(0 x80)+chr(0x10)+ chr(0xF0)+chr(0 x01)+chr(0xBF)+ chr(0x40))
while output != "":
output = ser.read()
print hex(ord(output) )
#--------------------------
The code looks OK.

With a constructor which takes as many arguments as Serial does I tend
to name them to avoid mistakes, eg

serial.Serial("/dev/ttyUSB0", baudrate=4800, bytesize=8, parity='N', stopbits=1, timeout=1)

8N1 is documented as the default so you could then reduce it to the
following if you wanted

serial.Serial("/dev/ttyUSB0", baudrate=4800, timeout=1)
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 experience with these sort of protocols is that if you make a
packet error (wrong address, wrong checksum, wrong start byte) you'll
get no reply at all.

If you make a command error (use a command that isn't understood in a
valid packet) you'll get a properly formatted reply with an error
message in. This should start 0x80 0xF0 0x10 .... (from and to
reversed).

I'd suspect that if you are just receiving the data back then you have
got your serial cable wrong and have somehow looped tx and rx. Did
you try your cable with the other programs? Or possibly you are using
the wrong serial port - are you sure you haven't any other USB
serials? ls /dev/ttyUSB* on a udev system will show you. lsusb (as
root) is useful as is dmesg immediately after plugging the port in.

I do a lot of this sort of thing at work (not with cars though with
satellite equipment) and it is always the first packet and the first
response which is the hard part. After that it is usually plain
sailing!

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Jun 27 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1986
by: Kelia Nichols | last post by:
Hello, I am using Pyserial to work with a RS232 device. My question is, how do I write hex to the device or cannot write hex to it pyserial? Kelia Nichols ______________________________________ No trees were killed in the sending of this message. However a large number of electrons were terribly inconvenienced.
3
7164
by: alastair | last post by:
Hi, I'm using pyserial to transfer data from PC to another device - the data is either from an ASCII file or binary data file. Everything works fine up until I start using files of a particular size on Linux, around 1.3MB. When I send the data on windows everything is ok - on Linux I get the following traceback:
3
4784
by: ouz as | last post by:
hi, I want to transfer 0 bit and 1 bit in order with pyserial.But pyserial only send string data. Can anyone help me? Sorry about my english.. _________________________________________________________________ Depolama alani sikintisindan kurtulun - hemen Hotmail'e üye olun! http://odeme.hotmail.com.tr
13
11059
by: Bob Greschke | last post by:
We have some equipment that communicates at 57600 baud RS232. The path from the PC is USB to a Phillips USB hub, then off of that a TUSB3410 USB/Serial converter. The driver for the 3410 chip creates a "normal" comm port (like COM3). There is a C++ program that has no problem talking to the equipment, but two Python programs have trouble. One is a big, scary, controller program, and the other, since that is the one I'm having trouble...
15
16899
by: Lone Wolf | last post by:
I'm trying to get data through my serial port from a CMUcam. This gizmo tracks a color and returns a packet of data. The packet has nine data points (well, really eight since the first point is just a packet header) separated by spaces as follows: M xxx xxx xxx xxx xxx xxx xxx xxx Here is the code I am using (python v24): import serial
0
596
by: pauland80 | last post by:
<snip> <snip> Late thanks for your both answers! (Please excuse me for that) The problem was a bug in the device firmware. But before finding this, I dugg lightly in the pyserial source and found (to take with care!) :
0
1368
by: Gabriel Genellina | last post by:
En Fri, 02 May 2008 16:50:46 -0300, Rich <richietommy@yahoo.comescribió: 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))
3
6226
by: Rainy | last post by:
Hello! I'm having some trouble with pyserial package, I'm sending commands and reading responses from a custom pcb, and sometimes I get a proper response, at other times I get nothing, and sometimes I get about half of the response string with beginning cut off. About half the time an empty string is returned, the other ~half time good response, and more rarely I get partial response. When I try to use the same Serial instance to...
5
6265
by: zxo102 | last post by:
Hello All, I have a system. An instrument attched to 'com1' is wireless connected to many sensors at different locations. The instrument can forward the "commands" (from pyserial's write()) to those sensors. Based on the "commands", the sensors keep sending corresponding data back to the instrument which wraps up those data and put into "com1" . The readlines() of pyserial pick up those data for processing. The data ’string' does...
0
9682
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10564
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10621
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10268
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9396
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7806
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6998
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5668
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5846
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.