Dim MbusQuery(11) As Byte
Public MbusResponse As String
Dim MbusByteArray(255) As Byte
Public MbusRead As Boolean
Public MbusWrite As Boolean
Dim ModbusTimeOut As Integer
Dim ModbusWait As Boolean
Private Sub Command1_Click()
Dim StartTime
If (Winsock1.State <> sckClosed) Then
Winsock1.Close
End If
Winsock1.RemoteHost = Text1.Text
Winsock1.Connect
StartTime = Timer
Do While ((Timer < StartTime + 2) And (Winsock1.State <> 7))
DoEvents
Loop
If (Winsock1.State = 7) Then
Text5.Text = "Connected"
Text5.BackColor = &HFF00&
Else
Text5.Text = "Can't connect to " + Text1.Text
Text5.BackColor = &HFF
End If
End Sub
Private Sub Command2_Click()
If (Winsock1.State <> sckClosed) Then
Winsock1.Close
End If
Do While (Winsock1.State <> sckClosed)
DoEvents
Loop
Text5.Text = "Disconnected"
Text5.BackColor = &HFF
End Sub
Private Sub Command3_Click()
Dim StartLow As Byte
Dim StartHigh As Byte
Dim LengthLow As Byte
Dim LengthHigh As Byte
If (Winsock1.State = 7) Then
StartLow = Val(Text2.Text) Mod 256
StartHigh = Val(Text2.Text) \ 256
LengthLow = Val(Text3.Text) Mod 256
LengthHigh = Val(Text3.Text) \ 256
MbusQuery(0) = 0
MbusQuery(1) = 0
MbusQuery(2) = 0
MbusQuery(3) = 0
MbusQuery(4) = 0
MbusQuery(5) = 6
MbusQuery(6) = 1
MbusQuery(7) = 4
MbusQuery(8) = StartHigh
MbusQuery(9) = StartLow
MbusQuery(10) = LengthHigh
MbusQuery(11) = LengthLow
MbusRead = True
MbusWrite = False
'MbusQuery = Chr(0) + Chr(0) + Chr(0) + Chr(0) + Chr(0) + Chr(6) + Chr(1) + Chr(3) + Chr(StartHigh) + Chr(StartLow) + Chr(LengtHigh) + Chr(LengthLow)
Winsock1.SendData MbusQuery
ModbusWait = True
ModbusTimeOut = 0
Timer1.Enabled = True
Else
MsgBox ("Device not connected via TCP/IP")
End If
End Sub
Private Sub VScroll1_Change()
End Sub
Private Sub Label3_Change(index As Integer)
Label3(j).Refresh
End Sub
Private Sub Timer1_Timer()
ModbusTimeOut = ModbusTimeOut + 1
If ModbusTimeOut > 2 Then
ModbusWait = False
ModbusTimeOut = 0
Text5.Text = "Modbus Time Out"
Text5.BackColor = &HFF
Timer1.Enabled = False
End If
End Sub
Private Sub Winsock1_DataArrival(ByVal datalength As Long)
Dim b As Byte
Dim j As Byte
For i = 1 To datalength
Winsock1.GetData b
MbusByteArray(i) = b
Next
j = 0
If MbusRead Then
For i = 10 To MbusByteArray(9) + 9 Step 2
Label3(j).Caption = Str((MbusByteArray(i) * 256) + MbusByteArray(i + 1)) * 0.1
j = j + 1
Next i
Text5.Text = "Registers read"
Text5.BackColor = &HFF00&
For l = j To 30
Label3(l).Caption = "*****"
Next l
ModbusWait = False
ModbusTimeOut = 0
Timer1.Enabled = False
End If
If MbusWrite Then
If (MbusByteArray(8) = 16) And (MbusByteArray(12) = Val(Text3.Text)) Then
Text5.Text = "Registers written"
Text5.BackColor = &HFF00&
ModbusWait = False
ModbusTimeOut = 0
Timer1.Enabled = False
Else
Text5.Text = "Error writting registers"
Text5.BackColor = &HFF
End If
End If
End Sub