Author Topic: 12 Bit Analog to Modbus Coils  (Read 2402 times)

mariley2250

  • Newbie
  • *
  • Posts: 31
    • View Profile
12 Bit Analog to Modbus Coils
« on: January 10, 2016, 07:36:58 PM »
I am in the process of developing a HMI that reads and writes Modbus values to a Crevis field IO device. The analogs on this device come in as a 12 bit register on 12 Boolean bits. I am having trouble using a datasubscriber to get this information in and processed. Any thoughts??

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5262
    • View Profile
    • AdvancedHMI
Re: 12 Bit Analog to Modbus Coils
« Reply #1 on: January 10, 2016, 10:01:05 PM »
Returning as bit values really makes it awkward. You can brute force it with something like this:
Code: [Select]
    Private AnalogValue As Integer
    Private Sub DataSubscriber21_DataChanged_2(sender As Object, e As Drivers.Common.PlcComEventArgs) Handles DataSubscriber21.DataChanged
        If e.PlcAddress = "00001" Then
            AnalogValue = SetNewBitValue(AnalogValue, e.Values(0), 0)
        ElseIf e.PlcAddress = "00002" Then
            AnalogValue = SetNewBitValue(AnalogValue, e.Values(0), 1)
        ElseIf e.PlcAddress = "00003" Then
            AnalogValue = SetNewBitValue(AnalogValue, e.Values(0), 2)
        End If
    End Sub

    Private Function SetNewBitValue(ByVal value As Integer, ByVal bitValue As Boolean, ByVal bitNumber As Integer) As Integer
        Dim MaskValue As Integer = (1 << bitNumber)
        If bitValue Then
            Return (Value Or MaskValue)
        Else
            Return (Value And (65535 - MaskValue))
        End If
    End Function

In this example, 00001 is bit 0, 00002 is bit 1, 00003 is bit 2

Add a DataSubscriber2 to the form and in the PLCAddressValue list, add each bit address.

mariley2250

  • Newbie
  • *
  • Posts: 31
    • View Profile
Re: 12 Bit Analog to Modbus Coils
« Reply #2 on: January 11, 2016, 12:50:30 AM »
Hi Archie,

Thanks for the code..

It is close but basically what i need is the following.

When Bit 0 is true it adds 1 to the variable
When Bit 1 is true it adds 2 to the variable
When Bit 2 is true it adds 4 to the variable
When Bit 3 is true it adds 8 to the variable
When Bit 4 is true it adds 16 to the variable
When Bit 5 is true it adds 32 to the variable
When Bit 6 is true it adds 64 to the variable
When Bit 7 is true it adds 128 to the variable
When Bit 8 is true it adds 256 to the variable
When Bit 9 is true it adds 512 to the variable
When Bit 10 is true it adds 1024 to the variable
When Bit 11 is true it adds 2048 to the variable

total value of bits adds up to 4096 @ 100%

Hope this makes sense

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5262
    • View Profile
    • AdvancedHMI
Re: 12 Bit Analog to Modbus Coils
« Reply #3 on: January 11, 2016, 06:51:57 AM »
What is the address for bit 0?

mariley2250

  • Newbie
  • *
  • Posts: 31
    • View Profile
Re: 12 Bit Analog to Modbus Coils
« Reply #4 on: January 11, 2016, 06:53:11 AM »
00017

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5262
    • View Profile
    • AdvancedHMI
Re: 12 Bit Analog to Modbus Coils
« Reply #5 on: January 11, 2016, 07:07:51 AM »
- Add a Datasubcriber2 to the form
- In the Properties windows click the ellipsis (3 dots) next to PLCAddressValueItems
- A new window will pop up
- Click the Add button
- In the right pane, enter 00017 for PLCAddress
- Click the Add Button again
- In the right pane, enter 00018 for PLCAddress
- Click the Add Button again
- In the right pane, eneter 00019 for PLCAddress
- Repeat the above 2 steps until you enter 0028
- Double click the Data subscriber to get to the value changed event handler
- Enter this code:
Code: [Select]
        If e.PlcAddress = "00017" Then
            AnalogValue = SetNewBitValue(AnalogValue, e.Values(0), 0)
        ElseIf e.PlcAddress = "00018" Then
            AnalogValue = SetNewBitValue(AnalogValue, e.Values(0), 1)
        ElseIf e.PlcAddress = "00019" Then
            AnalogValue = SetNewBitValue(AnalogValue, e.Values(0), 2)
        ElseIf e.PlcAddress = "00020" Then
            AnalogValue = SetNewBitValue(AnalogValue, e.Values(0), 3)
        ElseIf e.PlcAddress = "00021" Then
            AnalogValue = SetNewBitValue(AnalogValue, e.Values(0), 4)
        ElseIf e.PlcAddress = "00022" Then
            AnalogValue = SetNewBitValue(AnalogValue, e.Values(0), 5)
        ElseIf e.PlcAddress = "00023" Then
            AnalogValue = SetNewBitValue(AnalogValue, e.Values(0), 6)
        ElseIf e.PlcAddress = "00024" Then
            AnalogValue = SetNewBitValue(AnalogValue, e.Values(0), 7)
        ElseIf e.PlcAddress = "00025" Then
            AnalogValue = SetNewBitValue(AnalogValue, e.Values(0), 8)
        ElseIf e.PlcAddress = "00026" Then
            AnalogValue = SetNewBitValue(AnalogValue, e.Values(0), 9)
        ElseIf e.PlcAddress = "00027" Then
            AnalogValue = SetNewBitValue(AnalogValue, e.Values(0), 10)
        ElseIf e.PlcAddress = "00028" Then
            AnalogValue = SetNewBitValue(AnalogValue, e.Values(0), 11)
        End If

- Just above the event handler "Private Sub DataSubscriber21_DataChanged........" enter this line of code:
Code: [Select]
    Private AnalogValue As Integer

Below the event handler just after "End Sub", enter this code:
Code: [Select]
    Private Function SetNewBitValue(ByVal value As Integer, ByVal bitValue As Boolean, ByVal bitNumber As Integer) As Integer
        Dim MaskValue As Integer = (1 << bitNumber)
        If bitValue Then
            Return (Value Or MaskValue)
        Else
            Return (Value And (65535 - MaskValue))
        End If
    End Function

mariley2250

  • Newbie
  • *
  • Posts: 31
    • View Profile
Re: 12 Bit Analog to Modbus Coils
« Reply #6 on: January 11, 2016, 03:24:05 PM »
thanks Archie

Works perfectly

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5262
    • View Profile
    • AdvancedHMI
Re: 12 Bit Analog to Modbus Coils
« Reply #7 on: January 11, 2016, 09:27:41 PM »
You could also shorten you code in the event handler like this:
Code: [Select]
        Dim bitNumber As Integer = CInt(e.PlcAddress) - 17
        AnalogValue = SetNewBitValue(AnalogValue, e.Values(0), bitNumber)

mariley2250

  • Newbie
  • *
  • Posts: 31
    • View Profile
Re: 12 Bit Analog to Modbus Coils
« Reply #8 on: January 21, 2016, 06:07:11 PM »
Hi Archie,

I am having more trouble with these datasubribers. what i want to do is increase the speed of the modbus reading from a Siemens S7-414. I am reading roughly 400 points from it and it seems to do one at a time and is very very slow. Is there a way that a datasubscriber will fix this. If so can you provide a sample code. I apoligize for my stupidity but i just cant get my head around this one.

Marcus

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5262
    • View Profile
    • AdvancedHMI
Re: 12 Bit Analog to Modbus Coils
« Reply #9 on: January 21, 2016, 06:22:09 PM »
Set PollRateOverride to 0. Also see this thread:

http://advancedhmi.com/forum/index.php?topic=1057.msg5537#msg5537