Author Topic: Modbus TCP- Toggle Multiple memory bits with one button  (Read 1597 times)

Andrew.Lenzo

  • Newbie
  • *
  • Posts: 6
    • View Profile
Modbus TCP- Toggle Multiple memory bits with one button
« on: April 29, 2019, 11:44:17 AM »
I have an application where I am controlling identical "banks" of LED signals over a long distance. Each "bank" has 5 LED signals controlled by a dedicated PLC. All PLCs have identical programming but a unique IP address. Currently, I have modbus TCP connections to each PLC (total of 9 PLCs) and individual basic buttons for each signal.

I am wondering if there is a way to send the same command to multiple modbus TCP connections with a single button. For example, if I would like to turn on signal 1 (memory bit 00001) on every PLC, I need to have 9 basic buttons, 1 for each modbus connection. Is there a way to toggle 00001 on multiple PLCs with one button. For example, toggle 00001 on modbus connection 1, 2, 3, 4, etc.?

Sorry for the long winded question. This is not my area of expertise. If more info or examples would be helpful, please let me know.

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5324
    • View Profile
    • AdvancedHMI
Re: Modbus TCP- Toggle Multiple memory bits with one button
« Reply #1 on: April 29, 2019, 12:03:25 PM »
You can do this by directly accessing the driver via code. For instance:

- From the Toolbox, add a Button from the All Windows tool box group to the form
- Double click the button to get back to the code
- Add code similar to this:
Code: [Select]
ModbusTCPCom1.Write("0001",1)
ModbusTCPCom2.Write("0001",1)
ModbusTCPCom3.Write("0001",1)

Andrew.Lenzo

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Modbus TCP- Toggle Multiple memory bits with one button
« Reply #2 on: April 29, 2019, 12:13:20 PM »
Thanks for the reply! A few follow up questions:

1. I need to put this code in the private sub "Basicbutton1_Click" correct?
2. Do i need to have any additional code to allow the values to toggle between 0 and 1 or is it sufficient to select toggle as the output type in the button properties?

Thanks again in advance!

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5324
    • View Profile
    • AdvancedHMI
Re: Modbus TCP- Toggle Multiple memory bits with one button
« Reply #3 on: April 29, 2019, 05:34:52 PM »
If you need to toggle the bit like a momentary button, then you will need to do a mouse down and mouse up event handler:
Code: [Select]
    Private Sub Button1_MouseDown(sender As Object, e As MouseEventArgs) Handles Button1.MouseDown
        ModbusTCPCom1.Write("0001", 1)
        ModbusTCPCom2.Write("0001", 1)
        ModbusTCPCom3.Write("0001", 1)
    End Sub

    Private Sub Button1_MouseUp(sender As Object, e As MouseEventArgs) Handles Button1.MouseUp
        ModbusTCPCom1.Write("0001", 0)
        ModbusTCPCom2.Write("0001", 0)
        ModbusTCPCom3.Write("0001", 0)
    End Sub

Godra

  • Hero Member
  • *****
  • Posts: 1449
    • View Profile
Re: Modbus TCP- Toggle Multiple memory bits with one button
« Reply #4 on: April 29, 2019, 09:23:48 PM »
Andrew.Lenzo,

read Archie's posts carefully since he refers to the Button control and not the BasicButton control.

Andrew.Lenzo

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Modbus TCP- Toggle Multiple memory bits with one button
« Reply #5 on: April 30, 2019, 01:34:21 PM »
Thank you! This worked like a charm. Also, I was able to toggle using a single Button by setting a state variable equal to ModbusTCPComX.Read("00001"). They i used conditional statements to evaluate which value to write. (I know the additional state variables are unnecessary. This is a WIP!)


Code: [Select]
     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim state1 As Boolean
        Dim state2 As Boolean
        Dim state3 As Boolean
        Dim state4 As Boolean
        Dim state5 As Boolean
        state1 = ModbusTCPCom1.Read("00001")
        state2 = ModbusTCPCom1.Read("00003")
        state3 = ModbusTCPCom1.Read("00005")
        state4 = ModbusTCPCom1.Read("00007")
        state5 = ModbusTCPCom1.Read("00009")

        If state1 = 0 Then
            ModbusTCPCom1.Write("00001", 1)
            ModbusTCPCom1.Write("00003", 1)
            ModbusTCPCom1.Write("00005", 1)
            ModbusTCPCom1.Write("00007", 1)
            ModbusTCPCom1.Write("00009", 1)
        Else
            ModbusTCPCom1.Write("00001", 0)
            ModbusTCPCom1.Write("00003", 0)
            ModbusTCPCom1.Write("00005", 0)
            ModbusTCPCom1.Write("00007", 0)
            ModbusTCPCom1.Write("00009", 0)
        End If

    End Sub