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.
Send CAN messages
Re: Send CAN messages
Hello sjernna14,
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.
Arrays in Visual Basic .Net have to be initialized i.e. an array must be created. For example:sjernna14 wrote:Visual Basic creates an error because of TPCANMsg: I want send the DATA of CAN message as an array but is wrong
Code: Select all
message_send.DATA = New Byte(7) {}
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.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
Best regards,
Keneth
Keneth
Re: Send CAN messages
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.
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.
Re: Send CAN messages
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.sjernna14 wrote:<MarshalAs(UnmanagedType.ByValArray, sizeconst:=8)> _
Public DATA As Byte() ' Data 0 .. 7
Best regards,
Keneth
Keneth