就是VB编写串口通讯,以下程序可以参考.我也是看它做的
1. Option Explicit
2. Dim a As Integer
3. Dim BytReceived() As Byte
4. Dim strData As String
5. Dim lenInput As Integer
6. Dim bytSendByte() As Byte '发送二进制数据
7. Dim strSendText As String '发送文本数据
8. Dim blnAutoSendFlag As Boolean
9. Dim openFlag As Boolean
10.
11. Private Sub cmdOpen_Click() '打开关闭端口
12. If openFlag Then
13. cmdOpen.Caption = "打开串口"
14. MSComm1.PortOpen = False '打开端口
15. Shape1.FillColor = vbRed
16. Label1 = "COM" & a & " 关闭"
17. Else
18. OpenPortNum
19. cmdOpen.Caption = "关闭串口"
20. Shape1.FillColor = vbGreen
21. Label1 = "COM" & a & " 打开"
22. End If
23. openFlag = Not openFlag
24. End Sub
25.
26. Private Sub cmdSendASc_Click() '文本发送
27. Dim sj_Txt As String
28. sj_Txt = TxtSend
29. If MSComm1.PortOpen = True Then
30. MSComm1.Output = sj_Txt
31. End If
32. End Sub
33.
34. Private Sub cmdSendHex_Click() '16进制发送
35. Dim sj() As Byte
36. Dim sj_Txt As String
37. Dim i As Integer
38. sj_Txt = TxtSend
39. If Len(TxtSend) Mod 2 = 0 And Len(TxtSend) <> 0 Then '检验16进制字符串长
40. ReDim sj(Len(sj_Txt) / 2 - 1)
41. For i = 0 To Len(sj_Txt) - 1 Step 2
42. sj(i / 2) = Val("&H" & Mid(sj_Txt, i + 1, 2))
43. Next
44. If MSComm1.PortOpen = True Then
45. MSComm1.Output = sj
46. Else
47. MSComm1.PortOpen = True
48. Shape1.FillColor = vbGreen
49. MSComm1.Output = sj
50. End If
51. Else
52. MsgBox ("格式不对!")
53. End If
54. End Sub
55. '字符串表示的十六进制数据转化为相应的字节串,返回转化后的字节数
56.
57. Private Sub OpenPortNum()
58. On Error GoTo uerror
59. For a = 1 To 16
60. = a
61. MSComm1.PortOpen = True
62. If MSComm1.PortOpen = True Then
63. 'Print "可用Com号= "; a
64. Exit Sub
65. Else
66. End If
67. Next
68. Exit Sub
69. uerror:
70. 'Print "出错或占用Com号= "; a
71. Resume Next
72. End Sub
73.
74. Private Sub Form_Load()
75. MSComm1.Settings = "9600,n,8,1"
76. MSComm1.InputMode = comInputModeBinary '采用二进制传输
77. MSComm1.InBufferCount = 0 '清空接受缓冲区
78. MSComm1.OutBufferCount = 0 '清空传输缓冲区
79. MSComm1.RThreshold = 1 '产生MSComm事件
80. MSComm1.InBufferSize = 1024
81. TxtSend = ""
82. TxtSend = "" '800A00113135323634389794"
83. txtReceive = ""
84. Text2 = ""
85. Call cmdOpen_Click
86. End Sub
87.
88. Private Sub MSComm1_OnComm() '接收数据
89. Dim strBuff As String
90. Select Case
91. Case 2
92. MSComm1.InputLen = 0
93. strBuff = MSComm1.Input
94. BytReceived() = strBuff
95. jieshou
96. lenInput = Len(strData)
97. Text2 = lenInput \ 2
98. '数据处理代码
99. End Select
100. End Sub
101.
102. Public Function jieshou() '接收数据处理为16进制
103. Dim i As Integer
104. For i = 0 To UBound(BytReceived)
105. If Len(Hex(BytReceived(i))) = 1 Then
106. strData = strData & "0" & Hex(BytReceived(i))
107. Else
108. strData = strData & Hex(BytReceived(i))
109. End If
110. Next
111. txtReceive = strData
112. End Function