CAN message decoding - how is data returned?
-
Im trying to develop my code to interface with a SN65HVD230 CAN module. My device is a WiPy 3.0 with expansion board, and im using the callback to respond to messages as they come-in.
I have read the CAN documentation: https://docs.pycom.io/firmwareapi/pycom/machine/can/ but I would like to be able to extract the engineering values from the returned messages.
Im using a dictionary for my PGN attributes (engineering value,data bits, offset, scaling factor etc for the SPN's in the msg).
Note: im working with extended identifier msgs intel byte order, unsigned integer 32 bitI struggling with knowing how to extract the data, partly because im not sure how its is returned.
The documentation has a very basic example showing:
can.recv()
(id=0x012, data=b'123', rtr=False, extended=False)It looks as if the data sent for this msg was: can.send(id=12, data=bytes([1, 2, 3.............
Is the data returned not in a sort of byte array?
Im imaging I will need to: split out my relevant bytes, convert to integers, then sum in the correct order, then scale and apply offset.
Also, im new to Python, so any hints or tips for woring with the returned data would be appreciated. Many thanks Jake
-
To help others with this topic, the way I have extracted the engineering values is as per the below class method:
def extract_value(self,data): bit_list = bin(int.from_bytes(data,"little"))[2:] packed_bit_str = zfill(bit_list,64) spn_relevant_str = packed_bit_str[(-1*(self.start_bit+self.msg_bits)):(-1*(self.start_bit))] self.engineering_value = ((int(spn_relevant_str, 2)) * self.scaling_factor) + self.offset
I instantiate the class for each SPN, and for each PGN/msg the method is ran for all SPN's where the CAN ID matches the PGN ID.
The reason for working in bits as opposed to bytes is that some messages dont take up a full byte or share bytes with other messages.
I am new to Python and am in no way an experienced programmer, but though I would share my method since this didnt seem to be a commonly discussed topic.
-
I am pretty sure you can access the data by index:
Frame = can.recv() MyValue = Frame.data[0] + Frame.data[1] * 0x100
Johan