| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 451 人关注过本帖, 1 人收藏
标题:VB编写的上位机与STC单片机通信
只看楼主 加入收藏
实现梦想123
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2015-10-6
结帖率:0
收藏(1)
已结贴  问题点数:20 回复次数:1 
VB编写的上位机与STC单片机通信
Private Sub Form_Load()
MSComm1.Settings = "9600,n,8,1"
= 1
MSComm1.InputLen = 0
MSComm1.InBufferSize = 1024
MSComm1.InBufferCount = 0
MSComm1.OutBufferSize = 1024
MSComm1.OutBufferCount = 0
MSComm1.RThreshold = 1
MSComm1.SThreshold = 1
MSComm1.PortOpen = True
End Sub
Private Sub Text1_Click()
Text1.Text = ""
End Sub
Private Sub Command1_Click()
Dim outputLen As Integer
Dim outData As String
Dim SendData() As Byte
Dim DataSave As String
Dim datacount As Integer
Dim i, x As Integer
outData = UCase(Replace(Text1.Text, Space(1), Space(0)))
outData = UCase(outData)
outputLen = Len(outData)
For i = 0 To outputLen Step 1
DataSave = Mid(outData, i + 1, 1)
If (Asc(DataSave) >= 48 And Asc(DataSave) <= 57) Or (Asc(DataSave) >= 65 And Asc(DataSave) <= 70) Then
 datacount = datacount + 1
 Else

 Exit For
 Exit Sub
End If
 Next i
 If datacount Mod 2 <> 0 Then
 datacount = datacount - 1
 End If
 outData = Left(outData, datacount)
 ReDim SendData(datacount / 2 - 1)
For x = 0 To datacount / 2 - 1
SendData(x) = Val("&H" + Mid(outData, i * 2 + 1, 2))
Next x
MSComm.Output = SendData
End Sub
Private Sub MSComm1_OnComm()
Select Case
  Case comEvReceive
Dim receive() As Byte
Dim receivedata As String
Dim counter As Integer
Dim i As Integer
If (MSComm1.InBufferCount > 0) Then
counter = MSComm1.InBufferCount
receivedata = ""
receive = MSComm1.Input
For i = 0 To (counter - 1) Step 1
If (receive < 16) Then
receivedata = receivedata & "0" + Hex(receive(i)) & Space(1)
Else
receivedata = receivedata & Hex(receive(i)) & Space(1)
End If
Next i
Text2.Text = Text2.Text + receivedata
Case Else
  End Select
End Sub
Private Sub Command2_Click()
MSComm1.PortOpen = False
Unload Me

End Sub



麻烦大神帮我看看,现在我调试到If (Asc(DataSave) >= 48 And Asc(DataSave) <= 57) Or (Asc(DataSave) >= 65 And Asc(DataSave) <= 70) Then时提示说实时错误5无效的过程调用或参数,请问这是怎么回事?
搜索更多相关主题的帖子: 单片机 通信 
2015-10-06 10:07
xiangyue0510
Rank: 14Rank: 14Rank: 14Rank: 14
等 级:贵宾
威 望:86
帖 子:938
专家分:5244
注 册:2015-8-10
收藏
得分:20 
outputLen = Len(outData)
For i = 0 To outputLen Step 1
DataSave = Mid(outData, i + 1, 1)

我看着这几句有点问题,i = outputLen(字符长度)的时候 DataSave = Mid(outData, i + 1, 1)就有问题了。你自己检查一下
2015-10-07 11:37
快速回复:VB编写的上位机与STC单片机通信
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.038373 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved