Author Topic: InvalidOperationException OnDataReceived ModbusBase  (Read 1635 times)

andrew_pj

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
InvalidOperationException OnDataReceived ModbusBase
« on: January 27, 2015, 11:41:46 PM »
Dear all,

I am using version 3.97d.
Basically, I have BasicLabel which requires to be hide and shown in certain condition.
I dragged Timer object into the form and put the code inside so it will continuously detect this condition.

Code: [Select]
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Try
            Dim temp As Integer = Integer.Parse(ModbusTCPCom1.Read(414940))
            If temp < 0 Then
                Me.FWD_Indication.Show()
                Me.AFT_Indication.Hide()
            ElseIf temp >= 0 Then
                Me.AFT_Indication.Show()
                Me.FWD_Indication.Hide()
            End If
        Catch ex As Exception

        End Try
    End Sub

However, it always returns InvalidOperationException (pls refer to attachment for details).
Is there any other solution besides using BasicLabel and put the above code using Timer?
Please advise, Thank you.

Best regards,
Andrew

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5270
    • View Profile
    • AdvancedHMI
Re: InvalidOperationException OnDataReceived ModbusBase
« Reply #1 on: January 28, 2015, 08:41:28 AM »
Your timer may be firing before the form is fully loaded. I would add a DataSubscriber, set PLCAddressValue to 414940, double click the DataSubscriber to get to the DataChanged event handler.

Add your code there and use e.values(0) in place of ModbusTCPCom1.Read(414940)

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5270
    • View Profile
    • AdvancedHMI
Re: InvalidOperationException OnDataReceived ModbusBase
« Reply #2 on: January 28, 2015, 08:44:46 AM »
Another option is to modify the code like this:


    Protected Overridable Sub OnDataReceived(ByVal e As MfgControl.AdvancedHMI.Drivers.Common.PlcComEventArgs)
        If m_SynchronizingObject IsNot Nothing Then
            Dim Parameters() As Object = {e}
            If DirectCast(m_SynchronizingObject, System.Windows.Forms.Control).IsHandleCreated Then
                m_SynchronizingObject.BeginInvoke(drsd, Parameters)
            End If
        Else
            DataReceivedSync(e)
        End If
    End Sub

andrew_pj

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
Re: InvalidOperationException OnDataReceived ModbusBase
« Reply #3 on: January 28, 2015, 11:04:49 PM »
Your timer may be firing before the form is fully loaded. I would add a DataSubscriber, set PLCAddressValue to 414940, double click the DataSubscriber to get to the DataChanged event handler.

Add your code there and use e.values(0) in place of ModbusTCPCom1.Read(414940)

I use DataSubscriber then it is okay.

In 3.97e release note, there is additional features:
DataSubscriber - Can subscribe to multiple values by putting comma separated values in PLCAddressValue

So, I have tried to put 2 address by using comma as separation.
Then, parse the PLCAddressValue to be stored in separate variable.

Code: [Select]
Private Sub DataSubscriber2_DataChanged_1(ByVal sender As System.Object, ByVal e As MfgControl.AdvancedHMI.Drivers.Common.PlcComEventArgs) Handles DataSubscriber2.DataChanged
        ' temp = 414940
        Dim temp As Integer = Integer.Parse(ModbusTCPCom1.Read(Me.DataSubscriber2.PLCAddressValue.Substring(0, 6)))

        ' temp2 = 414942
        Dim temp2 As Integer = Integer.Parse(ModbusTCPCom1.Read(Me.DataSubscriber2.PLCAddressValue.Substring(7, 6)))

End Sub

This works really well.
However, besides manually parsing PLCAddressValue, do you have special function to get those address (by index maybe)?
Please advise the trick, Thank you.


Best regards,
Andrew


Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5270
    • View Profile
    • AdvancedHMI
Re: InvalidOperationException OnDataReceived ModbusBase
« Reply #4 on: January 28, 2015, 11:15:47 PM »
In the DataChanged event, it will return the address and value in the PlcComEventArgs object (e). So you can do this to determine which value is returned:

if e.PlcAddress="414940" then
   Me.Text="The value for 414940 is " & e.values(0)
elseif e.PlcAddress="414942" then
.
.
end if

andrew_pj

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
Re: InvalidOperationException OnDataReceived ModbusBase
« Reply #5 on: January 28, 2015, 11:22:52 PM »
In the DataChanged event, it will return the address and value in the PlcComEventArgs object (e). So you can do this to determine which value is returned:

if e.PlcAddress="414940" then
   Me.Text="The value for 414940 is " & e.values(0)
elseif e.PlcAddress="414942" then
.
.
end if


Thankss, that is a lot better way to do.

Code: [Select]
Private Sub DataSubscriber2_DataChanged_1(ByVal sender As System.Object, ByVal e As MfgControl.AdvancedHMI.Drivers.Common.PlcComEventArgs) Handles DataSubscriber2.DataChanged
        ' temp = 414940
        Dim temp As Integer

        ' temp2 = 414942
        Dim temp2 As Integer

        If e.PlcAddress = "414940" Then
            temp = e.Values(0)
        ElseIf e.PlcAddress = "414942" Then
            temp2 = e.Values(0)
        End If

End Sub