如何使用 SerialPort SerialDataReceived 帮助

发布于 2024-08-25 02:33:43 字数 1119 浏览 4 评论 0原文

不确定如何处理 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暮年 2024-09-01 02:33:43

当有数据需要读取时,即使接收到 Eof 字符,串口也会触发 DataReceived 事件。通常,此事件用于获取该数据并使用它启动进程或存储。例如,串行端口读取条形码并在数据库中找到它。

private static void DataReceivedHandler(
                    object sender,
                    SerialDataReceivedEventArgs e)
{
    SerialPort sp = (SerialPort)sender;
    string indata = sp.ReadExisting();
    if(!String.IsEmptyOrNull( indata)) 
    {
        AddProductToCart(indata);
    }
}

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.

private static void DataReceivedHandler(
                    object sender,
                    SerialDataReceivedEventArgs e)
{
    SerialPort sp = (SerialPort)sender;
    string indata = sp.ReadExisting();
    if(!String.IsEmptyOrNull( indata)) 
    {
        AddProductToCart(indata);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文