楼上 发过了,我这里就 免了
不贵,推荐 书籍
Visual C++_Turbo C串口通信编程实践
已发送,请查收.
其中能执行的是\第1章 串口编程调试精灵\Debug\CommWizard.EXE
文件.
代码如下:
Private Sub Form_Load()
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False
Else
End If
dim x as integer
for x=1 to 16
combo1.additem ("COM" & cstr(x))
next
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"
MSComm1.CommPort = 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 MSComm1.CommEvent '事件发生
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
End If
End Sub
Private Sub ComClean1_Click()
TextSend.Text = "" '清空发送窗口
End Sub
Private Sub Option3_Click()
MSComm1.InputMode = comInputModeBinary '选择接收方式
End Sub
Private Sub Option4_Click()
MSComm1.InputMode = comInputModeText '选择接收方式
End Sub
Private Sub Timer1_Timer()
Call ComSend1_Click '定时调用手动发送
End Sub
Private Sub Timer2_Timer()
If Combo3 = "无None" Then
MSComm1.Settings = Str(Combo2) + "N" + Str(Combo4) + Str(Combo5)
ElseIf Combo3 = "奇Odd" Then
MSComm1.Settings = Str(Combo2) + "O" + Str(Combo4) + Str(Combo5)
ElseIf Combo3 = "偶Even" Then
MSComm1.Settings = Str(Combo2) + "E" + Str(Combo4) + Str(Combo5)
End If
End Sub
Private Sub Combo1_Click()
If MSComm1.PortOpen = True Then '如果串口打开先关闭后再进行其他操作
MSComm1.PortOpen = False
End If
MSComm1.CommPort = Combo1.ListIndex + 1 '读取com口号
End Sub
Private Sub ComClean3_Click()
Label10.Caption = 0
Label11.Caption = 0
End Sub
Private Sub ComClean2_Click()
TextReceive.Text = "" '接收窗口
End Sub