以下是modbus通讯的VB代码,哪位高手解释以下红色字体显示的那部分代码意思,谢谢!
Option Explicit
' Call Windows API (For winsock use)
Private Declare Function inet_addr Lib "wsock32.dll" (ByVal s As String) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
' Call Window API (For registry use, Find Serial Port list)
Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
Private Declare Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, lpcbValueName As Long, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Const REG_SZ = 1
Const HKEY_LOCAL_MACHINE = &H80000002
Const ERROR_SUCCESS = 0&
Const SYNCHRONIZE = &H100000
Const STANDARD_RIGHTS_READ = &H20000
Const KEY_QUERY_VALUE = &H1
Const KEY_CREATE_SUB_KEY = &H4
Const KEY_ENUMERATE_SUB_KEYS = &H8
Const KEY_NOTIFY = &H10
Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
Private Sub Command1_Click()
Dim strReq As String
Dim strRes As String
ActStatus.Text = ""
Dim ip As Long
ip = inet_addr(IPAddr.Text)
Dim strPort As String
strPort = ComPort.Text
Dim modbus_mode As Long
modbus_mode = MBMode.ListIndex '1:ASCII 2:Rtu
Dim comm_type As Long
comm_type = CommType.ListIndex '0:RS-232 , 1:Ethernet
Dim conn_num As Long
conn_num = CInt(Mid(strPort, 4, Len(strPort)))
Select Case CommType.ListIndex
Case Is = 0 'Modbus
If modbus_mode = 0 Then
Call OpenModbusSerial(conn_num, 9600, 7, AscB("E"), 1, modbus_mode + 1)
Else
Call OpenModbusSerial(conn_num, 9600, 8, AscB("N"), 1, modbus_mode + 1)
End If
Case Is = 1 'Modbus/TCP
conn_num = 0
Call OpenModbusTCPSocket(conn_num, ip)
End Select
If QMode.ListIndex = 0 Then 'Standard Modbus Request
Dim sendbuf(1024) As Byte
Dim recvbuf(1024) As Byte
Dim modbus_addr As Long
Dim modbus_func As Long
Dim modbus_addr_ret As Long
Dim modbus_func_ret As Long
Dim req As Long
Dim res As Long
Dim i As Long
Dim sendlen As Long
Dim ASC(1) As Byte
strReq = ReqData.Text
'at least slave address and function code
If Len(strReq) > 3 Then
modbus_addr = "&H" + Mid(strReq, 1, 2)
modbus_func = "&H" + Mid(strReq, 3, 2)
sendlen = 0
For i = 4 To Len(strReq) - 1 Step 2
sendbuf((i - 4) / 2) = "&H" + Mid(strReq, i + 1, 2)
sendlen = sendlen + 1
Next i
req = RequestData(comm_type, conn_num, modbus_addr, modbus_func, sendbuf(0), sendlen)
If req = -1 Then
ActStatus.Text = "Request Failed"
Else
res = ResponseData(comm_type, conn_num, modbus_addr_ret, modbus_func_ret, recvbuf(0))
If res > 0 Then
Dim strTmp As String
strTmp = Hex(modbus_addr_ret)
If Len(strTmp) < 2 Then
strTmp = "0" + strTmp
End If
strRes = strTmp
strTmp = Hex(modbus_func_ret)
If Len(strTmp) < 2 Then
strTmp = "0" + strTmp
End If
strRes = strRes + strTmp
Select Case modbus_func_ret
Case Is = 1, 2, 3, 4, 17, 23
strTmp = Hex(res)
If Len(strTmp) < 2 Then
strTmp = "0" + strTmp
End If
strRes = strRes + strTmp
End Select
For i = 0 To res - 1
Call HEX_to_ASCI(recvbuf(i), ASC)
strTmp = Chr(ASC(0)) & Chr(ASC(1))
strRes = strRes + strTmp
Next i
ActStatus.Text = "Request Done"
ResData.Text = strRes
Else
ActStatus.Text = "No Data Received"
ResData.Text = ""
End If 'res > 0
End If 'req = -1
End If 'Len(strReq) > 3
Else 'DMT Wrapped Function Code
楼主最近还看过