Page 1 of 1

Problem Sending CAN Message

Posted: Fri 13. May 2016, 21:07
by adamw88
EDIT: As Wilhelm pointed out, my problem was I was sending a 3-byte data packet instead of the expected 8-byte packet. This fixed my issue!

Hi everyone,

I finally got the Linux-API working on my RaspberryPi with the PCAN USB bus. I am able to receive messages from my device, but am having trouble sending messages in the proper format. From the API, the message object should have the specified types:

Code: Select all

class TPCANMsg (Structure):
    """
    Represents a PCAN message
    """
    _fields_ = [ ("ID",      c_uint),          # 11/29-bit message identifier
                 ("MSGTYPE", TPCANMessageType), # Type of the message
                 ("LEN",     c_ubyte),          # Data Length Code of the message (0..8)
                 ("DATA",    c_ubyte * 8) ]     # Data of the message (DATA[0]..DATA[7])
I am trying to get a message that works when I transmit it over PCAN Explorer to work with this API, but I think I am having problems with the conversion from Python data types to c-types. For example, if I wanted to send a message over my PCAN_USBBUS1 with the following parameters:

Arbitration ID: 0x350
Data: 0x00AAFF

Would I be able to format it like so?:

Code: Select all

import ctypes
from PCANBasic import   *
can = PCANBasic()
can.Initialize(PCAN_USBBUS1, PCAN_BAUD_500K) #Initialize CAN

ID = c_uint(0x350)
MSGTYPE = PCAN_MESSAGE_STANDARD #From the API
LEN = c_ubyte * 3 #Since data is 0x00AAFF
py_data = c_ubyte * 3 #Creates a list of length 3 ubytes
DATA = py_data(0x00, 0xAA, 0xFF)

MyMSG = TPCANMsg(ID, MSGTYPE, LEN, DATA)
can.Write(PCAN_USBBUS1, MyMSG) 
The above code returns '0' when sent, but does not do what I expect it do (turn on my device). I feel that I have a problem with my data types in C; could anyone provide some assistance? I'll be happy to answer any questions I need to to clarify my problem.

Thank you!

Re: Problem Sending CAN Message

Posted: Mon 16. May 2016, 13:52
by PEAK-Support
Why do you create 3 Byte for DLC (the data Lenght Code) The DLC is one Byte, and must be SET to 3 (Value) in your example. In Principial you have to use the Msg.Structure to fill with ID, DLC, Type and Data. Please take a closer look in our samples, which are part of the software package - you will find working python sample code

Re: Problem Sending CAN Message

Posted: Mon 16. May 2016, 18:11
by adamw88
Hi Wilhelm,

This was exactly my problem; as soon as I posted this I realized I was sending a 3 byte data packet instead of an 8 byte packet. As soon as I corrected this, my message was sent and it worked! Thank you for the reply :)

-Adam