Example_vb.frm
资源名称:test.rar [点击查看]
上传用户:wuxg88
上传日期:2022-05-28
资源大小:814k
文件大小:15k
源码类别:
通讯编程
开发平台:
C++ Builder
- VERSION 5.00
- Begin VB.Form Form1
- Caption = "Example"
- ClientHeight = 5865
- ClientLeft = 60
- ClientTop = 450
- ClientWidth = 7050
- LinkTopic = "Form1"
- ScaleHeight = 5865
- ScaleWidth = 7050
- StartUpPosition = 3 'Windows Default
- Begin VB.CommandButton Command2
- Caption = "Cancel"
- Height = 375
- Left = 5640
- TabIndex = 11
- Top = 840
- Width = 1215
- End
- Begin VB.CommandButton Command1
- Caption = "Send"
- Height = 375
- Left = 5640
- TabIndex = 10
- Top = 360
- Width = 1215
- End
- Begin VB.Frame Frame1
- Caption = "Modbus Function Test"
- Height = 5655
- Left = 120
- TabIndex = 12
- Top = 120
- Width = 5175
- Begin VB.Frame Frame3
- Caption = "Modbus Response"
- Height = 1695
- Left = 120
- TabIndex = 21
- Top = 3840
- Width = 4935
- Begin VB.TextBox data_r
- Height = 495
- Left = 2040
- Locked = -1 'True
- TabIndex = 0
- TabStop = 0 'False
- Top = 1080
- Width = 2655
- End
- Begin VB.TextBox add_r
- Height = 285
- Left = 2040
- Locked = -1 'True
- TabIndex = 2
- TabStop = 0 'False
- Top = 360
- Width = 1335
- End
- Begin VB.TextBox func_r
- Height = 285
- Left = 2040
- Locked = -1 'True
- TabIndex = 1
- TabStop = 0 'False
- Top = 720
- Width = 1335
- End
- Begin VB.Label Label6
- Caption = "Response Data"
- Height = 255
- Left = 240
- TabIndex = 24
- Top = 1080
- Width = 1335
- End
- Begin VB.Label Label9
- Caption = "Modbus Address"
- Height = 255
- Left = 240
- TabIndex = 23
- Top = 360
- Width = 1335
- End
- Begin VB.Label Label10
- Caption = "Function Code"
- Height = 255
- Left = 240
- TabIndex = 22
- Top = 720
- Width = 1335
- End
- End
- Begin VB.Frame Frame2
- Caption = "Modbus Request"
- Height = 1695
- Left = 120
- TabIndex = 17
- Top = 1920
- Width = 4935
- Begin VB.TextBox data_s
- Height = 480
- Left = 2040
- TabIndex = 9
- Text = "0500FF00"
- Top = 1080
- Width = 2655
- End
- Begin VB.TextBox func_s
- Height = 270
- Left = 2040
- MaxLength = 4
- TabIndex = 8
- Text = "5"
- Top = 720
- Width = 1335
- End
- Begin VB.TextBox add_s
- Height = 270
- Left = 2040
- MaxLength = 4
- TabIndex = 7
- Text = "1"
- Top = 360
- Width = 1335
- End
- Begin VB.Label Label4
- Caption = "Modbus Data"
- Height = 255
- Left = 240
- TabIndex = 20
- Top = 1080
- Width = 1335
- End
- Begin VB.Label Label3
- Caption = "Function Code"
- Height = 255
- Left = 240
- TabIndex = 19
- Top = 720
- Width = 1335
- End
- Begin VB.Label Label2
- Caption = "Modbus Address"
- Height = 255
- Left = 240
- TabIndex = 18
- Top = 360
- Width = 1335
- End
- End
- Begin VB.ComboBox Combo3
- Height = 315
- Left = 2160
- TabIndex = 5
- Text = "Combo3"
- Top = 1080
- Width = 1455
- End
- Begin VB.TextBox ipaddr
- Height = 270
- Left = 2160
- TabIndex = 6
- Top = 1440
- Width = 1695
- End
- Begin VB.ComboBox Combo2
- Height = 315
- ItemData = "Example_vb.frx":0000
- Left = 2160
- List = "Example_vb.frx":000A
- TabIndex = 4
- Text = "Combo2"
- Top = 720
- Width = 1455
- End
- Begin VB.ComboBox Combo1
- Height = 315
- ItemData = "Example_vb.frx":001A
- Left = 2160
- List = "Example_vb.frx":0024
- TabIndex = 3
- Text = "Combo1"
- Top = 360
- Width = 1455
- End
- Begin VB.Label Label7
- Caption = "Serial Port"
- Height = 255
- Left = 360
- TabIndex = 16
- Top = 1080
- Width = 1335
- End
- Begin VB.Label Label8
- Caption = "Modbus Mode"
- Height = 255
- Left = 360
- TabIndex = 15
- Top = 720
- Width = 1575
- End
- Begin VB.Label Label1
- Caption = "Communication Type"
- Height = 255
- Left = 360
- TabIndex = 14
- Top = 360
- Width = 1575
- End
- Begin VB.Label Label5
- Caption = "Slave IP Address"
- Height = 255
- Left = 360
- TabIndex = 13
- Top = 1440
- Width = 1335
- End
- End
- End
- Attribute VB_Name = "Form1"
- Attribute VB_GlobalNameSpace = False
- Attribute VB_Creatable = False
- Attribute VB_PredeclaredId = True
- Attribute VB_Exposed = False
- 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 Combo1_Click()
- Dim idx As Integer
- idx = Combo1.ListIndex
- Select Case idx
- Case Is = 0
- Combo2.ListIndex = 0
- Combo2.Enabled = True
- Combo3.Enabled = True
- ipaddr.BackColor = RGB(236, 233, 216)
- ipaddr.Enabled = False
- Case Is = 1
- Combo2.ListIndex = 1
- Combo2.Enabled = False
- Combo3.Enabled = False
- ipaddr.BackColor = RGB(255, 255, 255)
- ipaddr.Enabled = True
- End Select
- End Sub
- Private Sub Command1_Click()
- Dim sendbuf(1024) As Byte ' Send buffer
- Dim recvbuf(1024) As Byte ' Receive buffer
- Dim remote_addr As Long ' Remote IP Address
- Dim slav_addr_s As Long ' Request Station address
- Dim func_code_s As Long ' Request function code
- Dim slav_addr_r As Long ' Receive Station address
- Dim func_code_r As Long ' Receive function code
- Dim rlen As Integer ' Receive data length
- Dim ConnNum As Integer ' Serial port number or ethernet connection number
- Dim i As Integer
- Dim parity As Byte ' Parity
- Dim parity1 As Byte ' Parity
- Dim DataLen As Integer ' Request data length
- Dim ASC(1) As Byte ' Used to Convert HEX value to ASCII
- Dim RecvString As String ' Receive string
- Dim idx As Integer ' Communication Type
- Dim mode As Integer ' Modbus mode
- slav_addr_s = CInt(add_s.Text) ' Modbus Address (Decimal)
- func_code_s = CInt("&H" & func_s.Text) ' Function Code (Hex String to Integer)
- Dim DataString As String
- DataString = data_s.Text ' Data, ex:0500FF00
- Dim ComString As String
- ComString = Combo3.Text ' COM port string, ex: COM1
- remote_addr = inet_addr(ipaddr) ' Get Internet IP Address
- For i = 0 To Len(DataString) - 1 Step 2 ' Convert Modbus Data
- sendbuf(i / 2) = "&H" + Mid(DataString, i + 1, 2)
- Next i
- ConnNum = CInt(Mid(ComString, 4, Len(ComString))) ' Get COM port number
- ' Assign data
- idx = Combo1.ListIndex ' Select Communication Type
- mode = Combo2.ListIndex ' Modbus mode
- parity = AscB("E") ' Parity (E)
- parity1 = AscB("N") ' Parity
- DataLen = 4 ' Request data length
- ' Modbus Communication Start ---------------------------------------
- Select Case idx
- Case Is = 0 'Modbus
- If mode = 0 Then
- Call OpenModbusSerial(ConnNum, 9600, 7, parity, 1, mode + 1) ' Open Modbus (9600, 7, E, 1, (1 for ASCII, 2 for RTU))
- Else
- Call OpenModbusSerial(ConnNum, 9600, 8, parity1, 1, mode + 1) ' Open Modbus (9600, 8, N, 1, (1 for ASCII, 2 for RTU))
- End If
- Call RequestData(idx, ConnNum, slav_addr_s, func_code_s, sendbuf(0), DataLen) ' Request Data
- rlen = ResponseData(idx, ConnNum, slav_addr_r, func_code_r, recvbuf(0)) ' Response Data
- For i = 0 To rlen - 1
- Call HEX_to_ASCI(recvbuf(i), ASC)
- RecvString = RecvString & Chr(ASC(0)) & Chr(ASC(1))
- Next i
- Call CloseSerial(ConnNum) ' Close Modbus
- Case Is = 1 'Modbus/TCP
- ConnNum = 0 ' Connection number
- Call OpenModbusTCPSocket(ConnNum, remote_addr) ' Open Modbus TCP Socket
- Call RequestData(idx, ConnNum, slav_addr_s, func_code_s, sendbuf(0), 4) ' Request Data
- If (ReadSelect(ConnNum, 100)) Then
- rlen = ResponseData(idx, ConnNum, slav_addr_r, func_code_r, recvbuf(0)) ' Response Data
- End If
- For i = 0 To rlen - 1
- Call HEX_to_ASCI(recvbuf(i), ASC)
- RecvString = RecvString & Chr(ASC(0)) & Chr(ASC(1))
- Next i
- Call CloseSocket(ConnNum) ' Close Modbus TCP Socket
- End Select
- ' Modbus Communication End -----------------------------------------
- add_r = slav_addr_r ' Assign data
- func_r = Hex(func_code_r)
- data_r.Text = RecvString
- ' Switch Y0 on or off
- If data_s.Text = "0500FF00" Then
- data_s.Text = "05000000"
- Else
- data_s.Text = "0500FF00"
- End If
- End Sub
- ' Convert ASCII to HEX
- Private Function ASCI_to_HEX(ByRef asci() As Byte, ByRef value_hex As Byte)
- value_hex = &H0
- Dim i As Integer
- i = 0
- For i = 0 To 2
- If (asci(i) >= &H30 & asci(i) <= &H39) Then ' 0-9
- value_hex = (value_hex * 16) + (asci(i) - &H30)
- ElseIf (asci(i) >= &H41 & asci(i) <= &H46) Then ' A-F
- value_hex = ((value_hex * 16) + (asci(i) - &H37))
- ElseIf (asci(i) >= &H61 & asci(i) <= &H66) Then ' a-f
- value_hex = ((value_hex * 16) + (asci(i) - &H57))
- End If
- Next i
- End Function
- 'Convert HEX to ASCII
- Private Sub HEX_to_ASCI(ByVal value_hex As Byte, ByRef asci() As Byte)
- Dim reglow, reghigh As Byte
- reghigh = (value_hex And &HF0) / 16 ' >> 4
- reglow = value_hex And &HF
- If (reghigh <= 9) Then
- asci(0) = reghigh + &H30
- Else
- asci(0) = (reghigh - 10) + &H41
- End If
- If (reglow <= 9) Then
- asci(1) = reglow + &H30
- Else
- asci(1) = (reglow - 10) + &H41
- End If
- End Sub
- Private Sub Command2_Click()
- End
- End Sub
- Private Sub Form_Load()
- 'Check registry serial Port list Start -----------------------------
- Dim typecode As Long
- Dim lngKeyHandle As Long
- Dim lngResult As Long
- Dim lngCurIdx As Long
- Dim ValueName As String * 256
- Dim ValueNameLen As Long
- Dim Value As String * 256
- Dim ValueLen As Long
- lngResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "HARDWAREDEVICEMAPSERIALCOMM", 0&, KEY_READ, lngKeyHandle)
- If lngResult <> ERROR_SUCCESS Then
- MsgBox ("Cannot open key")
- Exit Sub
- End If
- lngCurIdx = 0
- ValueNameLen = 256
- ValueLen = 256
- While RegEnumValue(lngKeyHandle, lngCurIdx, ByVal ValueName, ValueNameLen, 0&, typecode, ByVal Value, ValueLen) = ERROR_SUCCESS
- If typecode = REG_SZ Then
- Combo3.AddItem Value
- End If
- lngCurIdx = lngCurIdx + 1
- ValueNameLen = 256
- ValueLen = 256
- Wend
- Call RegCloseKey(lngKeyHandle)
- 'Check registry serial Port list End -------------------------------
- Combo1.ListIndex = 0
- Combo2.ListIndex = 0
- Combo3.ListIndex = 0
- Call Combo1_Click
- End Sub