VB串口通信(附图教程)

合集下载
相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

界面如下:

代码如下:

Private Sub Form_Load()

If MSComm1.PortOpen = True Then MSComm1.PortOpen = False Else

End If

Combo1.AddItem "COM1"

Combo1.AddItem "COM2"

Combo1.AddItem "COM3"

Combo1.AddItem "COM4"

Combo1.AddItem "COM5"

Combo1.AddItem "COM6"

Combo1.AddItem "COM7"

Combo1.AddItem "COM8"

Combo1.AddItem "COM9"

Combo1.AddItem "COM10" Combo1.AddItem "COM11"

Combo1.AddItem "COM12"

Combo1.AddItem "COM13"

Combo1.AddItem "COM14"

Combo1.AddItem "COM15"

Combo1.AddItem "COM16"

Combo1.ListIndex = 2

Combo2.AddItem "256000"

Combo2.AddItem "128000"

Combo2.AddItem "115200"

Combo2.AddItem "57600"

Combo2.AddItem "38400"

Combo2.AddItem "28800"

Combo2.AddItem "19200"

Combo2.AddItem "14400"

Combo2.AddItem "12800"

Combo2.AddItem "11520"

Combo2.AddItem "9600"

Combo2.AddItem "4800"

Combo2.AddItem "2400"

Combo2.AddItem "1200"

Combo2.AddItem "600"

Combo3.AddItem "无None"

Combo3.AddItem "奇Odd"

Combo3.AddItem "偶Even"

Combo4.AddItem "4"

Combo4.AddItem "5"

Combo4.AddItem "6"

Combo4.AddItem "7"

Combo4.AddItem "8"

Combo5.AddItem "1"

Combo5.AddItem "2"

mPort = Combo1.ListIndex + 1 MSComm1.Settings = "9600,n,8,1" ComOpen.Caption = "打开串口"

Shape1.FillColor = &HFFFFC0

End Sub

Private Sub ComOpen_Click()

On Error GoTo uerror '发现错误跳转到错误处理

If ComOpen.Caption = "关闭串口" Then

MSComm1.PortOpen = False

ComOpen.Caption = "打开串口" '按钮文字改变

Shape1.FillColor = &HFFFFC0 '灯颜色改变

Else

MSComm1.PortOpen = True

ComOpen.Caption = "关闭串口"

Shape1.FillColor = &HFF

End If

Exit Sub

uerror:

msg$ = "无效端口号" '错误显示

Title$ = "串口调试助手"

X = MsgBox(msg$, 48, Title$) '48标示显示警告图标

End Sub

Private Sub MSComm1_OnComm()

Dim BytReceived() As Byte

Dim strBuff As String

Dim i As Integer

Select Case mEvent '事件发生

Case 2

Cls

MSComm1.InputLen = 0 '读入缓冲区全部内容

strBuff = MSComm1.Input '读入到缓冲区

Label10.Caption = Label10.Caption + Len(strBuff) '接收计数

If MSComm1.InputMode = comInputModeBinary Then

BytReceived() = strBuff '如果是二进制接收模式则进行数据处理,否则直接显示字符串

For i = 0 To UBound(BytReceived)

If Len(Hex(BytReceived(i))) = 1 Then

strData = strData & "0" & Hex(BytReceived(i)) & " " '如果只有一个字符,则前补0,如F显示0F,最后补空格

Else '方便显示观察如:00 0F FE

strData = strData & Hex(BytReceived(i)) & " "

End If

Next

TextReceive = TextReceive & strData

strData = ""

Else

TextReceive = TextReceive & strBuff

End If

End Select

End Sub

Private Sub ComSend1_Click() '手动发送

Dim Temp(0) As Byte

Dim strBuff As String

If Option1.Value = True Then '如果显示16进制发送则进行16进制处理,这里只发送一个

Temp(0) = "&H" & TextSend

MSComm1.Output = Temp '发送一个16进制

Else

strBuff = TextSend

End If

If MSComm1.PortOpen = False Then

MsgBox "请打开串口"

End If

On Error GoTo uerror

MSComm1.Output = strBuff

Label11.Caption = Label11.Caption + Len(strBuff) '发送计数

uerror:

End Sub

Private Sub ComSend2_Click()

If ComSend2.Caption = "自动发送" Then

ComSend2.Caption = "关闭自动发送"

Timer1.Interval = TextTime.Text

Timer1.Enabled = True

Else

ComSend2.Caption = "自动发送"

Timer1.Enabled = False

相关文档
最新文档