Author Topic: Need help on ModbusTCPCom  (Read 1999 times)

halosome

  • Newbie
  • *
  • Posts: 31
    • View Profile
Need help on ModbusTCPCom
« on: September 24, 2019, 05:48:04 PM »
Hello Archie,

Is there a doc or tutorials to explain how to use the functions and events of ModbusTCPCom?

I am able to read and write holding registers but still need to understand deeper and more. For example, are there signals indicating the status of ModbusTCPCom.read() / ModbusTCPCom.write(), like processing, finished, timeout, failed? Do I need to wait for one read/write finished before I start another read/write?

Thanks!

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Need help on ModbusTCPCom
« Reply #1 on: September 24, 2019, 06:26:17 PM »
See if this helps any, this is common to all drivers:

https://advancedhmi.com/documentation/index.php?title=IComComponent

halosome

  • Newbie
  • *
  • Posts: 31
    • View Profile
Re: Need help on ModbusTCPCom
« Reply #2 on: September 24, 2019, 08:38:36 PM »
ase on that information, read/write should be always avoided.

I tried but I could not find the function beginreadmultiple/beginwritemultiple, they seems not included in ModBusTCPCOM.vb?

What would be the correct way to read or write 32 continuous integer? I tried beginread/beginwrite as below, but they don't seem to be the correct usage:

ModbusTCPCom.begineRead(40001.tostring, 32, arrayA as short())
FunctionHold200mS()
ModbusTCPCom.begineRead(40033.tostring, 32, arrayB as short())
FunctionHold200mS()
ModbusTCPCom.begineRead(40065.tostring, 32, arrayC as short())
FunctionHold200mS()

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Need help on ModbusTCPCom
« Reply #3 on: September 24, 2019, 08:57:59 PM »
ModbusTCPCom1.BeginRead("40001",32)

halosome

  • Newbie
  • *
  • Posts: 31
    • View Profile
Re: Need help on ModbusTCPCom
« Reply #4 on: September 24, 2019, 10:23:21 PM »
do you have a whole code sample of the usage of ModbusTCPCom1.BeginRead("40001",32) and BeginWrite?

Godra

  • Hero Member
  • *****
  • Posts: 1447
    • View Profile
« Last Edit: September 25, 2019, 01:54:30 AM by Godra »

halosome

  • Newbie
  • *
  • Posts: 31
    • View Profile
Re: Need help on ModbusTCPCom
« Reply #6 on: September 25, 2019, 08:30:38 PM »
Thanks a lot! The code works.


halosome

  • Newbie
  • *
  • Posts: 31
    • View Profile
Re: Need help on ModbusTCPCom
« Reply #7 on: September 25, 2019, 08:33:35 PM »
Hello Godra,

Just some ideas on your code:
1) Data_received might need a watchdog for timeout;
2) BeginRead might need a try catch for communication broken;
3) Select case e.PlcAddress might be better, case "40001" ...

Thanks again.

Godra

  • Hero Member
  • *****
  • Posts: 1447
    • View Profile
Re: Need help on ModbusTCPCom
« Reply #8 on: September 26, 2019, 03:15:23 PM »
Since you made suggestions then why not post your code as well, so other members can see it !?

halosome

  • Newbie
  • *
  • Posts: 31
    • View Profile
Re: Need help on ModbusTCPCom
« Reply #9 on: September 26, 2019, 06:52:05 PM »
I only wrote the select case but when I ran the program I realized the timeout and comm broken problems. Since you mentioned it, i just write a sample code for people who wants to have a look:


    Private Sub PLC_Read_Timer_Tick(sender As Object, e As EventArgs) Handles PLC_Read_Timer.Tick
         '100mS Timer

        PLC_Scan = IIf(PLC_Scan >= 9, 0, PLC_Scan + 1)

        Try

            Select Case PLC_IO_Scan
            Case 0
                 '
            Case 1
                ModbusTCPCom1.BeginRead("40001", 32)
            Case 2
                ModbusTCPCom1.BeginRead("43001", 32)
            Case 3
                ModbusTCPCom1.BeginRead("43101", 32)
            Case 4
                ModbusTCPCom1.BeginRead("40001", 32)
            Case 5
                ModbusTCPCom1.BeginRead("43001", 32)
            Case 6
                ModbusTCPCom1.BeginRead("43101", 32)
            Case 7
                ModbusTCPCom1.BeginRead("40001", 32)
            Case 8
                ModbusTCPCom1.BeginRead("43001", 32)
            Case 9
                ModbusTCPCom1.BeginRead("43101", 32)
            Case Else
                '
        End Select
        Catch ex As Exception
            ‘MessageBox.Show("Alarm: PLC Cannot be read!!")
        End Try

    End Sub

    Private Sub ModbusTCPCom1_DataReceived(sender As Object, e As Drivers.Common.PlcComEventArgs) Handles ModbusTCPCom1.DataReceived
        Dim ind As Integer = 0


Comm_WatchDog_Timer.Enabled = False

        Select Case e.PlcAddress
            Case "40001"
                ‘MessageBox.Show("PLC_Address = 40001”)
            Case "43101"
                For ind = 0 To 31
                    PC_Array(ind) = CInt(e.Values(ind))
                Next
            Case "43133"
                ‘MessageBox.Show("PLC_Address = 40001”)

            Case Else
                ‘MessageBox.Show("PLC_Error”)
        End Select

Comm_WatchDog_Timer.Enabled = True

    End Sub


    Private Sub Comm_WatchDog_Timer_Tick(sender As Object, e As EventArgs) Handles Comm_WatchDog_Timer.Tick
                      '5000mS Timer
                      ‘MessageBox.Show("Comm Timeout”)
    End Sub
« Last Edit: September 26, 2019, 07:02:28 PM by halosome »

Godra

  • Hero Member
  • *****
  • Posts: 1447
    • View Profile
Re: Need help on ModbusTCPCom
« Reply #10 on: September 26, 2019, 07:09:52 PM »
Cool.