Author Topic: Access to specified bit in Holding Registers from Modbus Tcp  (Read 2708 times)

viper_lasser

  • Newbie
  • *
  • Posts: 9
    • View Profile
Access to specified bit in Holding Registers from Modbus Tcp
« on: September 12, 2014, 09:29:57 AM »
How can I access to specified bit from Modbus where 40001 is Holding Register ?
I would like to display on/off information about some device in BasicIndicator component


Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Access to specified bit in Holding Registers from Modbus Tcp
« Reply #1 on: September 12, 2014, 12:43:05 PM »
The driver can't parse the bits directly, but you can do this with a little code. Try this:

1) Add a DataSubscriber to the form
2) Set PLCAddressValue to 400001
3) Double click the DataSubscriber to get to the ValueChanged Event
4) Add  this code:
Code: [Select]
    Private Sub DataSubscriber1_DataChanged(sender As System.Object, e As MfgControl.AdvancedHMI.Drivers.Common.PlcComEventArgs) Handles DataSubscriber1.DataChanged
        Try
            Dim BitToUse As Integer
            BitToUse = 4
            '* If BitToUse is True, then SelectColor2 will be set to True
            BasicIndicator1.SelectColor2 = ((e.Values(0) And 2 ^ BitToUse) >0 )
        Catch ex As Exception
            MsgBox("error " & ex.Message)
        End Try
    End Sub

Do not put anything in any of the PLCAddress properties for the BasicIndicator

viper_lasser

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Access to specified bit in Holding Registers from Modbus Tcp
« Reply #2 on: October 09, 2014, 11:51:41 AM »
Thanks Arnie for advice with datasubscriber.

I was wondering is it possible to create datasubscriber instances with some litte code...
For example my program would read csv file with defined tag names and addresses for the tag, and also sometimes it would be defined specified bit in value register.
If tag will be definied with specified bit then one instanced of datasubscriber would be created.




Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Access to specified bit in Holding Registers from Modbus Tcp
« Reply #3 on: October 09, 2014, 12:08:07 PM »
The DataSubscriber was designed to give a simple drag and drop method of using subscriptions and encapsulate their complexity. In your case, you will probably want to skip the DataSubscriber and go straight to subscribing to the data. Here is an example of how it is done:
Code: [Select]
    '* Create a variable to hold the ID of the subscription for reference
    Private SubscriptionID As Integer
    '* Start the data subscription
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Try
            SubscriptionID = ModbusRTUCom1.Subscribe("40001", 1, 500, AddressOf Subscribe_DataReceived)
        Catch ex As Exception
            MsgBox("Failed to subscribe. " & ex.Message)
        End Try
    End Sub

    '* Handle the data received from a subscription
    Private Sub Subscribe_DataReceived(sender As Object, e As Drivers.Common.PlcComEventArgs)
        If e.PlcAddress = "40001" Then
            Me.Text = e.Values(0)
        End If
    End Sub

    '* Be sure to unsubscribe when no longer needed so driver will not continue to poll for this
    Private Sub MainForm_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        ModbusRTUCom1.Unsubscribe(SubscriptionID)
    End Sub