Repository navigation
Expand file tree
/
Copy pathtransmit_can_example.py
More file actions
58 lines (47 loc) · 2.08 KB
/
Copy pathtransmit_can_example.py
File metadata and controls
58 lines (47 loc) · 2.08 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
import ics
# 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 dev_name(device):
# Return a friendly name of the device (ie. neoVI FIRE2 CY1234)
if int("AA0000", 36) <= device.SerialNumber <= int("ZZZZZZ", 36):
return device.Name + " " + ics.base36enc(device.SerialNumber)
else:
return device.Name + " " + str(device.SerialNumber)
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(dev_name(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." % dev_name(device))
return device
def transmit_can(device):
msg = ics.SpyMessage()
msg.ArbIDOrHeader = 0x01 # CAN Arbitration ID
msg.Data = (1,2,3,4,5,6,7,8) # Data Bytes go here
msg.NetworkID = ics.NETID_HSCAN # First channel of CAN on the device
# msg parameter here can also be a tuple of messages
ics.transmit_messages(device, msg)
def receive_can(device):
msgs, error_count = ics.get_messages(device)
print("Received {} messages with {} errors.".format(len(msgs), error_count))
for i, m in enumerate(msgs):
print('Message #{}\t'.format(i+1), end='')
print('\tArbID: {}\tData: {}'.format(hex(m.ArbIDOrHeader), [hex(x) for x in m.Data]))
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))
tx_dev = open_device(0)
rx_dev = open_device(1)
transmit_can(tx_dev)
receive_can(rx_dev)
print("Finished.")