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.
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.
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