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
| import os
import sys
import time
import signal
# application specific imports
import simply_py as simply
from simply_py import Message
def format_status(sts):
flags = ["---"] * 6
if sts & 0x02: #CAN_STATUS_RESET
flags[4] = "RST"
if sts & 0x04: #CAN_STATUS_BUSOFF
flags[3] = "BOF"
if sts & 0x08: #CAN_STATUS_ERRORSTATUS
flags[2] = "ERR"
if sts & 0x10: #CAN_STATUS_RXOVERRUN
flags[1] = "RxO"
if sts & 0x20: #CAN_STATUS_TXOVERRUN
flags[1] = "TxO"
if sts & 0x40: #CAN_STATUS_PENDING
flags[0] = "PDG"
return " ".join(flags)
def error():
err = simply.get_last_error()
print("Error:", simply.get_error_string(err))
simply.close()
sys.exit(-1)
def signal_handler(signal, frame):
print('You pressed Ctrl+C!')
simply.close()
sys.exit(0)
def receive_messages():
res, msg = simply.receive()
if res == 1:
print(msg)
return True
elif res == -1:
error()
return False
def send_message(msg):
simply.send(msg)
msg.ident = (msg.ident + 1) % 0x3FF
def main(ser_port):
print("\n#### simplyCAN Demo 1.0 (c) 2018-2019 HMS ####\n")
# abort with Ctrl+C
signal.signal(signal.SIGINT, signal_handler)
if not simply.open(ser_port): error()
id = simply.identify()
if not id: error()
print("Firmware version:", id.fw_version)
print("Hardware version:", id.hw_version)
print("Product version: ", id.product_version)
print("Product string: ", id.product_string)
print("Serial number: ", id.serial_number)
res = simply.stop_can() # to be on the safer side
res &= simply.initialize_can(250)
res &= simply.start_can()
if not res: error()
lastSent = 0
TxMsg = Message(0x100, [1,2,3,4,5,6,7,8])
print("Run application...")
while True:
if time.time() - lastSent > 1.0: # one message every second
lastSent = time.time()
send_message(TxMsg)
print("CAN Status:", format_status(simply.can_status()[0]))
if not receive_messages():
time.sleep(0.01)
#main(simply.retrieve_serial_port())
main(b"/dev/ttyACM0") # for Linux
#main(b"COM5") # for Windows |
Partager