Hi All,
First question, I am reading a serial port and putting output to a string,
but the string is blank, however if I stick a message box in, it causes enough of a gap to allow the text to appear in the string. I am using a delay based on the length of the expected reply.
The line of code that appears is reply = rtbIncoming.text.
rtbIncoming.Text has a string in it from the device. Is it because of seperate threads the reply in the text box appears on one thread, there is not enough of a delay for it to get from the comms thread to the UI thread?
Glenn
Loading

VulpesRank #55 · 29.2K reputationPosted Jan 25, 2012, 4:54 AM EST
VulpesRank #55 · 29.2K reputationPosted Jan 24, 2012, 4:27 AM EST
So instead of a line like this:
rtbIncoming.Text = reply;
I'd try code like this:
this.Invoke(new MethodInvoker(delegate()
{
rtbIncoming.Text = reply;
}));
This assumes the DataReceived eventhandler is in your Form class - not a separate class.
Glenn PattonRank #6,159 · 275 reputationPosted Jan 24, 2012, 5:19 AM EST
Umm tried a delegate in the event handler it didn't work and led to the issue see below:
private void port_DataReceived_1(object sender,SerialDataReceivedEventArgs e)
{
//int valueWait = WaitForData(12, 10);
InputData = myComPort.ReadExisting();
if (InputData != String.Empty)
{
this.BeginInvoke(new SetTextCallback(SetText), new object[]{ InputData});
}
}
Tried using the delegate you suggested with the same results!
I am a little condfused.
VulpesRank #55 · 29.2K reputationPosted Jan 24, 2012, 6:43 AM EST
Glenn PattonRank #6,159 · 275 reputationPosted Jan 24, 2012, 7:09 AM EST
Same guy different forum (I can't be the only person to mess with Serial Ports though!) to give you an update some problems with the board mean that
testing can only be done on one part of the board but it 'Works!!!' thank you I just moved where I was declaring the delegate etc.
this.Invoke(new MethodInvoker(delegate()
{
rtbIncoming.Text = reply;
}));
to just after the command is sent and it works!!!! about the handler name
port_DataReceived_1 that is the only noncommented interrupt handler
it was my name not Visual Studio's (great minds think alike, ah no wait....)
to fit in with others coding style (?) roll on Friday!!
Thanks
(how do I give you reputation points here?)
Glenn
Glenn PattonRank #6,159 · 275 reputationPosted Jan 25, 2012, 1:00 AM EST
Spotted a problem, if the machine is busy (mine was hammering the hard drive!) obviously the invoke method is very dependant on the use of the processor! Is there a source of info that (MircoSoft) is regaurded as official. As this I would like to check to see if there is a system legal way of making it a higher priority to always fire. This is getting away from my area of speciality into the Windows domain (beyond that point lies madness!)
Glenn
Glenn PattonRank #6,159 · 275 reputationPosted Jan 25, 2012, 1:23 AM EST
It looks like if I only enable the button once it has finished it gets around the problem.... Need my PC to go into one it's hard drive grinding moments.....
Glenn
Glenn PattonRank #6,159 · 275 reputationPosted Jan 25, 2012, 7:03 AM EST
Been in the lab for an hour or so, so didn't see your reply
in the past I have only used BeginInvoke and was a little unsure of Invoke but with the previous changes & that one the program now responds correctly.
this.BeginInvoke(new MethodInvoker(delegate()
{
rtbIncoming.Text = reply;
}));
I do really need to understand the delegate methods better & threading as a whole better could you recomend any resource?
Glenn
VulpesRank #55 · 29.2K reputationPosted Jan 25, 2012, 7:53 AM EST
I've also added a good resource on delegates and events:
http://www.albahari.com/threading/
http://www.akadia.com/services/dotnet_delegates_and_events.html