Theory: Make a function wait for an event or a timeout

Crionic

Member
Joined
Feb 1, 2007
Messages
7
Location
Norway
Programming Experience
1-3
Hi all,

I am in the process of redeveloping an application I once did in VB6.0. Back then I bought a component that accomplished the task I want to achieve in .NET now.

The application/module is quite simple, it reads SMS from a GSM device using serial port communication. I'll be using the SerialPort component available in VS2005, and this is how it works:
First I need to send a message to the device asking it if there are any messages in the inbox (I use AT commands for this), and herein lies the problem:
Since the communication is asynchronous I need to wait until the modem has replied with some data (text). Now I want to have a function in my component called i.e. GetMessages(), but how can I make the GetMessages() halt execution until data has been received on the serial port?
Can I i.e. use the DataReceived-event available from the serial port component, and what function do you suggest I use as a timeout check?

I am not looking for complete code from anyone here, but of course: If you have any pseudo-code I would be very grateful. If nothing else, if you will share your thoughts with me I am sure that it would be helpful as well!

Thanks in advance!
 
We dont have the function wait - we code on an event based model so that when data arrives and the relevant arrival event fires, we process on from there.. Let the notification of data be the start of your read-and-process code..
 
we code on an event based model so that when data arrives and the relevant arrival event fires, we process on from there
And herein lies my problem, I am having difficulties picturing how I should go about creating these events.
It could just as well be something like this:
Picture that I have two buttons and a textfield on a form, and I want to notify the user when both the buttons have been clicked. Problem is, it has to be a certain order, i.e. button A must be clicked before button B, and it needs to be able to do different things depending on what's written in the textfield.

In my mind the code should either fire from button A's click-event, or a third, independent object.

Difficult stuff this is, I've asked a few developers I've met at work as well, but they had no good answer on how to solve it... :)
 
I wouldnt expect you to raise those events... Surely they would be raised by the component youre using on the serial port?

You listen to the events, not raise them.. What events does the SerialPort component (part of the framework) support?


On the buttons note, we use a boolean toggle:

VB.NET:
ButtonA_CLick_Handler()
  buttonAClicked = true;

VB.NET:
ButtonB_CLick_Handler()
  If Not buttonAClicked Then
    MessageShow.SHow("Click button A First!")
  else
    DoProcess(TextField.Text)
    buttonAClicked = false
  endif
 
Ok, so maybe the buttons example was a bad one, it is unfortunately not that simple with serial communications (it seems).

The SerialPort has really just one event that I can use, this is DataReceived. This event is fired as soon as x number of bytes have been received on the input buffer. When the buffer contains data, I can read it, in chars, bytes, a line (until "end of line" character) or everything that exists in the buffer at once. Let's say that I i.e. want to check if the PIN code has been entered for the SIM card in the mobile phone/GSM modem. (Send/Receive):
VB.NET:
S:AT+CPIN?
R:AT+CPIN?
R:CPIN READY
R:
R:OK

To send the line I use serialport.WriteLine. Now, picture that I have a function called CheckPIN(), or even EnterPin(pin as String). These functions should send a command and return the message that the device gives me.
 
Back
Top