Mscomm 在 50ms 刷新的时候出错,高分寻求答案

enix 2005-11-03 05:20:50
在程序里加入了 timer ,让它50ms 重复执行,结果出现错误。

代码如下,请高手指点迷津

----------------------------------------------------------

Private Sub Command1_Click()
Unload Me
With Port_set.MSComm1
If .PortOpen = True Then
.PortOpen = False
End If
End With
End Sub

Private Sub Command2_Click()

With Port_set.MSComm1

Timer1.Enabled = True

Text9.Text = Now

'Add Net Recordset
Dim strsql As String
strsql = "select top 0 * from ad_temp"
Adodc1.CommandType = adCmdText
Adodc1.RecordSource = strsql
Adodc1.Refresh
Adodc1.Recordset.AddNew

Dim str As String

'Send #01(& vbkeyenter) to Comport
.Output = Chr(35) & Chr(48) & Chr(49) & Chr(13)
str = .Input
If Left(str, 1) <> "+" Or Left(str, 1) <> "-" Then
str = Right(str, 57)
Else
str = .Input
End If

'Read String per 7
Text1.Text = CVar(Left(str, 7))
Text2.Text = CVar(Right(Left(str, 14), 7))
Text3.Text = CVar(Right(Left(str, 21), 7))
Text4.Text = CVar(Right(Left(str, 28), 7))
Text5.Text = CVar(Right(Left(str, 35), 7))
Text6.Text = CVar(Right(Left(str, 42), 7))
Text7.Text = CVar(Right(Left(str, 49), 7))
Text8.Text = CVar(Right(Left(str, 56), 7))

'插入数据库
Adodc1.Recordset!channel1 = Text1.Text
Adodc1.Recordset!channel2 = Text2.Text
Adodc1.Recordset!channel3 = Text3.Text
Adodc1.Recordset!channel4 = Text4.Text
Adodc1.Recordset!channel5 = Text5.Text
Adodc1.Recordset!channel6 = Text6.Text
Adodc1.Recordset!channel7 = Text7.Text
Adodc1.Recordset!channel8 = Text8.Text
Adodc1.Recordset!date_time = Text9.Text
'Adodc1.Recordset!ad_time = Text10.Text

Adodc1.Recordset.Update

End With

End Sub

Private Sub Form_Load()
With Port_set.MSComm1

.InBufferSize = 512
.InputLen = 0
.PortOpen = True
.Output = Chr(35) & Chr(48) & Chr(49) & Chr(13)
'Text9.Text = .CommPort
Text9.Text = Now
'Text10.Text = Time
End With
End Sub

Private Sub Form_Unload(Cancel As Integer)

With Port_set.MSComm1
If .PortOpen = True Then

.PortOpen = False
End If

End With

End Sub

Private Sub Timer1_Timer()

Timer1.Enabled = True
Timer1.Interval = 50


With Port_set.MSComm1

If .PortOpen = True Then

Timer1.Enabled = True

Text9.Text = Now

'Add Net Recordset
Dim strsql As String
strsql = "select top 0 * from ad_temp"
Adodc1.CommandType = adCmdText
Adodc1.RecordSource = strsql
Adodc1.Refresh
Adodc1.Recordset.AddNew

Dim str As String

'Send #01(& enter) to Comport
.Output = Chr(35) & Chr(48) & Chr(49) & Chr(13)
str = .Input
If Left(str, 1) <> "+" Or Left(str, 1) <> "-" Then
str = Right(str, 57)
Else
str = .Input
End If

'每七位作为一个频道
'数据 +0.0001+0.0002+0.0011-0.0012+0.0008+0.0011-0.0011+0.0007
Text1.Text = CVar(Left(str, 7))
Text2.Text = CVar(Right(Left(str, 14), 7))
Text3.Text = CVar(Right(Left(str, 21), 7))
Text4.Text = CVar(Right(Left(str, 28), 7))
Text5.Text = CVar(Right(Left(str, 35), 7))
Text6.Text = CVar(Right(Left(str, 42), 7))
Text7.Text = CVar(Right(Left(str, 49), 7))
Text8.Text = CVar(Right(Left(str, 56), 7))

'Insert into Database
Adodc1.Recordset!channel1 = Text1.Text
Adodc1.Recordset!channel2 = Text2.Text
Adodc1.Recordset!channel3 = Text3.Text
Adodc1.Recordset!channel4 = Text4.Text
Adodc1.Recordset!channel5 = Text5.Text
Adodc1.Recordset!channel6 = Text6.Text
Adodc1.Recordset!channel7 = Text7.Text
Adodc1.Recordset!channel8 = Text8.Text
Adodc1.Recordset!date_time = Text9.Text

Adodc1.Recordset.Update

End If

End With
End Sub
...全文
144 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhzhq800204 2005-11-08
  • 打赏
  • 举报
回复
如果是接收数据的话可以通过串口控件的OnComm事件接收数据。
squller 2005-11-08
  • 打赏
  • 举报
回复
timer 可以控制的时间但是精度确实有限,而且在你使用串口的时候,频繁采集数据会导致串口的异常错误,这也是为什么串口逐渐被淘汰的原因之一。
同意烂壳的3。
subzero 2005-11-07
  • 打赏
  • 举报
回复
1、添加记录不要用addnew方法,效率很低
2、为什么规定刷新率是20/s,是不是因为下位机每50ms发送一次数据?如果是那么最好还是基于事件来处理通信
3、鉴于效率问题,你可以这样处理,维护一个数据缓冲区,每次通信后刷新这个缓冲区,写数据库的动作可以延长一下时间,比如说用另一个timer,定3秒写一次数据库,只要注意数据冲突问题,应该还算是个比较好的解决办法
dt168 2005-11-04
  • 打赏
  • 举报
回复
应该是数据通讯返回的时间问题吧,不关Timer 的事,时间太短了吧 接收数据时是不是要延时一下
weiweiplay 2005-11-04
  • 打赏
  • 举报
回复
Private Sub Timer1_Timer()

Timer1.Enabled = false
.
.
.
.
Timer1.Enabled = true

end sub

估计还是Interval太短的问题
feiyun0112 2005-11-04
  • 打赏
  • 举报
回复
为什么这样写
Private Sub Timer1_Timer()

Timer1.Enabled = True
Timer1.Interval = 50


一般先Timer1.Enabled = false
最后再Timer1.Enabled = True
faysky2 2005-11-04
  • 打赏
  • 举报
回复
在为 Timer 控件编程时应考虑对 Interval 属性的几条限制:

如果应用程序或其它应用程序正在进行对系统要求很高的操作─ 例如长循环、高强度的计算或者正在访问驱动器、网络或端口─ 则应用程序定时器事件的间隔可能比 Interval 属性指定的间隔长。


间隔的取值可在 0 到 64,767 之间(包括这两个数值),这意味着即使是最长的间隔也不比一分钟长多少(大约 64.8 秒)。


间隔并不一定十分准确。要保证间隔准确,应在需要时才让定时器检查系统时钟,而不在内部追踪积聚的时间。


系统每秒生成 18 个时钟信号─ 所以即使用毫秒衡量 Interval 属性,间隔实际的精确度不会超过 18 分之一秒。
zou19820704 2005-11-04
  • 打赏
  • 举报
回复
大概是时钟最快也就是70秒吧
enix 2005-11-04
  • 打赏
  • 举报
回复
自己顶一下。

补充:在 100ms 的时候没问题,我昨天测试通过11.5小时。
70ms也没出问题,60ms有时有问题,有时候没问题。50ms肯定出问题!
但是要求是 50次/s 也就是说 时间属性最少是 20ms 才可以。

帮帮忙啊!

1,451

社区成员

发帖
与我相关
我的任务
社区描述
VB 控件
社区管理员
  • 控件
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧