Problem Sending CAN Message

This forum covers PCAN-Linux and Linux development issues concerning our products
Post Reply
adamw88
Posts: 2
Joined: Fri 13. May 2016, 20:26

Problem Sending CAN Message

Post by adamw88 » Fri 13. May 2016, 21:07

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!
Last edited by adamw88 on Mon 16. May 2016, 18:13, edited 1 time in total.

User avatar
PEAK-Support
Sales & Support
Sales & Support
Posts: 1646
Joined: Fri 10. Sep 2010, 19:34

Re: Problem Sending CAN Message

Post by PEAK-Support » Mon 16. May 2016, 13:52

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
--------------------------------
PEAK-System Technik
Technical Support Team
support[at]peak-system.com
-------------------------------

adamw88
Posts: 2
Joined: Fri 13. May 2016, 20:26

Re: Problem Sending CAN Message

Post by adamw88 » Mon 16. May 2016, 18:11

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

Post Reply