I would like to get a code example to send a simple CAN message with the J1939 stack using Python.
The following web site has a simple example to receive a J1939 message: https://justkding.me/thoughts/python-sae-j1939-socket-support
The code on this page works great for receiving:
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()
I have been reading through the docs, but I can't seem to find a simple way to send a J1939 message in Python.
Here's the reference to the kernel documentation: https://www.kernel.org/doc/html/latest/networking/j1939.html
Here's a C utility code of testj1939 that could be useful: https://github.com/linux-can/can-utils/blob/master/testj1939.c
Can someone post a simple code to send a message using the J1939 protocol in Python? Any documentation that shows how to properly do this would be appreciated.
Thank you.