J1939 Messages - dfieschko/RP1210 GitHub Wiki
The RP1210.J1939
package contains functions and classes for generating and parsing J1939 messages, as well as functions that can assist in configuring an RP1210 adapter for connecting to a J1939 bus.
Functions
toJ1939Message
This function can be used to generate bytes for RP1210_SendMessage without the overhead of the J1939Message
class.
toJ1939Message(pgn, pri, sa, da, data, size = 0, how = 0) -> bytes
String arguments are read as base-10! If you want to provide an argument in base-16, do it as an int or byte string.
RP1210_SendMessage J1939 format:
- PGN (3 bytes)
- Priority (1 byte)
- Source Address (1 byte)
- Destination Address (1 byte)
- Message Data (0 - 1785 byes)
Leaving size = 0
to its default value will automatically determine message size based on len(data).
The how
parameter determines whether your RP1210 adapter will send the message via RTS/CTS or BAM transport methods. If how
is set to zero, it will automatically choose between message types, so you're usually best off leaving how
to its default value of 0.
If you're not sure what to do with some of these arguments due to differences between PDU1 and
PDU2 messages, you can most likely get away with leaving irrelevant portions blank (set to 0);
your RP1210 adapter drivers will format the message for you (or just use the J1939Message
class).
toJ1939Request
Calls and returns toJ1939Message()
with PGN = 0x00EA00 (with Destination Address set from da
param). Will automatically size message size to 3 if size = 3
param is left to its default value.
toJ1939Request(pgn_requested, sa, da = 255, pri = 6, size = 3) -> bytes
J1939Message Class
A class for parsing or generating an RP1210 J1939 message.
Parsing a J1939 message:
msg = J1939Message(client.rx()) # where client is instance of RP1210Client
msg_pgn = msg.pgn # access properties directly
... # etc, for all properties in 'Accessible properties' below
- If you used the command 'Set Echo Transmitted Messages' to turn echo on, set arg echo = True.
Generating a J1939 message:
msg_data = ... # bytes
msg = J1939Message(data=msg_data, pgn=0xEA00, sa=0xF9, da=0xBC, pri=3)
client.tx(msg) # where client is instance of RP1210Client
- This class will intelligently handle PDU1 vs PDU2, so DA is not always needed
- Priority will default to 6 if it is not specified
- Size will default to 8 if it is not specified
J1939Message Params
RP1210_ReadMessage_bytes
: bytes returned by RP1210_ReadMessagedata
: message data, usually 8 bytes (bytes)pgn
: parameter group number (int)da
: destination address (int)sa
: source address (int)pri
: message priority (defaults to 6) (int)size
: data size (defaults to 8) - 0xFF bytes will be appended to fill space (int)how
: how to send - 0 = RTS/CTS (default); 1 = BAM (int)
J1939Message Properties
msg
: the full contents of the RP1210 message, not including the 4-byte timestamp (bytes)pgn
: Parameter Group Number (int)da
: Destination Address (int)sa
: Source Address (int)pri
: Priority (int) - see NOTEdata
: Message Data (bytes)size
: Data Size (int)res
: Reserved bit (int)dp
: Data Page bit (int)how
: How To Send bit (int)timestamp
: 4-byte timestamp from RP1210_ReadMessage (int)
J1939Message Functions
pdu()
- returns PDU type (PDU 1 or PDU 2)pf()
- returns PDU Format byte as intps()
- returns PDU Specific byte as inttimestamp_bytes()
- returns timestamp as 4-byte string of bytes (for external formatting)isEcho()
- returns True if message is an echo of a message you sentisRequest()
- returns True if message is a J1939 Request, False if not.
J1939Message Notes
NOTE (from RP1210C 15.5):
In accordance with Section 5.2.1 of J 1939/21, the priority should be masked off by the VDA and set to zero. However VDA vendors can send back the actual message priority if they so desire. Because the priority may or may not be provided by the VDA, applications should function correctly regardless of the presence of the priority information.
NOTE: When PGN and other values like DA conflict, the most recently assigned value will take precedence. When ambiguous, this class will default to assigning the destination address to the PGN rather than from it.
J1939 Diagnostic Messages
Please see the J1939 Diagnostic Messages page.