清除串行端口锁存数据事件 [英] Clearing a Serial Port Latch Data event
问题描述
大家好,
CodeProject上有一个串口大师的问题,我很确定我的问题是由硬件引起的,但我需要一个解决方法。我必须用Netbook替换老人的PSION Walkabout,我现在已经将所有的comms文档都设置了我的端口并且可以与硬件通信。我看来,一旦处理完数据,就不会清除缓冲区。我已经经历了
Hi All,
Questions are there a Serial Port Guru''s on the CodeProject,I pretty sure my issue is caused by the hardware but I need a work around. I have to replace an elderly PSION Walkabout with a Netbook, I now have all the comms documentation have set up my port and can talk to the hardware. I appears that a buffer is not being cleared of the data once it has been processed. I have gone through the angony of
//myComPort.DiscardInBuffer();
//myComPort.DiscardOutBuffer();
//rtbIncomingData.Text = "";
没有注释清除PC上的缓冲区,这似乎没有任何影响,我很确定它被锁存直到读取(这有意义)是否有串行端口类清除任何锁定的方法(徒劳的希望!)我可以在这里找到一些东西,因为PSION代码有几个无法记录的功能/例程,我看不到。我很高兴去MSDN!
格伦
uncommentted to clear the buffers on the PC, this appears to have no effect, I''m pretty sure it being latched until read (which would make sense) is there a Serial Port Class method to clear any latching (vain hope!) Could I be on to something here as the PSION code has several undocumented features / routines which I can''t see. Ah well of to MSDN I go!
Glenn
推荐答案
我知道这有点像cludgy方法,但它是唯一一个我必须有效地保持阅读代码,直到有一个锁定窗口的回复它由Write_Board(你好)调用;
将发送你好myComPort与\ n& \r计时器NoDataAtPort是一个有10秒超时的时间,如果设备已经死亡或被带走,它应该是合适的,
This I know is a bit of a cludgy method but it is the only one I have to effectivly hold the reading code until there is a reply with out locking up windows it is called by Write_Board("hello");
which will send hello out of myComPort with a \n & \r the timer NoDataAtPort is atimer with a 10 second time out which should be suitable if the device has died or been taken away,
private string Write_Board(string Data_To_Board)
{
Pause.Start();
myComPort.Write(Data_To_Board + (char)10 + (char)13);
{
while (Reply_Status == (int)REPLY.NO_REPLY)
{
NoDataAtPort.Enabled = true;
}
NoDataAtPort.Enabled = false;
if (Reply_Status == (int)REPLY.TIMEOUT_REPLY)
{
Data_From_Board = "TIMEOUT\n";
Pause.Stop();
}
else if (Reply_Status == (int)REPLY.YES_REPLY)
{
Pause.Stop();
//add try catch
try
{
InputData = myComPort.ReadExisting();
if (InputData != String.Empty)
{
this.BeginInvoke(new SetTextCallback(SetText), new object[] { InputData });
}
else if (InputData == String.Empty)
{
return (null);
}
}
catch (UnauthorizedAccessException)
{
MessageBox.Show("Error Caught!!" );
}
}
else
{
Cursor.Current = Cursors.No;
Reply_Status = (int)REPLY.NO_REPLY;
}
}
Data_From_Board = Data_From_Board + "|||";
return (Data_From_Board);
}
----------------
我也在使用另一个计时器来查看端口是否有任何数据也暂停是100毫秒超时的计时器(我开始怀疑我是否有毒性计时器综合症!)
----------------
----------------
also I am using another timer to see if there is any data at the port also Pause is a timer 100 ms time out (I''m starting to wonder if I have toxic timer syndrome!)
----------------
public int WaitForData(int Timeout)
{
Stopwatch sw = Stopwatch.StartNew();
sw.Start();
this.Invoke(new MethodInvoker(delegate()
{
rtbIncomingData.Text += ReplyData;
}));
while (sw.ElapsedMilliseconds < Timeout && myComPort.BytesToRead > 0)
{
Thread.Sleep(1); // sleep for 1 millisecond
}
int elapsed = (int)sw.ElapsedMilliseconds;
sw.Stop();
return elapsed;
}
这个方法是我多次成功使用的方法我在caffine燃料周末开发了它以防止数据回复缺少ReadExisting()被选为read方法(当我确定数据中有\\\
时,我已将它与ReadLine()一起使用)因为没有这个硬件的文档,PSION似乎剥离了所有内容但是在读取现有数据时,数据和我无法获得任何数据显示。
任何想法:
This method is one I have use multiple times with success I did developed it in a caffine fuel weekend to prevent bits of data replies going missing ReadExisting() was chosen as the read method (I have used it with ReadLine() when I was sure there was a \n in the data) as there is no documentation for this hardware and the PSION appears to strip everything but the data and I can''t get any data to appear using it while read existing I do.
Any ideas:
这篇关于清除串行端口锁存数据事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!