Help with SSM/Python

Developer topics relating to software that logs data from ECUs

Moderator: Freon

Help with SSM/Python

Postby hotspoons » Thu May 01, 2008 5:59 pm

Hey guys,

I'm trying to get Python to talk to my ECU. I've read over a the 'SSM.PDF' file that explains the protocol, and I think I have enough of a handle on things to get started. I have something started...the code is below. It requires that PySerial
be installed with Python, or is in a directory called 'serial' directly below the script below.

The script should work on Windows...just replace '/dev/ttyUSB0' with 'COM4' or which ever com port you are using, and you will need to have python 2.x installed as well.

Code: Select all
#!/usr/bin/env python


import serial, string, binascii

HEADER = (0x80)
ECU_ID = (0x10)
DIAGNOSTIC_TOOL_ID = (0xF0)
READ_PADDING = (0x00)
READ_MEMORY_COMMAND = (0xA0)
READ_MEMORY_RESPONSE = (0xE0)
READ_ADDRESS_COMMAND = (0xA8)
READ_ADDRESS_RESPONSE = (0xE8)
WRITE_MEMORY_COMMAND = (0xB0)
WRITE_MEMORY_RESPONSE = (0xF0)
WRITE_ADDRESS_COMMAND = (0xB8)
WRITE_ADDRESS_RESPONSE = (0xF8)
ECU_INIT_COMMAND = (0xBF)
ECU_INIT_RESPONSE = (0xFF)
COM_PORT = '/dev/ttyUSB0'


def GenChecksum(ByteList):
   checksumTotal = 0
   s = ''
   for i in ByteList:
      checksumTotal = checksumTotal + i
   if checksumTotal < 0:
      return "-" + alt_bin(-checksumTotal)
   while checksumTotal != 0:
      if checksumTotal % 2 == 0:
         bit = '0'
      else:
         bit = '1'
      if  len(s) < 8:
         s = bit + s
      checksumTotal >>= 1
   return int(s, base=2)


def ByteToHex( byteStr ):     
    return ''.join( [ "%02X " % ord( x ) for x in byteStr ] ).strip()

def ECUInit():
   ser = serial.Serial(COM_PORT, 4800, 8, 'N', 1, timeout=.1)
   cmdByteStr = ""
   header = [HEADER, ECU_ID, DIAGNOSTIC_TOOL_ID]
   command = ECU_INIT_COMMAND
   cmd = []
   for el in header:
      cmd.append(el)
   cmd.append(1)
   cmd.append(command)
   cmd.append(GenChecksum(cmd))
   for i in cmd:
      cmdByteStr = cmdByteStr + chr(i)
   print cmdByteStr
   print ByteToHex(cmdByteStr)
   line = cmdByteStr   
   while line == cmdByteStr:
      x = ser.write(cmdByteStr)
      line = ser.readline()
      print line
      print str(ByteToHex(line))


def ECUReadAddress():
   ser = serial.Serial(COM_PORT, 4800, 8, 'N', 1, timeout=.1)
   cmdByteStr = ""
   header = [HEADER, ECU_ID, DIAGNOSTIC_TOOL_ID]
   command = READ_ADDRESS_COMMAND
   payLoad = [(0x08),(0x1C)]
   cmd = []
   for el in header:
      cmd.append(el)
   cmd.append(len(payLoad)*4)
   cmd.append(command)
   cmd.append(READ_PADDING)
   for el in payLoad:
      cmd.append(READ_PADDING)
      cmd.append(READ_PADDING)
      cmd.append(READ_PADDING)
      cmd.append(el)
   cmd.append(GenChecksum(cmd))
   for i in cmd:
      cmdByteStr = cmdByteStr + chr(i)
   print cmdByteStr
   print ByteToHex(cmdByteStr)   
   x = ser.write(cmdByteStr)
   line = ser.readline()
   print line
   print str(ByteToHex(line))
         
if __name__ == "__main__":
   ECUInit()
   ECUReadAddress()



So far, the script tries to send the init command to the ECU, and upon receiving something back besides what was sent, it will try to read an arbitrary address once.

The problem is that it never gets out of the init function as it never sends anything back but the command it is trying to send (it should be sending back something like 0x80, 0xF0, 0x10, something, then the ECU ID and checksum). I'm almost positive I am sending the correct data for initializing the ECU...it shows up as strange glyphs in the terminal (I don't know if these will post correctly, but here...output from the terminal, glyphs top, hex coverted from byte code below:)

���@
80 10 F0 01 BF 40
�T
80 10 F0 08 A8 00 00 00 00 08 00 00 00 1C 54

and converting from byte to hex string gives back the exact values I found in the ssm.pdf documents on a couple of examples. Maybe I am missing a step or something...I am using the tactrix cable, and following what I thought was needed to initialize and request data from the ECU...but perhaps I missed something. Any thoughts or ideas would be much appreciated.

Attached is the script above, and the library it depends on. Thanks,

-Rich
Attachments
read_address.zip
(29.8 KiB) Downloaded 193 times
hotspoons
 
Posts: 2
Joined: Mon Jun 18, 2007 7:20 pm

Return to Data Logging Software

Who is online

Users browsing this forum: No registered users and 2 guests