哪位大神做过vb的串口通信?
基于vb的PC与单片机串口通信,上位机代码写不下去了
把很早以前做的VB与单片机通讯的串口查找和收/发串口信息的代码给你参考
'搜索并列出所有可用串口
Sub findCOM()
Dim i As Byte
With Form1
For i = 1 To 16
On Error Resume Next
'当运行发生错误时,控件转到紧接着发生错误的语句之后的语句,并在此继续运行
. = i
.MSComm1.PortOpen = True
Select Case Err.Number
Case 0 '错误号为0(也就是没出错),
.Combo1.AddItem "Com" & Trim(i)
.MSComm1.PortOpen = False
Case 8005 '错误号为8005,也就是端口被占用
.Combo1.AddItem "Com" & Trim(i) & " 已经占用"
.MSComm1.PortOpen = False
End Select
Err = 0 '将错误号置0. 注:Err.Number可以简写为Err ,2者等效
Next
End With
End Sub
'发数据
'*************************************************************
'电子地图发布 盲节点显示地图
'*************************************************************
Sub emap_send()
Dim senddata(1 To 20) As Byte
Dim temp, i As Byte
create_emap
senddata(1) = &H2
senddata(2) = &H0
senddata(3) = &H18
senddata(4) = &HF '数据长度
senddata(5) = &HCB
senddata(6) = blindNode.addr_low '盲节点地址低八位
senddata(7) = blindNode.addr_high '盲节点地址高八位
senddata(8) = &HD3 '盲节点endpoint
senddata(9) = &H1E '簇低八位
senddata(10) = &H0 '簇高八位
senddata(11) = &H8 '数据长度
senddata(12) = emap(1)
senddata(13) = emap(2)
senddata(14) = emap(3)
senddata(15) = emap(4)
senddata(16) = emap(5)
senddata(17) = emap(6)
senddata(18) = emap(7)
senddata(19) = emap(8)
senddata(20) = fcs(senddata, 2, 19)
Form1.MSComm1.PortOpen = False
Form1.MSComm1.PortOpen = True
Form1.MSComm1.Output = senddata
End Sub
'收数据
Private Sub MSComm1_OnComm()
Dim inbyte() As Byte
Dim buf As String
Dim i, j As Integer
j = 1
inbyte = MSComm1.Input
For i = LBound(inbyte) To UBound(inbyte)
buf = buf + Hex(inbyte(i)) + Chr(32)
uartin(j) = inbyte(i)
j = j + 1
Next i
'Text1.Text = Text1.Text & buf & Chr(13) & Chr(10) '网关传至PC的数据
Call inputCompile '调用串口数据处理
End Sub
[ 本帖最后由 lianyicq 于 2015-4-19 18:22 编辑 ]