I'm trying to open a program which is originally written in vb6.0, in
When i try to run the program an error occurs. My guess is that the
problem lies in the fact that the type of adressing the memory is
changed. Therefor the translation to vb.net of the declaration of the
variables isn't correct.
perhaps its best that I will explain my program a liltle bit.
I am supposed to write a vb.net 2005 application that allows
communication between a PC (master) and a plc (slave) true the modbus
protocol with a rs232 cable.
The PC has to be able to read- write data to the plc and show it in the
program. Since the read and write error are the same I will explain the
So the basic idea is: From my main form ( where i can enter the mode
(read or write), the starting adres, number of registers i want to
read) a module is being called up (i send the mode, number of register,
starting register and such also to the module).
In this module a DLL is being called up (and again the mode, number of
register, starting register and such is send to the dll aswell) the dll
is in the system32 directory. The dll will then use the data from the
starting register, number of register to form a good modbus protocol
code to enter the module with the appropriat declarations:
Private Sub ReadShort_Click(ByVal eventSender As System.Object, ByVal
eventArgs As System.EventArgs) Handles ReadShort.Click
Dim BusState As Integer ' Een aantal shorts van de HPC lezen
Dim handle, j As Integer
Dim Ready As Boolean
Dim Address As TBusAddress
Dim Data As TShortData
MBRead( handle, j, 1,
This will call up the module and go to the function MBread
Declare Function MBRead Lib "Modbus" Alias "VBMBReadRequest" (ByRef
handle As Integer, ByVal UserData As Integer, ByVal NodeNr As Short,
ByVal VarType As Short, ByVal StartAddress As Short, ByVal Count As
Short) As Short
This function forms the request that is send to the PLC, so far so
Now the plc send a message back (which he actually sends, so that aint
the problem) which I will catch with the function
Ready = MBGetShorts(handle, j, BusState, Address, Data) <= is in the
Again the module is being called up.
Declare Function MBGetShorts Lib "Modbus" Alias "VBMBGetShortResults"
(ByVal MBHandle As Long, ByRef UserData As Long, ByRef BusState As
Integer, ByRef Address As TBusAddress, ByRef Data As TShortData) As
The function MBgetShorts catches the response from the plc and puts the
register data in the DATA aray. But from here it goes bad.
At the line "Ready = MBGetShorts(handle, j, BusState, Address, Data)"
the program stops and shows the error.
error may be a bug in the CLR or in the unsafe or non-verifiable
portions of user code. Common sources of this bug include user
marshaling errors for COM-interop or PInvoke, which may corrupt the
The program works correct in VB6.0, you fill in the correct registers,
starting adres and mode and then the module and DLL form the request
which is send to the PLC, the plc sends a message back and the DLL and
module collect the DATA from the registers and shows it on the computer
Someone told me that it might get fixed if i used internal pointers.
Anyone knows if this will solve my proble