The free CAN Software API (Application Programming Interface) for Windows®
-
yesweCAN
- Posts: 14
- Joined: Tue 25. Jul 2023, 14:34
Post
by yesweCAN » Tue 22. Aug 2023, 12:02
Hi,
I'm using the PCAN-Basic lib in a c# application.
I need to send a NMT package with 2 bytes in the payload.
I configure the message like this
Code: Select all
void Send_NMT()
{
TPCANMsg CANMsg = new TPCANMsg();
CANMsg.DATA = new byte[2];
CANMsg.ID = 0x00;
CANMsg.LEN = 2;
CANMsg.MSGTYPE = TPCANMessageType.PCAN_MESSAGE_STANDARD;
CANMsg.DATA[0] = 0x01;
CANMsg.DATA[1] = 0X00;
SendMsgToSingleID(CANMsg);
}
private TPCANStatus SendMsgToSingleID(TPCANMsg CANMsg)
{
return PCANBasic.Write(m_PcanHandle, ref CANMsg);
}
The compiler send me this error on the write calling
Type could not be mashaled because the length of an embedded array instance does not match the declared length in the layout
Thanks for you help
-
K.Wagner
- Software Development

- Posts: 1080
- Joined: Wed 22. Sep 2010, 13:36
Post
by K.Wagner » Tue 22. Aug 2023, 12:20
Hello,
the data array of a CAN structure is 8 byte in length. You are creating a two bytes array, which doesn't fit the structure declaration:

- TPCANMsg structure declaration for C#
- TPCANmsg.PNG (17.48 KiB) Viewed 2712 times
_
Please change the following line of your code:
to be like this:
Best regards,
Keneth
-
yesweCAN
- Posts: 14
- Joined: Tue 25. Jul 2023, 14:34
Post
by yesweCAN » Tue 22. Aug 2023, 12:22
Hi,
Ok,
The size of the array in always 8 but the Len define the number of byte to send.
it works
Thanks
-
K.Wagner
- Software Development

- Posts: 1080
- Joined: Wed 22. Sep 2010, 13:36
Post
by K.Wagner » Tue 22. Aug 2023, 12:46
Hello,
yesweCAN wrote: ↑Tue 22. Aug 2023, 12:22
So it is impossible to send a packet of 2 bytes ?
of course it's possible. I already told you what you need to change in my last post. Why not just try it? you will see that two bytes will be sent.
Background:
As wrote, the
data array of a CAN structure is 8 bytes in length, in other words, the data type of the field DATA is an array of length 8. The actual amount of CAN data to be transmitted is configured using the field LEN, as you already do in your code.
Please check the sample project for better understand.
Best regards,
Keneth