VB界面与单片机进行通讯之VB编程

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

Option Explicit
Dim Text6change As String
Dim a As String
Dim j As Integer
Private Sub Form_Load()
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False
End If
Form1.Picture = LoadPicture("")
BSE1.SchemeStyle = 0
BSE1.EndSubClassing
BSE1.InitSubClassing
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
MSComm1.Settings = "2400,N,8,1" '2400波特率,无校验,8位数据位,1位停止位
MSComm1.RThreshold = 1 '设置 Rthreshold 为 1,接收缓冲区收到每一个字符都会使 MSComm 控件产生 OnComm 事件。

为0不产生MSComm1.SThreshold = 0 '设置 Sthreshold 属性为 1,当传输缓冲区完全空时,MSComm 控件产生 OnComm 事件。

为0不产生
MSComm1.InputLen = 0 '设置并返回 Input 属性从接收缓冲区读取的字符数. 为 0 时,使用 Input 将使 MSComm 控件读取接收缓冲区中全部的内容。

MSComm1.InputMode = comInputModeText 'comInputModeText 0 通过 Input属性以文本方式取回数据。

/comInputModeBinary 1 通过 In put 属性以二进制方式检取回数据。

MSComm1.InBufferCount = 0 ' 是指调制解调器已接收,并在接收缓冲区等待被取走的字符数。

可以把InBufferCount 属性设置为 0 来清除接收缓冲区。

End Sub
Private Sub Combo1_Click() '串口选择按键
Dim msg As String
Dim x As String
Dim Title As String
If Command3.Caption = "关闭串口" Then
msg = "请先关闭串口再重新选择" '错误显示
Title = "串口调试助手"
x = MsgBox(msg, 48, Title) '48标示显示警告图标
Combo1.Text = "COM" & a
Else
mPort = Combo1.ListIndex + 1 '读取com口号
a = Combo1.ListIndex + 1
End If
End Sub
Private Sub Command10_Click() '清空计数按键
Label4.Caption = "0"
Label5.Caption = "0"
End Sub
Private Sub Command3_Click() '打开串口按键
Dim msg As String
Dim x As String
Dim Title As String
On Error GoTo uerror '发现错误跳转到错误处理
If Command3.Caption = "关闭串口" Then
MSComm1.PortOpen = False
Command3.Caption = "打开串口" '按钮文字改变
Label7.Caption = "关闭"
Shape1.FillColor = &HFFFFFF
Else
MSComm1.PortOpen = True
Command3.Caption = "关闭串口"
Shape1.FillColor = &HFFFFC0
Label7.Caption = Combo1.Text + "串口打开中..."
End If
Exit Sub
uerror:
msg = "没有发现此串口或其已被占用" '错误显示
Title = "串口调试助手"
x = MsgBox(msg, 48, Title) '48标示显示警告图标
End Sub
Private Sub Command5_Click() '清空接收区按键
Text1.Text = ""
End Sub
Private Sub Command6_Click() '清空发送区按键
Text10.Text = ""
End Sub
Private Sub Command7_Click() '发送按键
Dim msg As String
Dim x As String
Dim Title As String
If Command3.Caption = "打开串口" Then
msg = "请先打开串口" '错误显示
Title = "串口调试助手"
x = MsgBox(msg, 48, Title) '48标示显示警告图标
Else
Label5.Caption = Trim(Label5.Caption) + Len(Text10.Text) '发送区计数MSComm1.Output = Trim(Text10.Text) '将发送区内容输出给下位机
End If
End Sub
Private Sub Command8_Click() '退出按键
Unload Me
End Sub
Private Sub MSComm1_OnComm()
Dim strData As String
Dim rec As String
Dim rec2 As String
Dim rec3 As Double
Dim k As Double
Dim BytReceived() As Byte
Dim strBuff As String
Dim i As Integer
Select Case mEvent
Case comEvReceive
MSComm1.InputLen = 0 '读入缓冲区全部内容
strBuff = MSComm1.Input '读入到缓冲区
Text1 = strBuff & " " & Text1 '显示接收的内容到text1
Label4.Caption = Len(strBuff) + Trim(Label4.Caption) '接收区计数 MSComm1.InBufferCount = 0
End Select
End Sub。

相关文档
最新文档