对于初学modbus;通过VB来测试modbus通信;有一点帮助;如果要原代码的话,可以留下邮箱,我给你传过去!
1 Private Sub Command1_Click() ‘设置按钮
2 Dim bisend() As Byte
3 Dim crc
4 Dim btLoCRC As Byte, btHiCRC As Byte
5 Dim Data As Integer
6 If MSComm1.PortOpen = True Then
7 If Combo5.ListIndex = 0 Then
8 ReDim bisend(7) ‘重新定义数组长度
9 bisend(0) = "&h" + Hex(Val(Text1.Text)) ‘地址码
10 bisend(1) = "&h" + Hex(3) ‘功能码 读寄存器
11 bisend(2) = "&h" + Hex(0) ‘起始地址高位
12 bisend(3) = "&h" + Hex(0) ‘起始地址低位
13 bisend(4) = "&h" + Hex(0) ‘寄存器个数高位
14 bisend(5) = "&h" + Hex(Combo6.ListIndex + 1) ‘寄存器个数低位
15 crc = CRC16(bisend, 6, btLoCRC, btHiCRC)
16 bisend(6) = "&h" + Hex(btLoCRC) ‘CRC高位
17 bisend(7) = "&h" + Hex(btHiCRC) ‘CRC低位
18 ‘发送数据
19 MSComm1.Output = bisend
20 Else
21 ReDim bisend(10) ‘一次只能写一个寄存器
22 bisend(0) = "&h" + Hex(Val(Text1.Text)) ‘地址码
23 bisend(1) = "&h" + Hex(16) ‘功能码 写寄存器
24 bisend(2) = "&h" + Hex(0) ‘起始地址高位
25 bisend(3) = "&h" + Hex(0) ‘起始地址低位
26 bisend(4) = "&h" + Hex(0) ‘寄存器个数高位
27 bisend(5) = "&h" + Hex(1) ‘寄存器个数低位
28 bisend(6) = "&h" + Hex(2) ‘字节数
29 Data = Val(Trim(Text3.Text))
30 bisend(7) = "&h" + Hex(Data \ 256) ‘要写入寄存器的值的高字节
31 bisend(8) = "&h" + Hex(Data Mod 256) ‘要写入寄存器的值的低字节
32 crc = CRC16(bisend, 9, btLoCRC, btHiCRC)
33 bisend(9) = "&h" + Hex(btLoCRC) ‘CRC高位
34 bisend(10) = "&h" + Hex(btHiCRC) ‘CRC低位
35 MSComm1.Output = bisend
36 End If
37 Else
38 MsgBox "串口没有打开"
39 End If
40 End Sub
41 Private Sub Command2_Click() ‘实时采集按钮
42 Timer1.Enabled = Not Timer1.Enabled ‘进行状态切换
43 End Sub
44 Private Sub Command3_Click()
45 ‘初始化,并打开串口
46 With MSComm1
47 If .PortOpen = False Then
48 .CommPort = Combo7.ListIndex + 1 ‘打开串口1
49 .Settings = Combo1.Text + "," + Combo2.Text + "," + Combo3.Text + Combo4.Text
50 .InputMode = 1
51 .InputLen = 50 ‘一次性从接收缓冲区中读取所有数据(8个字节为一组!!)
52 .InBufferC = 0 ‘清空接收缓冲区
53 .OutBufferCt = 0 ‘清空发送缓冲区
54 .RThreshold = 5 + (Combo6.ListIndex + 1) * 2
55 .InBufferSize = 1024
56 .OutBufferSize = 1024
57
58 .PortOpen = True
59 Else
60 MsgBox "串口已经打开"
61 End If
62 End With
63 End Sub
64 Private Sub Command4_Click() ‘关闭串口按钮
65 If MSComm1.PortOpen = True Then
66 MSComm1.PortOpen = False
67 End If
68 End Sub
69 Private Sub Form_Load()
70 Dim i As Integer
71
72 ‘波特率设置
73 Combo1.AddItem "4800", 0
74 Combo1.AddItem "9600", 1
75 Combo1.AddItem "115200", 2
76 ‘校验位设置
77 Combo2.AddItem "N", 0
78 Combo2.AddItem "E", 1
79 Combo2.AddItem "O", 2
80 ‘数据位设置
81 Combo3.AddItem "7", 0
82 Combo3.AddItem "8", 1
83 ‘停止位设置
84 Combo4.AddItem "1", 0
85 Combo4.AddItem "2", 1
86 ‘功能码选择
87 Combo5.AddItem "读寄存器03", 0
88 Combo5.AddItem "写寄存器16", 1
89 ‘寄存器个数设置
90 Combo6.AddItem "1", 0
91 Combo6.AddItem "2", 1
92 Combo6.AddItem "3", 2
93 Combo6.AddItem "4", 3
94 Combo6.AddItem "5", 4
95 Combo6.AddItem "6", 5
96 Combo6.AddItem "7", 6
97 Combo6.AddIt
楼主,我是新手..有两个问题请教下楼主 问题问的很白 希望楼主别笑话
第一个是:在下面的代码中crc = CRC16(tbisend, 6, btLoCRC, btHiCRC) 是什么意思?程序中其他地方也没用到CRC
为什么还要把CRC16的值给CRC?
第二个是:下面代码中tbisend(6) = "&h" + Hex(btLoCRC) ‘CRC高位
tbisend(7) = "&h" + Hex(btHiCRC) ‘CRC低位
这两段中的btLoCRC,btHiCRC两个参数是CRC16计算出来的结果么?
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