| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 809 人关注过本帖
标题:串口程序出错
只看楼主 加入收藏
crystal_2345
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2006-11-22
收藏
 问题点数:0 回复次数:2 
串口程序出错

下面是一个接收串口数据的程序,可是有时候接不到,帮我看看原因吧
Option Explicit
Dim atok As Integer
Private Sub Form_Load()
MSComm1.CommPort = 1

'如果串口关闭则将其打开
If MSComm1.PortOpen = False Then
MSComm1.Settings = "9600,n,8,1"
MSComm1.PortOpen = True
End If

'清空缓冲区
MSComm1.OutBufferCount = 0
MSComm1.InBufferCount = 0
atok = 0
'给列表框赋值
lst_at.AddItem "at"
lst_at.AddItem "at+creg?"
lst_at.AddItem "at+cmgf=0"
lst_at.AddItem "at+cmgs=19"
lst_at.AddItem "at+cmgl=0"
lst_at.AddItem "at+cmgr"
End Sub


Private Sub cmd_ok_Click()
Dim code() As Integer
Dim instring As String
Dim string1 As String
Dim length As Integer
Dim i As Integer
Dim temp As Integer
instring = txt_ad.Text
length = Len(instring)
ReDim code(length + 1)
For i = 1 To length '把手机号码按每位数字存入数组code
string1 = Left(instring, i)
code(i) = Right(string1, 1)
'Text2.Text = Text2.Text & code(i)
Next i
'Text2.Text = vbCrLf
'对手机号码编码
For i = 1 To length + 1 Step 2
If i = length Then
If length \ 2 Then
ReDim Preserve code(length + 2)
code(length + 1) = &HF
End If
End If
temp = code(i)
code(i) = code(i + 1)
code(i + 1) = temp
txt_send.Enabled = False
txt_send.Text = txt_send.Text & Hex(code(i)) & code(i + 1)
'Text2.Text = Text2.Text & Hex(code(i)) & code(i + 1)
Next i
txt_send.Enabled = True
txt_send.Text = "0891683108100005F011000D91" & txt_send.Text & "000800" & "046D4B8BD5"

End Sub

Private Sub cmd_send_Click()

'初始化设置
txt_sent.Enabled = True
MSComm1.InputMode = 0 '以二进制形式从输入缓冲区中读数据
MSComm1.InputLen = 0 '一次读出输入缓冲区中的所有数据
MSComm1.RThreshold = 1 '每接收到一个字符就引发一次Oncomm事件
'发送信息
If atok = 1 Then
MSComm1.Output = txt_send.Text & Chr(&H1A)
MSComm1.OutBufferCount = 0
txt_sent.Text = txt_sent.Text & txt_send.Text
atok = 0
txt_flag.Text = ""
Else
MSComm1.Output = txt_send.Text & vbCr
txt_sent.Text = txt_sent.Text & txt_send.Text & vbCrLf
End If

End Sub
Private Sub cmdclear1_Click()
txt_send.Text = ""
txt_send.SetFocus
End Sub
Private Sub cmdclear2_Click()
txt_ad.Text = ""
txt_sent.Text = ""
txt_receive.Text = ""
lst_at.Enabled = True
End Sub


Private Sub Command1_Click()
atok = 1
txt_flag.Text = ">"
atok = 1
txt_send.Text = ""
txt_ad.SetFocus
lst_at.Enabled = False
End Sub

Private Sub MSComm1_OnComm()
txt_receive.Enabled = True

'从缓冲区中读出接收到的字符并记录在文本中
If MSComm1.InBufferCount Then

txt_receive.Text = txt_receive.Text & MSComm1.Input '& vbCrLf

End If
End Sub

Private Sub txt_send_Change()
cmd_send.Enabled = True
End Sub

Private Sub cmd_atok_Click()
txt_send.Text = lst_at.Text
End Sub
Private Sub lst_at_Click()
cmd_atok.SetFocus
End Sub

Private Sub lst_at_DblClick()
cmd_atok_Click
End Sub

搜索更多相关主题的帖子: 串口 lst AddItem 
2006-12-26 22:31
学习VB才2天
Rank: 5Rank: 5
等 级:贵宾
威 望:16
帖 子:1653
专家分:0
注 册:2006-5-4
收藏
得分:0 
攷驗我們麼````錯誤齣現沒?在哪一行呢````

[GLOW=255,DeepPink,3]我的免费网盘[/GLOW]
2006-12-27 10:34
小李寻欢
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2006-12-27
收藏
得分:0 
其实这也没有什么奇怪的。你之所以采用二进制形式进行接受,无非就是数据中嵌有控制字符、Nulls或非ANSI字符集的字符等。因此,常规的做法是采用Byte类型的数组(可变)作为接受缓冲区(例如:aBuffer = MSComm1.Input即可),之后再对缓冲区进行进一步的处理。否则,一旦有Null字符(ASCII Code = 0)——除非将NullDiscard设置成True(但这么做的话有时并不一定符合实际使用的要求),TextBox将无法显示该字符及其之后的所有内容(想一想,为何C++中有这样的匈牙利命名法前缀:lpsz——以Zero结尾的字符串远指针)。希望对你的问题解决有所帮助。
2006-12-27 16:02
快速回复:串口程序出错
数据加载中...
 
   



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

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