我昨天去接一个仪器,仪器是用来测量直径的,
当往com口发送字母 “f“后,仪器会往com口
发送大量的数据。
例如: ”12.034 12.056。。“
我写了一段程序是从com口读取这个数据串,并且把
数据分离出来。(com口数据是时刻在变的)Private Sub Command13_Click()
com.CommPort = 1 '假定为com1口
com.Settings = "4800,n,8,1" '设置参数
com.OutBufferCount = 0 '清空输出寄存器
com.InBufferCount = 0 '清空输入寄存器
com.InputMode = comInputModeText '设置接收数据格式为文本
com.PortOpen = True '打开com口
com.Output = "f"
End Sub
private Sub Timer1_timer()
dim value_temp ,data as string
if com.input<>"" then
value_temp=com.input
............(分离程序)
End If
End Sub程序在 if com .....处设置断点,单句执行,程序表现正常。
但是如果全启动的话,读出内容始终为0。
不知道是否变量定义错了,因为com口确实有数据,但是value_temp
却为空。请帮助!!!
当往com口发送字母 “f“后,仪器会往com口
发送大量的数据。
例如: ”12.034 12.056。。“
我写了一段程序是从com口读取这个数据串,并且把
数据分离出来。(com口数据是时刻在变的)Private Sub Command13_Click()
com.CommPort = 1 '假定为com1口
com.Settings = "4800,n,8,1" '设置参数
com.OutBufferCount = 0 '清空输出寄存器
com.InBufferCount = 0 '清空输入寄存器
com.InputMode = comInputModeText '设置接收数据格式为文本
com.PortOpen = True '打开com口
com.Output = "f"
End Sub
private Sub Timer1_timer()
dim value_temp ,data as string
if com.input<>"" then
value_temp=com.input
............(分离程序)
End If
End Sub程序在 if com .....处设置断点,单句执行,程序表现正常。
但是如果全启动的话,读出内容始终为0。
不知道是否变量定义错了,因为com口确实有数据,但是value_temp
却为空。请帮助!!!
解决方案 »
- 程序中有几个窗体,怎么使其中一个窗体关闭,程序关闭了
- 我的这段代码运行的结果不太对
- VB Textbox Common Problem:Textbox返回第N行的中文文字
- 奇怪问题!!想不明白呀!!请大虾帮忙!!
- 新手提问
- 大家帮忙,很菜的问题
- 删除字符串的问题!各位高手帮忙
- 如何在VB中把当前工程加入指定SOURCESAFE数据库中?
- 如何用ScriptControl编写动态调试程序?
- 求救--为了装vs7.net各位大虾,救命阿!!!(StrayBear)
- 有没有人告诉我一下:关于listbox控件的问题,在线等待请求帮助?急急急
- VB的日期控件没有找到,只有microsoft windows common controls 2 6.0(sp4)
If MSComm1.CommEvent = comEvReceive Then
value_temp=com.input
............(分离程序)
End If
End Sub
select case MSComm1.CommEvent
case comEvReceive
''''''''end select
private Sub Timer1_timer()
dim value_temp ,data as string value_temp=com.input '串口读一次后就清空缓冲区了
if value_temp<>"" then
............(分离程序)
End If
End Sub
select case MSComm1.CommEvent
case comEvReceive
''''''''end select同意上面的方法,比较简单
不过你数据量多的时候,进onComm事件后最好延迟一段时间后再读
测试上面的ONcomm事件,但是好像不发生。
Private Sub Command13_Click()
dim i as integer
com.CommPort = 1 '假定为com1口
com.Settings = "4800,n,8,1" '设置参数
com.OutBufferCount = 0 '清空输出寄存器
com.InBufferCount = 0 '清空输入寄存器
com.InputMode = comInputModeText '设置接收数据格式为文本
com.PortOpen = True '打开com口
for i=1 to 1000
com.Output = i
next
End Sub
Private Sub MSComm_OnComm ()
If MSComm1.CommEvent = comEvReceive Then
value_temp=com.input
............(分离程序)
End If
End Sub
dim i as integer
com.CommPort = 1 '假定为com1口
com.Settings = "4800,n,8,1" '设置参数
com.OutBufferCount = 0 '清空输出寄存器
com.InBufferCount = 0 '清空输入寄存器
com.InputMode = comInputModeText '设置接收数据格式为文本
com.RThreshold = 1'这个设定有信息就触发
com.PortOpen = True '打开com口
for i=1 to 1000
com.Output = i
next
End Sub
Private Sub MSComm_OnComm ()
If MSComm1.CommEvent = comEvReceive Then
value_temp=com.input
............(分离程序)
End If
End Sub
如果com口里面数据是12的时候,再读取的时候会把1和2分开。这样就要
用程序去判断。
dim value_temp ,data as string
if com.input<>"" then <--- com每次读取会清空缓冲
value_temp=com.input <--- 这里再次读取就为空了
............(分离程序)
End If
End Sub改为:
private Sub Timer1_timer()
dim value_temp as string
dim data as string
value_temp=com.input
if value_temp<>"" then
............(分离程序)
End If
End Sub