def read(self, timeout=None): self.sock.settimeout(timeout) try: frame, addr = self.sock.recvfrom(16) return CanMessage.from_raw(frame) except socket.timeout: return None
def read(self, timeout=None): self.sock.settimeout(timeout) try: frame, addr = self.sock.recvfrom(16) msg = CanMessage.from_raw(frame) print("recv: ", msg) return msg except socket.timeout: return None
__author__ = 'hd' import time import random from socketcan import CanMessage, CanSocket sock = CanSocket() sock.open("can0") msg = CanMessage(0x133701ef, [0,0,255,255]) colors = [0]*10 h = 0 while True: h = (h + random.randint(10, 120)) % 360 colors = colors[1:] + [h] for i, h in enumerate(reversed(colors)): msg.data[0] = i msg.data[1] = int(h/2) sock.send(msg) time.sleep(1)
__author__ = 'hd' import time from socketcan import CanMessage, CanSocket sock = CanSocket() sock.open("can0") msg = CanMessage(0x133701ef, [0,0,255,255]) h = 0 while True: msg.data[0] = 0 msg.data[1] = h % 180 sock.send(msg) msg.data[0] = 1 msg.data[1] = (h + 20) % 180 sock.send(msg) msg.data[0] = 2 msg.data[1] = (h + 40) % 180 sock.send(msg) h += 1 time.sleep(0.05)
while not do_quit: msg = echo_sock.read(0.1) if msg is not None: echo_sock.send(msg) if_master = sys.argv[1] if_slave = sys.argv[2] iterations = int(sys.argv[3]) echo_thread = threading.Thread(target=can_echo_thread, args=(if_slave, )) echo_thread.start() sock = CanSocket() sock.open(if_master) msg = CanMessage(0x13371010, [0, 0, 0, 0, 0, 0, 0, 0]) t_start = time.time() for i in range(0, iterations): sock.send(msg) msg = sock.read() x = msg.data[0] msg.data[0] = (x + 1) % 256 t_finish = time.time() do_quit = True print("\n\nsent and received %d messages in %f seconds." % (iterations, t_finish - t_start))