Send CAN messages

CAN Software API for Windows®. REGARD! This API is out-dated. Please use PCAN-Basic for new software developments.
Post Reply
sjernna14
Posts: 2
Joined: Mon 4. Mar 2013, 10:46

Send CAN messages

Post by sjernna14 » Mon 4. Mar 2013, 12:20

Hello,

I have a PCAN-USB and a motor Maxon EPOS 24/1 and I am developing an interface on Visual Basic for the communication between them.

In addition to this I have a PCAN_USB.vb and my doubt is: How can I send a CAN message? Visual Basic creates an error because of TPCANMsg: I want send the DATA of CAN message as an array but is wrong and it's because I don't understand this part of the code:

<StructLayout(LayoutKind.Sequential, Pack:=1)> Public Structure TPCANMsg
Public ID As Integer ' 11/29 bit identifier
Public MSGTYPE As Byte ' Bits from MSGTYPE_*
Public LEN As Byte ' Data Length Code of the Msg (0..8)
<MarshalAs(UnmanagedType.ByValArray, sizeconst:=8)> _
Public DATA As Byte() ' Data 0 .. 7

End Structure


function --> Write(ByRef msg As TPCANMsg)

I've defined a new variable as TPCANMsg:

Dim message_send As TPCANMsg

message_send.ID = 601
message_send.MSGTYPE = 0
message_send.LEN = 8

here DATA --> ???????

Thank you very much.

Regards.

K.Wagner
Software Development
Software Development
Posts: 767
Joined: Wed 22. Sep 2010, 13:36

Re: Send CAN messages

Post by K.Wagner » Mon 4. Mar 2013, 12:53

Hello sjernna14,
sjernna14 wrote:Visual Basic creates an error because of TPCANMsg: I want send the DATA of CAN message as an array but is wrong
Arrays in Visual Basic .Net have to be initialized i.e. an array must be created. For example:

Code: Select all

message_send.DATA = New Byte(7) {}
If you need help by using the PCAN-Light dll within VB.Net, you can use the sample projects that you have received together with the PCAN-Light package. They are written in several languages (VB.Net is one of them) and they will teach you how to use the PCAN-Light libraries.
sjernna14 wrote:and it's because I don't understand this part of the code:
<StructLayout(LayoutKind.Sequential, Pack:=1)> Public Structure TPCANMsg
Public ID As Integer ' 11/29 bit identifier
Public MSGTYPE As Byte ' Bits from MSGTYPE_*
Public LEN As Byte ' Data Length Code of the Msg (0..8)
<MarshalAs(UnmanagedType.ByValArray, sizeconst:=8)> _
Public DATA As Byte() ' Data 0 .. 7
End Structure
If you don't know how to program using VB.Net please take a look for it in internet or in the microsoft online help.
Best regards,
Keneth

sjernna14
Posts: 2
Joined: Mon 4. Mar 2013, 10:46

Re: Send CAN messages

Post by sjernna14 » Mon 4. Mar 2013, 13:08

Thank you very much.

I am programming in VB 2008, and the part of the code that I don't understand is :

<MarshalAs(UnmanagedType.ByValArray, sizeconst:=8)> _
Public DATA As Byte() ' Data 0 .. 7

but in my preview post this line wasn't in bold style, sorry.

I'll defined --> message_send.DATA = New Byte(7) {}

on this way, but I thought that when I defined a new structure as TPCANMsg the part of DATA was defined too.

Thanks a lot again.

Regards.

K.Wagner
Software Development
Software Development
Posts: 767
Joined: Wed 22. Sep 2010, 13:36

Re: Send CAN messages

Post by K.Wagner » Mon 4. Mar 2013, 13:30

sjernna14 wrote:<MarshalAs(UnmanagedType.ByValArray, sizeconst:=8)> _
Public DATA As Byte() ' Data 0 .. 7
This is how arrays are marshaled from unmanaged code to managed code and vice-versa within the .Net Framework. Please understand that we cannot do support on this since this is a .Net related question. More info can be found in the microsoft online help.
Best regards,
Keneth

Post Reply