def can_to_trc(converter_name, thruster_id, serial_port, can_bitrate, output=None, verbose=False, speed=10): # Try and load the converter script with that name from converters module mod_path = f'converters.{converter_name}' lib = importlib.import_module(mod_path) # potentials = [x for x in dir(lib) if x.startswith("Captain")] CAN2TRCConverter = getattr(lib, "CAN2TRCConverter") converter = CAN2TRCConverter(thruster_id) dev = cantact.CantactDev(serial_port) dev.set_bitrate(can_bitrate) dev.start() # Go on the bus # Set up how we want to process outgoing nmea sentences if output is not None: target_type, addr, port = output.split(':') if target_type == 'udp': # NMEASender supports this sender = NMEASender(addr, int(port), verbose=verbose, checksum=True) out_fn = sender.send_one else: raise ValueError("Invalid output string", output) else: out_fn = print sleep_time = 1 / speed try: while True: frame = dev.recv() # Receive a CAN frame converter.update(frame) nmea = converter.nmea if nmea is not None: out_fn(nmea) if verbose: print(converter) print(frame) print(nmea) time.sleep(sleep_time) except KeyboardInterrupt as e: print("Interrupt!")
def decode(serial_port, can_ids, unpack_fmt): """ Print a CAN message in hex and the unpacked message according to some unpack """ dev = cantact.CantactDev(serial_port) dev.set_bitrate(500000) # Set the bitrate to a 1Mbaud dev.start() # Go on the bus try: while True: frame = dev.recv() # Receive a CAN frame if frame.id in can_ids: # unpacked = struct.unpack(unpack_fmt) hex_str = pretty_hex(frame.data) except KeyboardInterrupt as e: print("Interrupt!")
from pyvit import can from pyvit.hw import cantact from pyvit.utils.queue import CanQueue import sys dev = cantact.CantactDev(sys.argv[1]) dev.set_bitrate(125000) cq = CanQueue(dev) cq.start() print cq.recv() req = can.Frame(0x6A5) req.data = [0x10, 0xFF, 0xFF] cq.send(req) print cq.recv(arb_id=0x625, timeout=10) cq.stop()
# from pyvit import can # from pyvit.hw.cantact import CantactDev from pyvit.hw import cantact dev = cantact.CantactDev("/dev/tty.usbmodemFA131") dev.set_bitrate(500000) dev.start() while True: print(dev.recv())
print("Unable to locate a serial device") time.sleep(3) continue elif devCount == 1: print("Using device %s") % serialDev break elif devCount > 1: print( "Multiple serial devices plugged, in. Please remove any extraneous devices." ) time.sleep(3) continue else: serialDev = sys.argv[1] print("Connecting to %s") % serialDev dev = cantact.CantactDev(serialDev) dev.set_bitrate(500000) if sys.platform == "linux" or sys.platform == "linux2": # b/c SocketCAN dev.ser.write('S6\r'.encode()) drive = False if sys.platform == "linux" or sys.platform == "linux2": hasDevice = hasExternalStorage() if hasDevice: os.chdir(hasDevice) dev.start() while not drive: try: frame = dev.recv() except (KeyboardInterrupt, SystemExit): print("Exiting...") dev.stop()
from pyvit import can from pyvit.hw import cantact import sys import time dev = cantact.CantactDev("/dev/tty.usbmodem1411") dev.start() count = 0 while True: count += 1 print("%d: %s" % (count, str(frame)))
def trc_to_can(converter_name, serial_port, can_bitrate, source=None, verbose=False, thruster_id=None): # Try and load the converter script with that name from converters module mod_path = f'converters.{converter_name}' lib = importlib.import_module(mod_path) # potentials = [x for x in dir(lib) if x.startswith("Captain")] TRC2CANConverter = getattr(lib, "TRC2CANConverter") converter = TRC2CANConverter(thruster_id=thruster_id) dev = cantact.CantactDev(serial_port) dev.set_bitrate(can_bitrate) dev.start() # Go on the bus valid_nmea_received = 0 invalid_nmea_received = 0 def collect_metrics(): return { "valid_nmea_received": valid_nmea_received, "invalid_nmea_received": invalid_nmea_received, } # def on_nmea(nmea_list): # if source is None: print( "reading from stdin not yet supported. Try a udp string (udp:localhost:7777)" ) return else: target_type, addr, port = source.split(':') if target_type == 'udp': # NMEAReceiver supports this nr = NMEAReceiver(UDPReceiver(int(port))) nr.open() #nonlocal valid_nmea_received, invalid_nmea_received while True: try: print("reading...") nmea_list = nr.read() for nmea in nmea_list: frames = converter.convert(nmea) if frames: valid_nmea_received += 1 if verbose: print("NMEA Received : ", nmea) print("Frames = ", [str(x) for x in frames]) for f in frames: dev.send(f) else: invalid_nmea_received += 1 except KeyboardInterrupt as e: print("done.") sys.exit(0)