文件名称:vb 串口调试 经调试直接可用
文件大小:7KB
文件格式:ZIP
更新时间:2014-05-28 03:20:12
vb串口调试
Private Sub clear_receivetext_Click() receivetext.Text = "" End Sub Private Sub clear_sendtext_Click() sendtext.Text = "" End Sub Private Sub close_Click() MSComm1.PortOpen = False '关闭串口 End Sub Private Sub Form_Load() MSComm1.CommPort = 4 '使用串行端口1 MSComm1.Settings = "9600,N,8,1" '设置波特率等 MSComm1.InputLen = 0 '清空从接收缓存器中读到的字符 MSComm1.PortOpen = True '打开端口 '这是整个的初始化过程 End Sub Private Sub open_Click() MSComm1.CommPort = 4 '使用串行端口1 MSComm1.Settings = "9600,N,8,1" '设置波特率等 MSComm1.InputLen = 0 '清空从接收缓存器中读到的字符 MSComm1.PortOpen = True '打开端口 End Sub Private Sub receive_Click() Dim strBuff As String Dim str() As Byte strBuff = MSComm1.Input str() = strBuff '接收数据处理为16进制 For i = 0 To UBound(str) If Len(Hex(str(i))) = 1 Then strData = strData & "0" & Hex(str(i)) Else strData = strData & Hex(str(i)) End If Next receivetext.Text = LCase(strData) '转换成小写输出 'dat = MSComm1.Input ' receivetext.Text = " q" 实现将接受到的数据以文本形式写到接收部分的窗体内这是核心代码。 End Sub Private Sub send_Click() On Error Resume Next '简单的错误处理 If MSComm1.PortOpen = False Then MsgBox "串口已关闭" End If Dim a() As Byte Dim l As Integer, s As String s = sendtext.Text l = Len(s) / 2 If l < 1 Then Exit Sub ReDim a(1 To l) 'For I = 1 To l a(1) = ConvertHexChr(Mid(s, 1, 1)) * 16 + ConvertHexChr(Mid(s, 2, 1)) Print a(1) 'Next I MSComm1.Output = a MSComm1.InputMode = comInputModeBinary ' '设置接受模式是文本接受,还是二进制接受 End Sub '********************************** '字符表示的十六进制数转化为相应的整数 '错误则返回 -1 '********************************** Function ConvertHexChr(str As String) As Integer Dim test As Integer test = Asc(str) If test >= Asc("0") And test <= Asc("9") Then test = test - Asc("0") ElseIf test >= Asc("a") And test <= Asc("f") Then test = test - Asc("a") + 10 ElseIf test >= Asc("A") And test <= Asc("F") Then test = test - Asc("A") + 10 Else test = -1 '出错信息 End If ConvertHexChr = test End Function
【文件预览】:
Product2
----Product.vbp(784B)
----Product.frm(6KB)
----Product.vbw(50B)
----Product2.exe(24KB)