Click here to Skip to main content
15,881,651 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?
Posted
Comments
Samuel Cherinet 11-Sep-10 22:10pm    
The EventHandler is not a guarantee that it will be run in a separate thread. its just calling on pointer to the function(delegates). please post some code about your problem.

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);
}


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
 
Share this answer
 
v2
Comments
Dalek Dave 13-Sep-10 3:41am    
Good Call.
Hi,

You ca use
Thread.Join
method to wait for the main thread to complete and then run the second thread
 
Share this answer
 
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.
 
Share this answer
 
v2
Hi,

I don't know how to control threading in events but i think you are "barking up the wrong tree". it appears what you are trying to do is force the serial port class to be synchronous but using the asynchronous functionality like events...

In other words, if you always want to wait for data before continuing with other operations, I suggest you imeplement a synchronous architecture.

So what this means is using things like the SerialPort BytesToReasd property to determine if there is data in the buffer and then read it.

I hope this sort of makes some sense. I can provide code if you give more information about what you are actually trying to acheive :)

Dave
 
Share this answer
 
Thank you Dave,
So basically you are saying I should create my own event and implement the synchronization in that event? I can do that. :) I was just trying to avoid rebuilding the wheel again. :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900