//NOTE: not a working sample, code is for demonstration purposes only
class SampleMessageHandler
{
//Setup Thread wait / Mutex
EventWaitHandle ewh = new EventWaitHandler(false, EventResetMode.AutoReset);
public delegate void IncomingMessageDelegate(Frame incomingFrame);
Frame sentFrame;
public void StartSending()
{
while(isMessageInQueue)
{
sentFrame = SendNextFrame();
//Wait For Ack
ewh.WaitOne();
}
}
public void HandleIncomingFrame(Frame incomingFrame)
{
if(incomingFrame.isAckOf(sentFrame))
{
ewh.set();//signal the wait
}
else
{
doSomethingElse(incomingFrame);
}
}
public class SerialListener
{
public static StartSerialListener()
{
IncomingMessageDelegate imd1 = new IncomingMessageDelegate(HandleIncomingFrame);
openComPort();
startReceiving();
}
//Listener methods
// .
// .
// .
// .
public void onIncomingMessage(Frame f)
{
//
//
//
imd1(f);
}
}
}
class Test()
{
public static void Main()
{
SerialAckSmaple sas = new SerialAckSample();
sas.StartSerialListener();
sas.StartSending();
}
} |