Author Topic: IndexOutOfArrayException on Modbus Bit Address  (Read 2471 times)

andrew_pj

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
IndexOutOfArrayException on Modbus Bit Address
« on: December 11, 2014, 04:32:38 AM »
I am basically using Modbus bit address feature to implement program for controlling valve open and close.
One case: To open using 40001.0 and To close using 40001.2, and the signal is "pulse".
So I created one function which simply reads the current value of addr (40001) and manipulate it with value of the bit.

Code: [Select]
Private Function Modify(ByVal addr As String, ByVal value As Integer)
        'Try
        Dim tempStr As String = MainForm.ModbusTcpCom1.Read(addr)
        Dim currValue As Integer = CInt(tempStr) + value
        tempStr = CStr(currValue)
        MainForm.ModbusTcpCom1.Write(addr, tempStr)
        'Catch ex As Exception
        'End Try
        Return 0
    End Function

Then, I create pop-up window that simply consists of 2 buttons, OPEN and CLOSE.
As mentioned, it is a pulse signal, so when user release/up the button, value will be reset.

Code: [Select]
Private Sub Button2_Down(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OPEN_BTN.MouseDown
        If MainForm.ValveNo = 1 Then
            Modify(40001, 1)
        End If
    End Sub

    Private Sub Button2_Up(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OPEN_BTN.MouseUp
        If MainForm.ValveNo = 1 Then
            Modify(40001, -1)
        End If
    End Sub

    Private Sub Button3_Down(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CLOSE_BTN.MouseDown
        If MainForm.ValveNo = 1 Then
            Modify(40001, 4)
        End If
    End Sub

    Private Sub Button3_Up(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CLOSE_BTN.MouseUp
        If MainForm.ValveNo = 1 Then
            Modify(40001, -4)
        End If
    End Sub

Functionally it works fine.
Then, after doing some arbitrary test, by pressing the button many times OPEN, CLOSE, OPEN, CLOSE, ...
At certain point, the program stops and indicates IndexOutOfArrayException.

Please help to advise solution regarding this matter.
Thank you.


Best regards,
Andrew

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: IndexOutOfArrayException on Modbus Bit Address
« Reply #1 on: December 11, 2014, 06:22:51 AM »
Is this with version 3.91?

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: IndexOutOfArrayException on Modbus Bit Address
« Reply #2 on: December 11, 2014, 06:31:30 AM »
I found the problem. Modify the code in ModbusBase.vb at line 114 like this:


    Public Function Read(ByVal startAddress As String, ByVal numberOfElements As Integer) As String() Implements IComComponent.Read
        Dim TransactionID As Integer = BeginRead(startAddress, numberOfElements)

        If WaitForResponse(CUShort(TransactionID)) = 0 Then
            Dim TNSByte As Integer = (TransactionID And 255)
            If Responses(TNSByte) IsNot Nothing AndAlso Responses(TNSByte).ErrorId = 0 Then
                Dim tmp(Responses(TNSByte).Values.Count - 1) As String
                For i As Integer = 0 To tmp.Length - 1
                    tmp(i) = Responses(TNSByte).Values(i)
                Next
                Return tmp
            Else
                Throw New MfgControl.AdvancedHMI.Drivers.Common.PLCDriverException(MfgControl.AdvancedHMI.Drivers.ModbusUtilities.DecodeMessage(Responses(TNSByte).ErrorId))
            End If
        Else
            Throw New MfgControl.AdvancedHMI.Drivers.Common.PLCDriverException("No Reponse from PLC. Ensure driver settings are correct.")
        End If
    End Function

andrew_pj

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
Re: IndexOutOfArrayException on Modbus Bit Address
« Reply #3 on: December 11, 2014, 08:01:35 PM »
Sry for not mentioning, yes this is version 3.91.
With that code modification, now it works fine all the time.
Thanks Archie.


Best regards,
Andrew

andrew_pj

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
Re: IndexOutOfArrayException on Modbus Bit Address
« Reply #4 on: December 11, 2014, 09:59:43 PM »
Download and tested this function in version 3.92, and different error comes out "InvalidOperationException"
Program stopped at Line 183 on ModbusTCPCom.vb
Please advise regarding this, version 3.91 doesn't have this issue though.
Thank you.


Best regards,
Andrew

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: IndexOutOfArrayException on Modbus Bit Address
« Reply #5 on: December 12, 2014, 09:10:02 AM »
I posted version 3.93. See where that version stands

andrew_pj

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
Re: IndexOutOfArrayException on Modbus Bit Address
« Reply #6 on: December 14, 2014, 11:07:23 PM »
Thanks Archie, version 3.93 fixed this problem.

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: IndexOutOfArrayException on Modbus Bit Address
« Reply #7 on: December 17, 2014, 09:14:04 AM »
Version 3.94 has been posted that now uses thread safe collections and should avoid the InvalidOperationException