If MSComm1.PortOpen = True Then Dim buffer As Variant Dim txt dim j txt = "58" Dim strsend As String Dim j As Integer ReDim sj(Len(txt) / 2 - 1) For j = 0 To Len(txt) - 1 Step 2 sj(j / 2) = Val("&H" & Mid(txt, j + 1, 2)) Next MSComm1.Output = sj
strData = "" Else MsgBox "请先打开串口", , "提示信息!!!" End If
你用这个程序试一下 If MSComm1.PortOpen = True Then Dim buffer As Variant Dim txt dim j txt = "58" Dim strsend As String Dim j As Integer ReDim sj(Len(txt) / 2 - 1) For j = 0 To Len(txt) - 1 Step 2 sj(j / 2) = Val("&H" & Mid(txt, j + 1, 2)) Next MSComm1.Output = sj
strData = "" Else MsgBox "请先打开串口", , "提示信息!!!" End If
MSComm1_OnComm()的事件如下:不知道你是否可以看的懂。看不懂可以问一下 Private Sub MSComm1_OnComm() Dim indata As Variant Dim bytInput() As Byte Dim intInputLen As Integer Dim i As Integer Select Case MSComm1.CommEvent Case comEvReceive '...有接受事件发生 '此处添加处理接收的代码 MSComm1.InputMode = comInputModeBinary '二进制接收
intInputLen = MSComm1.InBufferCount ReDim bytInput(intInputLen) bytInput = MSComm1.Input 'jieshou For i = 0 To UBound(bytInput) If Len(Hex(bytInput(i))) = 1 Then strData = strData & "0" & Hex(bytInput(i)) Else strData = strData & Hex(bytInput(i)) End If Next Text2.Text = strData End Select MSComm1.InBufferCount = 0 MSComm1.OutBufferCount = 0End Sub
这个程序我试了,单片机还是没收到。我自己做了一个自检测的小程序(如下),发送"X"后,input中仍然为空,这是为什么?我现在实在是想不明白了。 Dim OutByte As String Dim InByte As String'串口初始化 Private Sub Form_Load() MSComm1.CommPort = 1 '设置通信端口号为COM1 MSComm1.Settings = "9600,N,8,1" '设置串口参数 MSComm1.InputMode = 0 MSComm1.PortOpen = True End Sub'打开通信端口 '把字符通过串口发送出去 Private Sub Cmdsend_Click() Dim Buf$ If MSComm1.PortOpen = False Then MSComm1.PortOpen = True OutByte = "X" '转换字符串 Buf = MSComm1.Input '清空输入缓冲区 MSComm1.Output = OutByte End SubPrivate Sub CmdReceive_Click() Timer1.Enabled = False InByte = MSComm1.Input TextReceive.Text = InByte 'End If End Sub
If MSComm1.PortOpen = True Then
Dim buffer As Variant
Dim txt
dim j
txt = "58" Dim strsend As String
Dim j As Integer ReDim sj(Len(txt) / 2 - 1) For j = 0 To Len(txt) - 1 Step 2 sj(j / 2) = Val("&H" & Mid(txt, j + 1, 2)) Next MSComm1.Output = sj
strData = ""
Else
MsgBox "请先打开串口", , "提示信息!!!"
End If
If MSComm1.PortOpen = True Then
Dim buffer As Variant
Dim txt
dim j
txt = "58" Dim strsend As String
Dim j As Integer ReDim sj(Len(txt) / 2 - 1) For j = 0 To Len(txt) - 1 Step 2 sj(j / 2) = Val("&H" & Mid(txt, j + 1, 2)) Next MSComm1.Output = sj
strData = ""
Else
MsgBox "请先打开串口", , "提示信息!!!"
End If
Dim indata As Variant Dim bytInput() As Byte Dim intInputLen As Integer Dim i As Integer Select Case MSComm1.CommEvent Case comEvReceive '...有接受事件发生 '此处添加处理接收的代码 MSComm1.InputMode = comInputModeBinary '二进制接收
intInputLen = MSComm1.InBufferCount ReDim bytInput(intInputLen) bytInput = MSComm1.Input 'jieshou For i = 0 To UBound(bytInput) If Len(Hex(bytInput(i))) = 1 Then strData = strData & "0" & Hex(bytInput(i)) Else strData = strData & Hex(bytInput(i)) End If Next Text2.Text = strData End Select MSComm1.InBufferCount = 0 MSComm1.OutBufferCount = 0End Sub
Dim OutByte As String
Dim InByte As String'串口初始化
Private Sub Form_Load()
MSComm1.CommPort = 1 '设置通信端口号为COM1
MSComm1.Settings = "9600,N,8,1" '设置串口参数
MSComm1.InputMode = 0
MSComm1.PortOpen = True
End Sub'打开通信端口
'把字符通过串口发送出去
Private Sub Cmdsend_Click()
Dim Buf$
If MSComm1.PortOpen = False Then MSComm1.PortOpen = True
OutByte = "X" '转换字符串
Buf = MSComm1.Input '清空输入缓冲区
MSComm1.Output = OutByte
End SubPrivate Sub CmdReceive_Click()
Timer1.Enabled = False
InByte = MSComm1.Input
TextReceive.Text = InByte
'End If
End Sub
我现在要把单片机发给我的信息保存到txt文件中,但是运行时提示两个编译错误:
1、过程太大
2、end select无select