hello
i "came" from C, i just started with python 3.6 and i wrote this:
it is a script that running on my pc emulate the inizialize sequence of an ECU (the things that are into motorbike, cars ecc) and also send to a MCU an array of fake sensors data
i "came" from C, i just started with python 3.6 and i wrote this:
it is a script that running on my pc emulate the inizialize sequence of an ECU (the things that are into motorbike, cars ecc) and also send to a MCU an array of fake sensors data
# -*- coding: utf-8 -*-
import serial, time
from random import randint
startCom = ["0x81", "0x12", "0xf1", "0x81", "0x5"]
startCom_ok = [0x80, 0xF1, 0x12, 0x03, 0xC1, 0xEA, 0x8F, 0xC0]
requestSens = ["0x80", "0x12", "0xf1", "0x2", "0x21", "0x8", "0xae"]
fakeSens = [0x80, 0xF1, 0x12, 0x34]
request = [None]*7
ECUConnected = False
lastByte = False
byte = 0
default_port = '/dev/ttyUSB0' # ' ' for windows
print ("default port is: '"+default_port+"' write if different")
user_input = input("Serial:") or default_port
print('Choosed port:', user_input)
ser = serial.Serial(
port=user_input,
baudrate=115200,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout = None
#timeout = 1
)
def send (toSend):
#send an array of int as binary data
for i in toSend:
ser.write(i.to_bytes(1,byteorder='little'))
#in python 2.7 use: ser.write(chr(i))
time.sleep(0.01)
def compare(received,correct,lengh):
#compare two array
#print ("received:",received)
#print ("correct:",correct)
coincident = 0
for i in range(len(correct)):
if received[i] == correct[i]:
coincident = coincident+1
if coincident == lengh:
return True
else:
return False
def calc_cs (arr):
#calculate checksum
cs = 0
for i in arr:
cs += i
cs &= 0xFF
return cs
print("Starting ECU Emulator")
while 1:
startTime = time.time()
while ser.in_waiting > 0:
#print "# incoming bytes:", ser.in_waiting
currentTime = time.time()
#print "time:", currentTime - startTime
while (currentTime - startTime < 2) and (lastByte == False):
in_bin = ser.read()
in_hex = hex(int.from_bytes(in_bin,byteorder='little'))
#for python 2.x use: in_hex = "0x"+in_bin.encode('hex')
#print ("incoming:", in_hex, " byte:", byte)
request[byte] = in_hex
byte += 1
currentTime = time.time()
if (in_hex == "0xae") or (in_hex == "0x5"):
lastByte = True
#print (request)
byte = 0
lastByte = False
if ECUConnected == False and compare(request,startCom,len(startCom)):
#time.sleep(0.04)
send(startCom_ok)
print('\nConnection established \n')
ECUConnected = True
if ECUConnected:
if compare(request,requestSens,len(requestSens)):
#time.sleep(0.04)
print("\nSending fake sensor data \n")
for i in range(0,52):
random = randint(0,255)
fakeSens.append(random)
cs = calc_cs(fakeSens)
fakeSens.append(cs)
'''
for i in fakeSens:
print (hex(i)),
'''
send(fakeSens)
del fakeSens[4:]
would you do something in a different way? if yes what?

but it is not immediate to read for user not used with high level languages