Author Topic: code help  (Read 1463 times)

MrPike

  • Sr. Member
  • ****
  • Posts: 297
    • View Profile
code help
« 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]

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5262
    • View Profile
    • AdvancedHMI
Re: code help
« Reply #1 on: January 17, 2016, 09:15:51 PM »
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))

MrPike

  • Sr. Member
  • ****
  • Posts: 297
    • View Profile
Re: code help
« Reply #2 on: January 18, 2016, 07:48:50 AM »
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

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5262
    • View Profile
    • AdvancedHMI
Re: code help
« Reply #3 on: January 18, 2016, 08:09:05 AM »
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?

rbelknap

  • Jr. Member
  • **
  • Posts: 68
    • View Profile
Re: code help
« Reply #4 on: January 18, 2016, 09:23:21 AM »
I've never seen a select case done like that.

I think it should be like this:

Code: [Select]

    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.

MrPike

  • Sr. Member
  • ****
  • Posts: 297
    • View Profile
Re: code help
« Reply #5 on: January 18, 2016, 02:30:19 PM »
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). 

MrPike

  • Sr. Member
  • ****
  • Posts: 297
    • View Profile
Re: code help
« Reply #6 on: January 18, 2016, 02:42:56 PM »
I found my error that rbelknap had eluded to.  I had to change e.PLCAddress(0) to just e.PLCAddress.  Thanks all