Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    CAN message decoding - how is data returned?

    Scheduled Pinned Locked Moved Discussion
    3 Posts 2 Posters 49 Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • J Offline
      JSmith
      last edited by

      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 bit

      I 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

      1 Reply Last reply Reply Quote 0
      • J Offline
        johand
        last edited by

        I am pretty sure you can access the data by index:

        Frame = can.recv()
        MyValue = Frame.data[0] + Frame.data[1] * 0x100
        

        Johan

        1 Reply Last reply Reply Quote 0
        • J Offline
          JSmith
          last edited by

          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.

          1 Reply Last reply Reply Quote 0

          Hello! It looks like you're interested in this conversation, but you don't have an account yet.

          Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

          With your input, this post could be even better 💗

          Register Login
          • 1 / 1
          • First post
            Last post

          Pycom on Twitter

          Pycom