VB串口编程,使用MSComm
PC连接一个多画面合成器,波特率:9600,1位起始位,8位数据,1位校验位,1位停止位。
发往下位机的命令由两字节构成:55H(地址码)--命令码,第一字节为地址码(十六进制数55H);第二字节为命令码,两字节间时延不要超过50毫秒(这个我就不太明白了)。命令码依次为31H,32H,33H,34H,35H,36H,37H,38H,39H,3AH,3BH,3CH,3DH。
我现在刚刚开始学习,还不太清楚,请问我应该如何编写。谢谢。
分不多了,挤出一半给大家分。
[解决办法]
估计就是这样
Option ExplicitPrivate Sub Command1_Click() Dim a(0 To 1) As Byte a(0) = &H55 a(1) = &H31 MSComm1.Output = aEnd SubPrivate Sub Form_Load() MSComm1.CommPort = 1 '"选用com1串行口 MSComm1.Settings = "115200,n,8,1" '"波特率9600,无奇偶校验位,8位数据位1位停止位 MSComm1.InputLen = 1 '"读取input接收缓冲区全部字节 MSComm1.InBufferSize = 1024 '"设置接收缓冲区的字节长度 MSComm1.InBufferCount = 0 '"清除发送缓冲区数据 MSComm1.OutBufferCount = 0 '"清除接收缓冲区数据 MSComm1.InputMode = 1 '"输入模式为binary MSComm1.RThreshold = 1 '"控件收到数据时将触发OnComm事件 MSComm1.Handshaking = 2 MSComm1.PortOpen = TrueEnd SubPrivate Sub MSComm1_OnComm() Dim a() As Byte a = MSComm1.Input Dim i As Long Debug.Print UBound(a) 'For i = 0 To UBound(a) ' Debug.Print Hex$(a(i)) & " "; 'Next iEnd Sub
[解决办法]
你网上下载一个串口调试工具,例如《串口精灵》
然后在串口精灵上选择十六进制发送模式,定时器发送时间=100毫秒
波特率设置为9600,1位起始位,8位数据,1位校验位,1位停止位
然后在输入框中输入55 31
然后看你的单片机是否有反应,当单片机接收正确,你再用VB编写上位机程序嘛!
[解决办法]
Private Sub Form_Load()
'设置打开端口
If OpenPort(3) = False Then
MsgBox "串口打开失败!"
End If
End Sub
Private Sub Command1_Click()
Dim sBuf(2) As Byte
sBuf(0) = &H55
sBuf(1) = &H31
sBuf(2) = &HFF
If SendByte(sBuf) = False Then
End If
End Sub
'收到数据产生MSComm事件
Private Sub MSComm1_OnComm()
Dim i As Integer
Dim rBuf() As Byte
rBuf = MSComm1.Input
For i = 0 To UBound(rBuf)
Debug.Print rBuf(i)
Next i
End Sub
'串口初始化
Private Function OpenPort(PortNum As Integer) As Boolean
On Error GoTo ErrExit '出错转移到ErrExit处理
With Me.MSComm1
.CommPort = PortNum '端口号
.Settings = "9600,n,8,1" '波特率=9600bps;n(无奇偶校验)e(偶校验)o(奇校验);8位数据位;1位停止位
.InputMode = 1 '采用二进制数据传输
.NullDiscard = False 'NULL字符从端口传送到接受缓冲区
.DTREnable = False 'DTR线无效
.EOFEnable = False '不寻找EOF符
.RTSEnable = False 'RTS线无效
.InBufferCount = 0 '清空接受缓冲区
.OutBufferCount = 0 '清空传输缓冲区
.SThreshold = 0 '不产生MSComm事件
.RThreshold = 1 '产生MSComm事件
.InputLen = 0 '当输入占用时,告诉控件读入整个缓冲区。
.InBufferSize = 256 '接收缓冲区 默认为1024个字节
.OutBufferSize = 256 '发送缓冲区 默认为512个字节
.PortOpen = True '打开端口
End With
OpenPort = True '标明串口打开
Exit Function '函数结束
ErrExit: '错误处理
OpenPort = False '标明串口打开失败
End Function '函数结束
'以二进制发送byte数组
Private Function SendByte(Buf() As Byte) As Boolean
On Error GoTo ErrExit '出错转移到ErrExit处理
With MSComm1
.OutBufferCount = 0 '清空接收缓冲区
.Output = Buf '发送数据
End With
SendByte = True '标明数据发送成功
Exit Function '函数结束
ErrExit: '错误处理
SendByte = False '标明串口发送失败
End Function
------解决方案--------------------
LZ:你的说明未说明校验是奇校验?偶校验?无校验?,以下代码是按无奇偶校验。你说无反应,我估计是未设置MSComm1.RThreshold=1
Option Explicit Dim sjByte(1) As Byte Dim BytReceived() As Byte Dim strData As String Dim openFlag As BooleanPrivate Sub Command1_Click() If openFlag Then Timer1.Enabled = False '停止论询 Command1.Caption = "Send" Else Timer1.Enabled = True '执行论询 Command1.Caption = "Stop" End If openFlag = Not openFlagEnd SubPrivate Sub Form_Load() MSComm1.CommPort = 1 MSComm1.Settings = "9600,N,8,1" 'N是无奇偶校验 MSComm1.InputMode = comInputModeBinary '二进制方式接收 MSComm1.RThreshold = 1 '产生OnComm事件 MSComm1.PortOpen = True Timer1.Interval = 50 Timer1.Enabled = FalseEnd SubPrivate Sub MSComm1_OnComm() '接收数据 Dim strBuff As String Text1 = "" Select Case MSComm1.CommEvent Case 2 MSComm1.InputLen = 0 strBuff = MSComm1.Input BytReceived() = strBuff jieshou Text1 = strData End SelectEnd SubPrivate Sub Timer1_Timer() '论询命令生成 Static sum As Integer sjByte(0) = &H55 sum = sum + 1 sjByte(1) = 48 + sum MSComm1.Output = sjByte If sum >= 13 Then sum = sum - &HD End IfEnd SubPublic Sub jieshou() '接收处理为16进制显示 Dim i As Integer For i = 0 To UBound(BytReceived) If Len(Hex(BytReceived(i))) = 1 Then strData = strData & "0" & Hex(BytReceived(i)) Else strData = strData & Hex(BytReceived(i)) End If NextEnd Sub
[解决办法]
MSComm1.CommPort = 1
MSComm1.Settings = "9600,n,8,1"
MSComm1.InputLen = 0
MSComm1.InputMode=0
MSComm1.PortOpen = True
dim a() as byte
dim I as integer
redim a(2)
a(0)=&H55
for I=1 to 13
Sleep(1000)
a(1)=&H30+i
MSComm1.output=a
next I
mscomm.output = a