How to use SerialPort SerialDataReceived help

Posted by devnet247 on Stack Overflow See other posts from Stack Overflow or by devnet247
Published on 2010-03-08T21:17:40Z Indexed on 2010/03/08 21:21 UTC
Read the original article Hit count: 449

Filed under:

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...

    }

© Stack Overflow or respective owner

Related posts about serial-port