Skip to content

Instantly share code, notes, and snippets.

@karlding
Created April 25, 2020 09:45
Show Gist options
  • Save karlding/cd51c45794417d255b4e0a82816f4b17 to your computer and use it in GitHub Desktop.
Save karlding/cd51c45794417d255b4e0a82816f4b17 to your computer and use it in GitHub Desktop.
A partial re-implementation of testj1939 from can-utils in Python to test bpo-40291
"""A partial re-implementation of testj1939 from can-utils in Python to test
https://github.com/python/cpython/pull/19538
"""
import socket
def main():
with socket.socket(
family=socket.PF_CAN, type=socket.SOCK_DGRAM, proto=socket.CAN_J1939
) as s:
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
addr = "vcan0", socket.J1939_NO_NAME, socket.J1939_NO_PGN, socket.J1939_NO_ADDR
s.bind(addr)
while True:
data, addr = s.recvfrom(128)
print("{:02x} {:05x}:".format(addr[3], addr[2]), end="")
for j in range(len(data)):
if j % 8 == 0 and j != 0:
print("\n{:05x} ".format(j), end="")
print(" {:02x}".format(data[j]), end="")
print("\n", end="")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment