How to use SerialPort SerialDataReceived help
- by devnet247
Hi
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...
}