Threading.Timer和从SerialPort读取问题 [英] Threading.Timer and read from SerialPort problem

查看:129
本文介绍了Threading.Timer和从SerialPort读取问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我的应用程序每隔x毫秒从一个串行端口(MyDataReader)读取一些数据并将其显示在控件上,MyDataReader也可以是一个文件,我可以从该文件读取数据.
我使用了一个简单的计时器,每次滴答时,我都已从串行端口/文件中读取数据,对其进行处理并显示出来,效果很好.

现在,我添加了另一个需要从中读取数据的设备,并切换到System.Threading.Timer,但是现在我的滴答"功能无法正常工作,当它从串行端口读取时,我得到了零.当我进入调试模式并使用断点readfromserialport函数时,有时我会获得有效数据,有时却没有.
如果将读取从串行端口切换为从文件读取,则可以正常工作,我已经调用了所有显示数据的控件.
我锁定了来自串行端口的读/写:

Hello,
My application reads every x ms some data from a serial port(MyDataReader) and displays it on a control, MyDataReader also can be a file, from which I can read data.
I used a simple timer, every tick I''ve read the data from the serial port/file, processed it and displayed it, It worked great.

Now I''ve added another device from which I need to read data and I''ve switched to System.Threading.Timer, but now my "tick" function is not working properly, when it reads from the serial port, I get zeros. when I enter debug mode and breakpoint readfromserialport function, sometimes I get valid data, sometimes I don''t.
If I switch the reading from serialport to read from file it works fine, I''ve invoked all the controls that display data.
I locked the read/write from the serialport:

lock(this)
   {
   writetoserialport;
   readfromserialport;
   }


任何人都不知道为什么我总是一直为零,而当我断点时,我有时会有数据吗?
就像打开了从不同线程上的串行端口读取数据一样,我需要等待数据被读取.

谢谢
格列夫(Glev.)
[edit]已添加代码块-OriginalGriff [/edit]
[edit]添加了其他信息[/edit]
从OP的解决方案"中移出:
我正在寻找造成常规计时器和Threading.Timer之间差异的原因,当我使用常规计时器从端口读取数据时,一切正常,当我将其更改为Threading.Timer时,则没有数据. >
顺便说一句,为了从我的设备中获取数据,我需要编写一个请求,编写一个请求,并立即获得数据的响应.


anyone have any idea why I get zeros all the time, and when I breakpoint I have sometimes data?
It''s like it opens the read from serial port on different thread and I need to wait to the data to be read.

Thank you,
Glev.
[edit]Code block added - OriginalGriff[/edit]
[edit]Added additional information[/edit]
Moved from OP''s "solution":
I''m looking for the cause for the difference between regular timer and Threading.Timer, when I read data from the port with regular timer everything is ok, when I change it to Threading.Timer then I get no data.

BTW, in order to get data from my device I need to write a request, I write a request and immediately get response with data.

推荐答案

在实际使用中,很难弄清正在发生的事情.但是,将基于计时器的串行端口更改为基于事件可能是值得的:处理SerialPort.DataReceived事件,而不是每隔x ms进行一次.

File/SerialPort的差异可能是什么:串行端口上的不良物理环回,给编码问题,以为您假设串行数据始终存在":没有代码,就不可能准确!
Without seeing the code you are actually using, it is difficult to work out what is happening. However, it may be worth you changing from Timer based serial port to event based: Handle the SerialPort.DataReceived event instead of doing it every x ms.

The File/SerialPort difference could be anything: a bad physical loopback on the serial port, to code problems, to you assuming that serial data is "always there": without code, it is not possible to be accurate!


这篇关于Threading.Timer和从SerialPort读取问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆