Click here to Skip to main content
11,637,176 members (68,696 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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(, , ) function. someone can help me?

follow the code

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

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

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 9-Aug-12 21:40pm
Comments
Member 11579366 at 10-Apr-15 10:03am
   
are you still having the same problem with Modbus rtu?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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!
  Permalink  

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

  Print Answers RSS
0 stibee 313
1 jyo.net 295
2 OriginalGriff 295
3 himanshu agarwal 184
4 Sergey Alexandrovich Kryukov 154
0 OriginalGriff 9,031
1 Sergey Alexandrovich Kryukov 8,513
2 Mika Wendelius 6,922
3 F-ES Sitecore 2,388
4 Suvendu Shekhar Giri 2,320


Advertise | Privacy | Mobile
Web03 | 2.8.150728.1 | Last Updated 10 Aug 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100