Click here to Skip to main content
15,940,550 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi all,
i'm tring to create a modbus rtu master program... i found a lot of code but in C#. when i try to convert it in visual basic (vb.net 2010) it doesn't work... the PLC doesn't answer.
i think that there is a problem with the serialport.write(<byte>, <int>, <int>) function. someone can help me?

follow the code

VB
Public Function SendFc3(ByVal address As Byte, ByVal start As UShort, ByVal registers As UShort, ByRef values As Short())

        Dim flag As Boolean = False
        'Ensure port is open:
        If (Sp.IsOpen) Then

            'Clear in/out buffers:
            Sp.DiscardOutBuffer()
            Sp.DiscardInBuffer()
            'Function 3 request is always 8 bytes:
            Dim message As Byte() = New Byte(7) {}
            'Function 3 response buffer:
            Dim response As Byte() = New Byte((5 + 2 * registers) - 1) {}
            'Build outgoing modbus message
            BuildMessage(address, CByte(3), start, registers, message)
            'Send modbus message to Serial Port:
            Try

                sp.Write(message, 0, message.Length)
                'Evaluate message:
                If (GetResponse(response, sp, values)) Then
                    flag = True
                End If

            Catch err As Exception
                modbusStatus = "Error in read event: " + err.Message
                flag = False
            End Try
        Else

            modbusStatus = "Serial port not open"
            flag = False
        End If
        Return flag

    End Function


VB
Public Sub BuildMessage(ByVal address As Byte, ByVal type As Byte, ByVal start As UShort, ByVal registers As UShort, ByRef message As Byte())

       'Array to receive CRC bytes:
       Dim CRC As Byte() = New Byte(2) {}

       message(0) = Byte.Parse(address)
       message(1) = Byte.Parse(type)
       message(2) = Byte.Parse(start >> 8)
       message(3) = Byte.Parse(start)
       message(4) = Byte.Parse(registers >> 8)
       message(5) = Byte.Parse(registers)

       GetCRC(message, CRC)
       message(message.Length - 2) = Byte.Parse(84) 'CRC(0)
       message(message.Length - 1) = Byte.Parse(10) 'CRC(1)

   End Sub


VB
Public Function GetResponse(ByRef response As Byte(), ByRef spModbus As SerialPort, ByRef values As Short())

        Dim i As Integer = 0
        'Thread.Sleep(300)
        While (spModbus.BytesToRead <> 0)
            response(i) = Byte.Parse(spModbus.ReadByte())
            'Thread.Sleep(200)
            i += 1
            'Application.DoEvents()
        End While

        'Verifico il messaggio
        If (CheckResponse(response)) Then

            'Ritorno il valore dei registri richiesti
            For i = 0 To ((response.Length - 5) / 2) - 1

                response(i) = response(2 * i + 3)
                response(i) <<= 8
                response(i) += response(2 * i + 4)
                values(i) = response(2 * i + 3)
                values(i) <<= 8
                values(i) += response(2 * i + 4)

            Next

            Return True

        Else

            Return False

        End If

    End Function



Thanks
Posted
Comments
Member 11579366 10-Apr-15 10:03am    
are you still having the same problem with Modbus rtu?

There's nothing wrong with the serial port class between C# and VB.NET. It's the exact same code you're running no matter which language you chose.

The problem is most likely conversion problems in your BuildMessage and GetResponse code, or something those methods are calling, though I have no idea what it is because I don't know what ModBus or your PLC requires.
 
Share this answer
 
Given the fact you say the problematic function is serialport.write(xxx), I understand your Modbus PLC is not working with Modbus TCP, so you can be under 232 or 485...

Are you sure that the Modbus PLC you are trying to communicate to is configured to use 232?

If not you will need an adaptor from 232 to 485 in your PC.

Apart of that, it would be a good idea to get a MODBUS sniffer to attach it to your network and see what is happening with the packages that are sent/received...

Take a look at those links:
http://qmodbus.sourceforge.net/[^]

http://www.aggsoft.com/serial-port-monitor/plugins.htm[^]

http://www.windmill.co.uk/serial.html[^]

I've searched "Modbus sniffer" in google to find them.

PS: keep in mind that Modbus (as almost all the fieldbusses out there) require that the addresses are well positioned... double check them.

The typical problems I encountered working with Modbus have been:

1. bad addresses.
2. resistors wrong placed.

Good luck!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900