AdvancedHMI Software
General Category => Support Questions => Topic started by: MrPike on January 17, 2016, 06:15:07 PM
-
Looking for some help to get the following code to work. I want to display labels with Dec values and hex as below, however any of the 4 registers will change only the text of the first labels. I have done this with IF THEN but wanted to use this SELECT CASE instruction. Thank you
[Dim address As String
Private Sub DataSubscriber21_DataChanged(sender As Object, e As Drivers.Common.PlcComEventArgs) Handles DataSubscriber21.DataChanged
Select Case (address) = e.PlcAddress(0)
Case address = "400001"
Integer1.Text = e.Values(0)
Hex1.Text = Conversion.Hex(e.Values(0))
Case address = "400002"
Integer2.Text = e.Values(0)
Hex2.Text = Conversion.Hex(e.Values(0))
Case address = "400003"
Integer3.Text = e.Values(0)
Hex3.Text = Conversion.Hex(e.Values(0))
Case address = "400004"
Integer4.Text = e.Values(0)
Hex4.Text = Conversion.Hex(e.Values(0))
End Select]
-
e.Values(0) is a string and Conversion.Hex does not accept a string, therefore an implicit conversion must be done by VB. The best thing is to do an explicit conversion in order to get more predictable results. For example:
Conversion.Hex(CInt(e.Values(0))
-
I implemented the code change with no change. The problem I am having is that the registers 400001-400004 will only affect the first labels text. A change in any of them will change just Integer1.text and hex1.text. The IF THEN code below works perfect for all 4 labels. Just trying to figure out how to do it with the SELECT CASE instruction just for learning purposes. Thank you for the help
If e.PlcAddress = "400001" Then
Integer1.Text = e.Values(0)
Hex1.Text = Conversion.Hex(e.Values(0))
ElseIf e.PlcAddress = "400002" Then
Integer2.Text = e.Values(0)
Hex2.Text = Conversion.Hex(e.Values(0))
ElseIf e.PlcAddress = "400003" Then
Integer3.Text = e.Values(0)
Hex3.Text = Conversion.Hex(e.Values(0))
ElseIf e.PlcAddress = "400004" Then
Integer4.Text = e.Values(0)
Hex4.Text = Conversion.Hex(e.Values(0))
End If
-
When you added the values to the DataSubscriber2, did you use one item of 40001 with 4 elements, or did you add 4 individual items, one for each address?
-
I've never seen a select case done like that.
I think it should be like this:
Private Sub DataSubscriber21_DataChanged(sender As Object, e As Drivers.Common.PlcComEventArgs) Handles DataSubscriber21.DataChanged
Select Case e.PlcAddress
Case "400001"
Integer1.Text = e.Values(0)
Hex1.Text = Conversion.Hex(e.Values(0))
Case "400002"
Integer2.Text = e.Values(0)
Hex2.Text = Conversion.Hex(e.Values(0))
Case "400003"
Integer3.Text = e.Values(0)
Hex3.Text = Conversion.Hex(e.Values(0))
Case "400004"
Integer4.Text = e.Values(0)
Hex4.Text = Conversion.Hex(e.Values(0))
End Select
Also, if you put in a break-point(F9) then you can see what addresses are coming through and step-through the code.
-
In my DataSub2 PLCAddressValueItems collection I added 4 individual items 400001,400002,400003,400004
The above code returns the same as my code. Debugging reveals that only the first CASE is executed(400001).
-
I found my error that rbelknap had eluded to. I had to change e.PLCAddress(0) to just e.PLCAddress. Thanks all