AdvancedHMI Software
General Category => Support Questions => Topic started by: mariley2250 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??
-
Returning as bit values really makes it awkward. You can brute force it with something like this:
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.
-
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
-
What is the address for bit 0?
-
00017
-
- 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:
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:
Private AnalogValue As Integer
Below the event handler just after "End Sub", enter this code:
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
-
thanks Archie
Works perfectly
-
You could also shorten you code in the event handler like this:
Dim bitNumber As Integer = CInt(e.PlcAddress) - 17
AnalogValue = SetNewBitValue(AnalogValue, e.Values(0), bitNumber)
-
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
-
Set PollRateOverride to 0. Also see this thread:
http://advancedhmi.com/forum/index.php?topic=1057.msg5537#msg5537