本人刚学VB,在写一个串口数据采集大的小软件,刚开始就遇到了问题,以下代码,我在调试时都正常运行,能正确获得返回数据,可是生成exe文件后,程序却没反应,也没有错误信息出现!就是没有数据,求高手帮忙!
Public gSend As Boolean 'Global used for Send button state
Dim strReturn As String '定义strReturn为返回值变量
Dim strHold As String '中间变量
Dim Term As String '终止符
Dim ZeroCount As Integer '超时计数器
Dim strCommand As String '发往仪器的字符串
Private Sub Cmdexit_Click()
End
End Sub
Private Sub CmdReady_Click()
Frmserial.Show
Term = Chr(13) & Chr(10) '终止符为
ZeroCount = 0 '初始化计数器
strReturn = "" '初始化返回值
strHold = "" '初始化中间变量
If Frmserial.MSComm1.PortOpen = True Then '关闭端口
Frmserial.MSComm1.PortOpen = False
End If
Frmserial.MSComm1.CommPort = 5 '选择端口
Frmserial.MSComm1.Settings = "1200,o,7,1" '通信参数
Frmserial.MSComm1.InputLen = 1 '每次读取字符数为1个字符
Frmserial.MSComm1.PortOpen = True '打开端口
End Sub
Private Sub Cmdstart_Click()
strCommand = txtCommand.Text
If strCommand = "" Then
MsgBox "请输入命令", vbOKCancel, "警告"
End If
Timer1.Enabled = True
End Sub
Private Sub Cmdstop_Click()
Timer1.Enabled = False
End Sub
Private Sub Timer1_Timer()
MSComm1.Output = strCommand & Term '向仪器发送指令
If InStr(strCommand, "?") <> 0 Then '若指令中有?则执行以下指令接收仪器返的数据
While (ZeroCount < 20) And (strHold <> Chr$(10)) '20微秒内返回数据,在未接收到终止符之前重复接受数据
If Frmserial.MSComm1.InBufferCount = 0 Then '若超时则加1
Frmserial.Timer2.Enabled = True
Do
DoEvents '等待10毫秒
Loop Until Frmserial.Timer2.Enabled = False
ZeroCount = ZeroCount + 1 '超时加1
Else
ZeroCount = 0 '重设超时计时器
strHold = Frmserial.MSComm1.Input '读取一位
strReturn = strReturn + strHold '把读取的字符添加在字符串中
End If
Wend '读取字符直到结束字符(LF)
If strReturn <> "" Then '检查接收字符串是否为空
strReturn = Mid(strReturn, 1, InStr(strReturn, Term) - 1) '剥去末位字符即结束符
Else
strReturn = "No Response" '若接收字符串为空显示无响应
End If
Labtemp.Caption = strReturn
Frmserial.txtResponse.Text = strReturn '把返回值付给txtresponse显示出来
strHold = "" '重设临时变量strHold
ZeroCount = 0 '重设计数器