串行事件等待 [英] Serial Event Wait

查看:77
本文介绍了串行事件等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个通过串行端口与gsm调制解调器通信的应用程序.我已经成功连接到它,并且能够发送AT命令并接收响应.我正在使用DataRecieved事件处理程序,该事件处理程序当然是与运行应用程序的主线程不同的线程.因此,这会造成问题,因为第二个at命令是在收到答案之前发送的.我的问题是:有没有办法让主线程在继续之前等待事件完成?

I''m writing an application that communicates through serial port to a gsm modem. I have successfully connected to it and was able to send an AT command and receive the response. I''m using the DataRecieved eventhandler which of course is a different thread than the main thread running the application. So, this creates a problem since the second at command is sent before the answer is received. My question is: Is there a way to make the main thread wait for the event to finish before continuing?

推荐答案

不,事件不是答案.

例如,假设您有一种初始化调制解调器的方法.作为初始化过程的一部分,要求调制解调器响应匿名呼叫,让呼叫为0x00.调制解调器响应后,您可以通过在串口上发送0x01来初始化其标识符.

No no, events are not the answer.

For example, lets say you have a method that initialises the modem. As part of the initialisation procedure, the modem is asked to respond to an anonymous call, lets make the call 0x00. Once the modem has responded, you then initialise its identifier by sending lets just say 0x01 down the serial port.

SerialPort commPort = new SerialPort(); // add port params

void InitialiseModem(string identifier)
{
  byte[] data = new byte[1];
  
  // send the call first
  data[0] = 0x00;
  this.comPort.Write(data, 0);

  // wait for a response from modem before continuing
  while(SerialPort.BytesToRead <= 0);

  // modem has responded (ignore response data). Send the next lot of data
  data[0] = 0x01;
  this.comPort.Write(data, 0);
}



这是一种同步方法,即它将一直阻塞,直到调制解调器响应为止,最糟糕的是,如果调制解调器从不响应,则必须实现一种机制以确保永远不会循环,这很容易用if来完成.语句和某种计数器或计时器.

为了避免锁定GUI(我不知道这是否是一个问题),您应该尝试在另一个线程中运行整个方法.对于同步操作,使用事件不是一个好主意,除非您可以遵循定义的协议,而该协议实际上是调制解调器固件固有的.

戴夫



This is a synchronous method, i.e. it will block until the modem responds, the bad part is that if the modem never responds, you must implemet a mechanism to make sure you don''t loop forever, it can easily be done with an if statement and a counter or timer of some sort.

To avoid locking up a GUI (I dont know if this is an issue) you should try and run the whole method inside another Thread. Using events is not a good idea for synchronous operations, unless there is a defined protocol that you can adhere to that is actually inherent to the modem firmware.

Dave




您可以使用
Hi,

You ca use
Thread.Join

方法等待主线程完成,然后运行第二个线程

method to wait for the main thread to complete and then run the second thread


感谢您的回复.我的问题是我希望主线程等待事件完成.

我做了一些额外的研究,并且我了解Eventhandler有一个单元线程.因此,基本上我的问题是如何使主线程在继续之前等待单元线程完成.请了解,事件处理程序不是我的,我使用的是现成的Microsoft.
Thank you for replying. My problem is I want the main thread to wait for the event to finish.

I''ve done some extra research and I understand that the Eventhandler has an apartment thread. So basically my question is how to make the main thread wait for the apartment thread to finish before continuing. Please understand that the eventhandler is not mine I''m using a ready microsoft made one.


这篇关于串行事件等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆