blah blah blah is here! blah blah » Close

up0down
link

Hi,

This is related to a previous post regauding Serial Port issues "Do I need a custom event handler or would a timer do?". It appears that the clunky way of doing serial comms that I am using there will work for a time and then gum up a thread and all sorts of random things start happening (PC reboots etc). I have been advised elsewhere to use the following code

public delegate void ReplyReadyEventHandler(string Reply);

public event ReplyReadyEventHandler ReplyReady;


private void Button1_Click(object sender, System.EventArgs e)
{
serialPort1.WriteLine(CommandToSend);
timer1.Start();
}

private void timer1_Tick(object sender, System.EventArgs e)
{
timer1.Stop();
try
{
if (ReplyReady != null) ReplyReady(serialPort1.ReadLine());
}
catch
{
timer1.Start();
}
}


Now I have spent some of weekend trying to translate this to the previsouly documented code with interesting results.


private string Write_DUT(string Data_To_DUT)
{
string Data_From_DUT = "";
if (DUTComPort.IsOpen == true)
{
DUTComPort.Write(Data_To_DUT + (char)13);
NoDataAtPortDUT.Start();
}
return (Data_From_DUT);
// string Data_From_DUT = "";
// label8.Text = "Write_DUT";
//if (DUTComPort.IsOpen == true)
//{
// Reply_Status = (int)REPLY.NO_REPLY;
// DUTComPort.Write(Data_To_DUT + (char)13);
// while (Reply_Status == (int)REPLY.NO_REPLY)
// {
// NoDataAtPort.Enabled = true;
// }

// NoDataAtPort.Enabled = false;

// if (Reply_Status == (int)REPLY.TIMEOUT_REPLY)
// {
// Data_From_DUT = "TIMEOUT";
// }
// else if (Reply_Status == (int)REPLY.YES_REPLY)
// {
// Data_From_DUT = DUTComPort.ReadTo("\r");
// }
// else
// {
// MessageBox.Show("ERROR", "ETRX Tester");
// }
//}
//else MessageBox.Show("DUT is Closed");
//prgbarTesting.Value++;

//return (Data_From_DUT);
}
private void OnTimeOutDUT_Tick(object sender, ElapsedEventArgs e)
{
NoDataAtPortDUT.Stop();
try
{
if (ReplyReady != null) ReplyReady(DUTComPort.ReadLine());
}
catch
{
NoDataAtPortDUT.Start();
}
}


Is this even close to right as all I seem to get is a time out from this code??

Glenn

last answered one year ago

1 answers

up0down
link

I don't see anywhere that you're adding an eventhandler to the ReplyReady event. If you don't do that, then ReplyReady will always be null and so nothing will happen except a time out.

If the handler is supposed to be your Write_DUT method, then you need to add somewhere (such as Form_Load):

ReplyReady += Write_DUT;


EDIT - see comments below

When you add an eventhandler all you need to specify is the name of the method. The compiler then looks at it and checks that the method has the same signature as the event (and hence the underlying delegate).

However, I've just noticed that the Write_DUT method returns a string whereas the delegate is void.

Assuming you do want to return this string (which will be empty as the code stands) you need to change the delegate accordingly:

public delegate string ReplyReadyEventHandler(string Reply); // now returns string

public event ReplyReadyEventHandler ReplyReady;

ptivate void Form1_Load(object sender, EventArgs e)
{
ReplyReady += Write_DUT;
}

private void Button1_Click(object sender, System.EventArgs e)
{
serialPort1.WriteLine(CommandToSend);
timer1.Start();
}

private void timer1_Tick(object sender, System.EventArgs e)
{
timer1.Stop();
try
{
if (ReplyReady != null)
{
string Data_From_DUT = ReplyReady(serialPort1.ReadLine());
// do something with Data_From_DUT
}
}
catch
{
timer1.Start();
}
}

private string Write_DUT(string Data_To_DUT)
{
string Data_From_DUT = "";
if (DUTComPort.IsOpen == true)
{
DUTComPort.Write(Data_To_DUT + (char)13);
NoDataAtPortDUT.Start();
}
// need to fill Data_From_DUT here
return (Data_From_DUT);
}

GlennP
329

Hi Vulpes, Added ReplyRead += Write_DUT; to the Form load however on thinking about it Write_DUT takes a string and Write_DUT() should if I'm not missing something should moan too shouldn't it? Glenn. Also not mentioned is the delegate and the event handler public delegate void ReplyReadyEventHandler(string Reply); public event ReplyReadyEventHandler ReplyReady; are added at the class form level.

vulpes
17279

Please see my edit.

GlennP
329

Hi Vulpes, I haven't had chance to try it, tomorrow morning looks like the earliest chance I will get now I'm tired! Thanks alot Glenn

Feedback