61
Feature Request / Using Modbus Register Bits in Controls
« on: November 15, 2014, 10:36:02 AM »
Hi Archie,
It would be really great if you could accommodate some of the Modbus community on this.
The ability to address a Modbus Register Bit where you need a Boolean in the controls would be nice.
It was actually fairly easy to do in version 3.70, but now in 3.80 the PLCAddress has moved inside the driver.
This means that it is not as easy to handle the PLCAddress to set up polling and then decode the bits of the data.
For Example on the BasicIndicator if the PLCAddressSelectColor2 could contain "40001.1" to use Bit 1.
This could be extended to all of the 16 bits with the highest being accessed using "40001.16".
I have been able to make this work by modifying the PolledDataReturned and adding a BitTest.
Unfortunately it fails when the bit number goes beyond one digit, so 10, 11, ..16 do not work.
Here is my code to show you want I was trying to do:
I know that my code is not the best, but you should be able to make any necessary changes easily.
It appears that if you changed the internals of the driver to accept Modbus addresses from 5-8 characters this would work.
I have attached a copy of the output showing the error when using the high bit.
Is it possible that you could change the driver to allow this syntax?
If that were the case, then it would be easy to start adding the additional code to handle this one control at a time.
The code above actually works fine for bits 1 through 9 on the BasicIndicator and seems to prove the concept.
Also, I attempted to use Hex values such as "40001.A", but this caused errors as well.
I have attached a screen capture showing this.
Thanks for all of your work on this project. It is always impressive to me to see it grow.
It would be really great if you could accommodate some of the Modbus community on this.
The ability to address a Modbus Register Bit where you need a Boolean in the controls would be nice.
It was actually fairly easy to do in version 3.70, but now in 3.80 the PLCAddress has moved inside the driver.
This means that it is not as easy to handle the PLCAddress to set up polling and then decode the bits of the data.
For Example on the BasicIndicator if the PLCAddressSelectColor2 could contain "40001.1" to use Bit 1.
This could be extended to all of the 16 bits with the highest being accessed using "40001.16".
I have been able to make this work by modifying the PolledDataReturned and adding a BitTest.
Unfortunately it fails when the bit number goes beyond one digit, so 10, 11, ..16 do not work.
Here is my code to show you want I was trying to do:
Code: [Select]
Private Sub PolledDataReturned(ByVal sender As Object, ByVal e As SubscriptionHandlerEventArgs)
If e.PLCComEventArgs.ErrorId = 0 Then
Try
Dim TempValue As Boolean
'* Write the value to the property that came from the end of the PLCAddress... property name
If e.SubscriptionDetail.PLCAddress.IndexOf(".") = 5 And _
Val(Mid(e.SubscriptionDetail.PLCAddress, 6)) > 0 And _
sender.ToString.ToUpper.IndexOf("MODBUS") >= 0 Then
TempValue = BitTest(e.PLCComEventArgs.Values(0), _
Val("&H" & Mid(e.PLCComEventArgs.PlcAddress, 7)))
Me.GetType().GetProperty(e.SubscriptionDetail.PropertyNameToSet). _
SetValue(Me, Convert.ChangeType(TempValue, _
Me.GetType().GetProperty(e.SubscriptionDetail.PropertyNameToSet).PropertyType), Nothing)
Else
Me.GetType().GetProperty(e.SubscriptionDetail.PropertyNameToSet). _
SetValue(Me, Convert.ChangeType(e.PLCComEventArgs.Values(0), _
Me.GetType().GetProperty(e.SubscriptionDetail.PropertyNameToSet).PropertyType), _
Nothing)
End If
Catch ex As Exception
DisplayError("INVALID VALUE RETURNED!" & e.PLCComEventArgs.Values(0))
End Try
Else
DisplayError("Com Error. " & e.PLCComEventArgs.ErrorMessage)
End If
End Sub
Private Function BitTest(Number As Long, bit As Integer) As Boolean
If bit = 16 Then
BitTest = ((Number < 0) * -1.0) ' Bit 16 (High Bit)
Else
BitTest = ((Number And 2 ^ (bit - 1)) <> 0) * -1
End If
End Function
I know that my code is not the best, but you should be able to make any necessary changes easily.
It appears that if you changed the internals of the driver to accept Modbus addresses from 5-8 characters this would work.
I have attached a copy of the output showing the error when using the high bit.
Is it possible that you could change the driver to allow this syntax?
If that were the case, then it would be easy to start adding the additional code to handle this one control at a time.
The code above actually works fine for bits 1 through 9 on the BasicIndicator and seems to prove the concept.
Also, I attempted to use Hex values such as "40001.A", but this caused errors as well.
I have attached a screen capture showing this.
Thanks for all of your work on this project. It is always impressive to me to see it grow.