?? modbus.frm
字號:
VERSION 5.00
Object = "{648A5603-2C6E-101B-82B6-000000000014}#1.1#0"; "MSComm32.ocx"
Begin VB.Form Form1
Caption = "MODBUSTEST"
ClientHeight = 7650
ClientLeft = 60
ClientTop = 450
ClientWidth = 11820
LinkTopic = "Form1"
ScaleHeight = 7650
ScaleWidth = 11820
StartUpPosition = 3 'Windows Default
Begin VB.CommandButton Command4
Caption = "關閉串口"
Height = 495
Left = 4680
TabIndex = 23
Top = 1800
Width = 1215
End
Begin VB.ListBox List1
Height = 2595
ItemData = "modbus.frx":0000
Left = 240
List = "modbus.frx":0002
TabIndex = 22
Top = 4680
Width = 11295
End
Begin VB.TextBox Text3
Height = 375
Left = 1800
TabIndex = 20
Top = 3240
Width = 1455
End
Begin VB.CommandButton Command3
Caption = "打開串口"
Height = 495
Left = 4560
TabIndex = 19
Top = 360
Width = 1215
End
Begin VB.Timer Timer1
Enabled = 0 'False
Interval = 1000
Left = 8160
Top = 3960
End
Begin VB.CommandButton Command2
Caption = "實時采集"
Height = 495
Left = 7920
TabIndex = 18
Top = 3120
Width = 1215
End
Begin MSCommLib.MSComm MSComm1
Left = 5280
Top = 3960
_ExtentX = 1005
_ExtentY = 1005
_Version = 393216
DTREnable = 0 'False
InputLen = 20
InputMode = 1
End
Begin VB.ComboBox Combo7
Height = 315
Left = 1800
TabIndex = 16
Text = "Combo7"
Top = 360
Width = 1575
End
Begin VB.ComboBox Combo6
Height = 315
Left = 8760
TabIndex = 14
Text = "Combo6"
Top = 2280
Width = 1575
End
Begin VB.CommandButton Command1
Caption = "設置"
Height = 495
Left = 4680
TabIndex = 13
Top = 3120
Width = 1215
End
Begin VB.TextBox Text1
Height = 375
Left = 1800
TabIndex = 11
Top = 2400
Width = 1575
End
Begin VB.ComboBox Combo5
Height = 315
ItemData = "modbus.frx":0004
Left = 1800
List = "modbus.frx":0006
TabIndex = 9
Text = "Combo5"
Top = 1800
Width = 1575
End
Begin VB.ComboBox Combo4
Height = 315
Left = 8760
TabIndex = 7
Text = "Combo4"
Top = 1680
Width = 1575
End
Begin VB.ComboBox Combo3
Height = 315
ItemData = "modbus.frx":0008
Left = 1800
List = "modbus.frx":000A
TabIndex = 5
Text = "Combo3"
Top = 1080
Width = 1575
End
Begin VB.ComboBox Combo2
Height = 315
Left = 8760
TabIndex = 2
Text = "Combo2"
Top = 960
Width = 1575
End
Begin VB.ComboBox Combo1
Height = 315
Left = 8760
TabIndex = 0
Text = "Combo1"
Top = 240
Width = 1575
End
Begin VB.Label Label10
Caption = "發送數據"
Height = 255
Left = 600
TabIndex = 21
Top = 3360
Width = 1215
End
Begin VB.Label Label9
Caption = "串口選擇"
Height = 255
Left = 360
TabIndex = 17
Top = 360
Width = 1215
End
Begin VB.Label Label8
Caption = "校驗位設置"
Height = 255
Left = 7320
TabIndex = 15
Top = 960
Width = 975
End
Begin VB.Label Label7
Caption = "接受數據"
Height = 375
Left = 240
TabIndex = 12
Top = 4320
Width = 1215
End
Begin VB.Label Label6
Caption = "地址設置"
Height = 255
Left = 360
TabIndex = 10
Top = 2520
Width = 975
End
Begin VB.Label Label5
Caption = "寄存器個數設置"
Height = 255
Left = 7320
TabIndex = 8
Top = 2280
Width = 1455
End
Begin VB.Label Label4
Caption = "功能選擇"
Height = 255
Left = 360
TabIndex = 6
Top = 1800
Width = 975
End
Begin VB.Label Label3
Caption = "停止位設置"
Height = 255
Left = 7320
TabIndex = 4
Top = 1680
Width = 1215
End
Begin VB.Label Label2
Caption = "數據位設置"
Height = 255
Left = 360
TabIndex = 3
Top = 1080
Width = 1215
End
Begin VB.Label Label1
Caption = "波特率設置"
Height = 255
Left = 7320
TabIndex = 1
Top = 240
Width = 1215
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub Command1_Click() '設置按鈕
Dim bisend() As Byte
Dim crc
Dim btLoCRC As Byte, btHiCRC As Byte
Dim data As Integer
If MSComm1.PortOpen = True Then
If Combo5.ListIndex = 0 Then
ReDim bisend(7) '重新定義數組長度
bisend(0) = "&h" + Hex(Val(Text1.Text)) '地址碼
bisend(1) = "&h" + Hex(3) '功能碼 讀寄存器
bisend(2) = "&h" + Hex(0) '起始地址高位
bisend(3) = "&h" + Hex(0) '起始地址低位
bisend(4) = "&h" + Hex(0) '寄存器個數高位
bisend(5) = "&h" + Hex(Combo6.ListIndex + 1) '寄存器個數低位
crc = CRC16(bisend, 6, btLoCRC, btHiCRC)
bisend(6) = "&h" + Hex(btLoCRC) 'CRC高位
bisend(7) = "&h" + Hex(btHiCRC) 'CRC低位
'發送數據
MSComm1.Output = bisend
Else
ReDim bisend(10) '一次只能寫一個寄存器
bisend(0) = "&h" + Hex(Val(Text1.Text)) '地址碼
bisend(1) = "&h" + Hex(16) '功能碼 寫寄存器
bisend(2) = "&h" + Hex(0) '起始地址高位
bisend(3) = "&h" + Hex(0) '起始地址低位
bisend(4) = "&h" + Hex(0) '寄存器個數高位
bisend(5) = "&h" + Hex(1) '寄存器個數低位
bisend(6) = "&h" + Hex(2) '字節數
data = Val(Trim(Text3.Text))
bisend(7) = "&h" + Hex(data \ 256) '要寫入寄存器的值的高字節
bisend(8) = "&h" + Hex(data Mod 256) '要寫入寄存器的值的低字節
crc = CRC16(bisend, 9, btLoCRC, btHiCRC)
bisend(9) = "&h" + Hex(btLoCRC) 'CRC高位
bisend(10) = "&h" + Hex(btHiCRC) 'CRC低位
MSComm1.Output = bisend
End If
Else
MsgBox "串口沒有打開"
End If
End Sub
Private Sub Command2_Click() '實時采集按鈕
Timer1.Enabled = Not Timer1.Enabled '進行狀態切換
End Sub
Private Sub Command3_Click()
'初始化,并打開串口
With MSComm1
If .PortOpen = False Then
.CommPort = Combo7.ListIndex + 1 '打開串口1
.Settings = Combo1.Text + "," + Combo2.Text + "," + Combo3.Text + Combo4.Text
.InputMode = 1
.InputLen = 50 '一次性從接收緩沖區中讀取所有數據(8個字節為一組!!)
.InBufferCount = 0 '清空接收緩沖區
.OutBufferCount = 0 '清空發送緩沖區
.RThreshold = 5 + (Combo6.ListIndex + 1) * 2
.InBufferSize = 1024
.OutBufferSize = 1024
.PortOpen = True
Else
MsgBox "串口已經打開"
End If
End With
End Sub
Private Sub Command4_Click() '關閉串口按鈕
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False
End If
End Sub
Private Sub Form_Load()
Dim i As Integer
'波特率設置
Combo1.AddItem "4800", 0
Combo1.AddItem "9600", 1
Combo1.AddItem "115200", 2
'校驗位設置
Combo2.AddItem "N", 0
Combo2.AddItem "E", 1
Combo2.AddItem "O", 2
'數據位設置
Combo3.AddItem "7", 0
Combo3.AddItem "8", 1
'停止位設置
Combo4.AddItem "1", 0
Combo4.AddItem "2", 1
'功能碼選擇
Combo5.AddItem "讀寄存器03", 0
Combo5.AddItem "寫寄存器16", 1
'寄存器個數設置
Combo6.AddItem "1", 0
Combo6.AddItem "2", 1
Combo6.AddItem "3", 2
Combo6.AddItem "4", 3
Combo6.AddItem "5", 4
Combo6.AddItem "6", 5
Combo6.AddItem "7", 6
Combo6.AddItem "8", 7
Combo6.AddItem "9", 8
Combo6.AddItem "10", 9
Combo6.AddItem "11", 10
Combo6.AddItem "12", 11
Combo6.AddItem "13", 12
Combo6.AddItem "14", 13
Combo6.AddItem "15", 14
Combo6.AddItem "16", 15
Combo6.AddItem "17", 16
Combo6.AddItem "18", 17
Combo6.AddItem "19", 18
Combo6.AddItem "20", 19
Combo6.AddItem "21", 20
Combo6.AddItem "22", 21
'串口選擇
Combo7.AddItem "串口1", 0
Combo7.AddItem "串口2", 1
Combo7.AddItem "串口3", 2
Combo7.AddItem "串口4", 3
'初始賦值
Combo1.ListIndex = 1
Combo2.ListIndex = 1
Combo3.ListIndex = 1
Combo4.ListIndex = 0
Combo5.ListIndex = 0
Combo6.ListIndex = 2
Combo7.ListIndex = 0
'初始化串口
End Sub
Private Sub Form_Unload(Cancel As Integer)
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False
End If
End Sub
Private Sub MSComm1_OnComm()
Dim INByte() As Byte
Dim Buf As String
Dim btLoCRC As Byte, btHiCRC As Byte
Dim data As Integer
If MSComm1.CommEvent = comEvReceive Then '接收到數據以后
INByte = MSComm1.Input
If INByte(1) = 3 Then '讀寄存器
'CRC校驗
crc = CRC16(INByte, UBound(INByte) - LBound(INByte) - 1, btLoCRC, btHiCRC)
If INByte(UBound(INByte) - 1) = btLoCRC And INByte(UBound(INByte)) = btHiCRC Then
'校驗正確
'////////////////////////////////////
For i = 3 To UBound(INByte) - 2 Step 2
data = "&h" + Hex(INByte(i)) + Hex(INByte(i + 1))
' Buf = Buf + Hex(INByte(i)) + Chr(32)
Buf = Buf + str(data) '轉換為十進制顯示
Next i
List1.AddItem Buf
End If
End If
MSComm1.InBufferCount = 0 '請緩存
End If
End Sub
Private Sub Timer1_Timer()
'定時發送命令
Dim tbisend(7) As Byte
Dim crc '定時1s
Dim btLoCRC As Byte, btHiCRC As Byte
Dim Buf As String
If MSComm1.PortOpen = True Then
tbisend(0) = "&h" + Hex(Val(Text1.Text)) '地址碼
tbisend(1) = "&h" + Hex(3) '功能碼 讀寄存器
tbisend(2) = "&h" + Hex(0) '起始地址高位
tbisend(3) = "&h" + Hex(0) '起始地址低位
tbisend(4) = "&h" + Hex(0) '寄存器個數高位
tbisend(5) = "&h" + Hex(Combo6.ListIndex + 1) '寄存器個數低位
crc = CRC16(tbisend, 6, btLoCRC, btHiCRC)
tbisend(6) = "&h" + Hex(btLoCRC) 'CRC高位
tbisend(7) = "&h" + Hex(btHiCRC) 'CRC低位
'發送數據
MSComm1.Output = tbisend
End If
End Sub
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -