我目前正在开发一个项目,我正在使用 Visual Studios 2010 c# 中的SerialPort库从定制的 PCB 上读取数据。
我目前正在尝试通过单击按钮从设备读取作为数据字节提供给我的某种类型的数据,如果设备没有响应,则在超时前等待五秒钟。
但是由于某种原因,计时器出现故障,并且经常在五秒钟之前到期,从而停止存储有效数据。以下是我使用的代码,有人可以指出我的缺陷吗?我怀疑我使用的System.Windows.Forms.Timer中的事件计时器功能不正确。
按钮点击
bool flag = false;
private void btnGetData_Click(object sender, EventArgs e)
{
string command = "*data#";//make string command to send
sendCommand(command);
flag = true;
gui.Timer.Interval = timerlength;
gui.Timer.Enabled = true;
gui.Timer.Tick += new EventHandler(Timer_Tick);
}
定时器事件处理程序
private void readVarsTimer_Tick(object sender, EventArgs e)
{
if (flag == true)
{
flag = false;//set flag low
print("Data Timer Expired, Run Command Again.");
gui.Timer.Stop();//stop timer
gui.Timer.Enabled = false; //event over
timerlength = 5000;//reset timer
}
}
串口读取数据,请假设串口设置正确,因为任何不使用定时器的传输或接收数据都可以正常工作。
if (flag == true)
{
//code to read data in, and parse to display in text boxes correct
flag = false;
}
其他重要说明:
我可以从设备中获得三种类型的数据,因此我使用 if/else 和相应按钮设置的标志。因此,“标志”变量的全局声明。
我没有检测到串行端口获取可能让我失望的垃圾数据的问题,我已经运行并分析了足够多的数据以知道我得到了正确的预期数据。
当设备关闭时,这个计时器也是完美的,并且会持续整整五秒钟然后到期。