Page 1 of 1

Problem sending CAN messages.

Posted: Wed 12. Jan 2022, 08:58
by Tinny.D
Hello,
I'm making a python script which sends a CAN message through a PCAN-USB(IPEH-002021). The problem is when i want to send a float type data i receive the following error <unsupported operand type(s) for >>: 'float' and 'int'>

This is my code:

Code: Select all

connect1 = PCANBasic()

# Initialize connection
res = connect1.Initialize(PCAN_USBBUS1, 
PCAN_BAUD_500K, 
TPCANType(0),0,0)
        
msg = TPCANMsg()
msg.ID = 0xW8
msg.MSGTYPE = PCAN_MESSAGE_STANDARD   
msg.LEN = 8
        
msg.DATA[0] = (12.4>> 24) & 0xff
msg.DATA[1] = (12.4 >> 16) & 0xff
msg.DATA[2] = (12.4 >> 8) & 0xff
msg.DATA[3] = (12.4) & 0xff
        
msg.DATA[4] = (35.4 >> 24) & 0xff
msg.DATA[5] = (35.4 >> 16) & 0xff
msg.DATA[6] = (35.4 >> 8) & 0xff
msg.DATA[7] = (35.4) & 0xff


result = connect1.Write(PCAN_USBBUS1,msg)
if result != PCAN_ERROR_OK:
    # An error occurred, get a text describing the error and show it
    #
    result = connect1.GetErrorText(result,0x00)
    print(result)
else:
    print("VP_POSITION : Message sent successfully")
            
        
res = connect1.Uninitialize(PCAN_USBBUS1)

Is there anay way for it to accept float data and not just integer data ?
Thank you in advance.

Re: Problem sending CAN messages.

Posted: Wed 12. Jan 2022, 09:29
by PEAK-Support
A CAN 2.0a/b Message could handle up to 8 Data Bytes. What you put into this Data Bytes is up to you.
If you talking about a float value, you need to understand how your target(s) read/write a float value from the memory which store the Value.
Maybe you read this pages link and link and try to understand how a float is stored in computer memory. In the same way you have to fill the CAN Data Bytes.

In C it is very easy, you define a VAR as float and you know that it will be stored in 4 Bytes (32 Bit). Then you could use this 4 Bytes to fill the first 4 CAN Databytes to send a float inside the CAN Frame in Byte 0..3. This could be easy done by using a union.

How you setup this in Python is a Python question, maybe you better post this question inside a Python Forum.