如何使用 SerialPort SerialDataReceived 帮助
不确定如何处理 SerialPort DataReceived。 场景
我有一个与设备通信的应用程序,并且该设备返回一个状态。这发生在不同的阶段 EG
public enum ActionState { 开始了, 进行中, 完全的 ETC... 现在,
如果我要使用 DataReceivedEventHandler,我如何知道正在执行什么方法?例如 Action1 或 Action2 等...? 我还想在从设备取回内容时加入某种超时。 有什么例子或建议吗?
public ActionState Action1
{
serialPort.write(myData);
string result=serialPort.ReadExisting());
//convertTo ActionState and return
return ConvertToActionState(result);
}
public ActionState Action2
{
serialPort.write(myData);
string result=serialPort.ReadExisting());
//convertTo ActionState and return
return ConvertToActionState(result);
}
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
//How can I use this to detect which method is firing and set the actionState Enum accordingly?
}
private ActionState(string result)
{
if(result==1)
return ActionState.Started;
else if (result==2)
return ActionState.Completed
etc...
}
Not sure how to handle SerialPort DataReceived.
Scenario
I have an application that communicate with a device and this device returns a status .This happens in different stages EG
public enum ActionState
{
Started,
InProgress,
Completed
etc...
}
Now if I were to use the DataReceivedEventHandler how can I tell what Method is executing? eg Action1 or Action2 etc...?
I also want to include some sort of timeout when getting back stuff from device.
Any example or advice?
public ActionState Action1
{
serialPort.write(myData);
string result=serialPort.ReadExisting());
//convertTo ActionState and return
return ConvertToActionState(result);
}
public ActionState Action2
{
serialPort.write(myData);
string result=serialPort.ReadExisting());
//convertTo ActionState and return
return ConvertToActionState(result);
}
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
//How can I use this to detect which method is firing and set the actionState Enum accordingly?
}
private ActionState(string result)
{
if(result==1)
return ActionState.Started;
else if (result==2)
return ActionState.Completed
etc...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当有数据需要读取时,即使接收到 Eof 字符,串口也会触发 DataReceived 事件。通常,此事件用于获取该数据并使用它启动进程或存储。例如,串行端口读取条形码并在数据库中找到它。
The event DataReceived is trigger by the serialport when have some data to read, even Eof character is received. Tipicaly this event is used to get that data and start a process with it or store. For example, the serialport read a barcode and find it on the database.