等待OK或>的正确方法是什么?从董事会 [英] What is the correct method of waiting for an OK or a > from a board
问题描述
大家好,
这与前不久在
中提出的一个问题有关
a for loop not loop not looping [ ^ ]
我正在使用WaitForData例程,但需要对其进行修改
就像
Hi All,
This related to a question asked a little while ago in
a for loop not loop not looping[^]
I am using the WaitForData routine but need to modify it
so as the
while (timeWaited < Timeout && myComPort.BytesToRead < NbDataToWait)
用作
时
which when used as
Value = WaitForData(4, 100);
将等待4字节的数据并超时100毫秒,我希望它等待>回到董事会已经看到了命令.更好的方法(更快)是等待董事会通过,如果在按钮中单击下面的演示会更明智,则单击
will wait for 4 Bytes of data and time out 100 mS, I would like it to wait a > coming back that the board has seen the command. A better way (faster) would be to wait for an OK from the board would it be more sensible method be below demoed in a button click
<pre lang="cs">private void button1_Click(object sender, EventArgs e)
{
string TextContaing;
int LocationOf;
TextContaing = textBox1.Text;
MessageBox.Show(TextContaing);
LocationOf = TextContaing.IndexOf(">");
if (LocationOf > 0)
{
MessageBox.Show("Got One! "+LocationOf.ToString());
}
}
等待OK或>的正确方法是什么?来自董事会
What is the correct method of waiting for an OK or a > from the board
推荐答案
嗨.
你可以试试看.它应该读取COM3上的所有数据并返回值.
此后,这是检查>"的简单情况.角色.
不幸的是,没有任何更多信息,我无法提供更准确的解决方案.
Hi.
You could try this. It should read all the data on COM3 and return the values.
Thereafter it is a simple case of checking for the ">" character.
Unfortunately without any further information i cannot provide a more accurate solution.
static bool WaitForData(int NbDataToWait, int Timeout)
{
bool Result = false;
int TimeWaited = 0;
System.IO.Ports.SerialPort SPort = new System.IO.Ports.SerialPort();
SPort.PortName = "COM3";
SPort.Open();
byte[] MyResult = new byte[SPort.BytesToRead];
SPort.Read(MyResult, 0, SPort.BytesToRead);
while (TimeWaited < Timeout && SPort.BytesToRead < NbDataToWait)
{
for (int i = 0; i < MyResult.Length; i++)
{
if (MyResult[i].ToString() == ">")
{
Result = true;
}
}
TimeWaited++;
}
return Result;
}
这篇关于等待OK或>的正确方法是什么?从董事会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!