Repository navigation
Expand file tree
/
Copy pathtransmit_lin_example.py
More file actions
executable file
·92 lines (79 loc) · 2.81 KB
/
Copy pathtransmit_lin_example.py
File metadata and controls
executable file
·92 lines (79 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import ics
import random
import time
# Tells our custom open_device() function to do special behavior
# with neoVI Server enabled so we can successfully open devices
enable_use_server = True
def open_device(index=0):
device = None
if enable_use_server:
# ics.open_device() won't open a device if we have handles open already
# so we need to find them and specify which ones to connect to.
devices = ics.find_devices()
print(
"Opening Device {} (Open Client handles: {})...".format(
devices[index], devices[index].NumberOfClients
)
)
ics.open_device(devices[index])
device = devices[index]
else:
print("Opening Device...")
device = ics.open_device()
print("Opened Device %s." % device)
return device
def calc_checksum(msg):
checksum = 0
for byte in msg.Header[1:] + msg.Data:
checksum += byte
if checksum > 255:
checksum -= 255
msg.Data += tuple([~checksum & 0xFF])
def transmit_lin(msg):
calc_checksum(msg)
msg.NumberBytesHeader = len(msg.Header)
msg.NumberBytesData = len(msg.Data)
try:
ics.transmit_messages(device, msg)
except ics.RuntimeError as ex:
print(ex)
raise ex
def prepare_message(device):
msg = ics.SpyMessageJ1850()
msg.StatusBitField = ics.SPY_STATUS_INIT_MESSAGE
msg.Protocol = ics.SPY_PROTOCOL_LIN
msg.NetworkID = ics.NETID_LIN
msg.Header = (0x11, 0x11, 0x22)
msg.Data = (0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x88)
transmit_lin(msg)
def receive_lin(device):
msgs, error_count = ics.get_messages(device)
print("Received {} messages with {} errors.".format(len(msgs), error_count))
netids = {ics.NETID_LIN: "LIN 1", ics.NETID_LIN2: "LIN 2"}
for i, m in enumerate(msgs):
print("\nMessage #{}: ".format(i + 1), end="")
netid = netids.get(m.NetworkID)
if netid is not None:
print("{}".format(netid), end="")
if m.StatusBitField & ics.SPY_STATUS_INIT_MESSAGE:
print(" | Init", end="")
data_list = []
for x in m.Header[1:]:
data_list.append(x)
for y in m.Data:
data_list.append(y)
print("\nID: {} | ".format(hex(m.Header[0])), end="")
if len(data_list):
checksum = data_list.pop()
print("Data: {} | ".format([hex(x) for x in data_list]), end="")
print("Checksum: {}".format(hex(checksum)))
if __name__ == "__main__":
import time
# Lets figure out how many are connected to the PC and display it
connected_count = len(ics.find_devices())
print("Found {} connected device(s)...".format(connected_count))
device = open_device(0)
prepare_message(device)
time.sleep(1)
receive_lin(device)
print("Finished.")