Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,

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

C#
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? As I have had the following advice
C#
So instead of a line like this:
   rtbIncoming.Text = reply;


I'd try code like this:
C#
this.Invoke(new MethodInvoker(delegate()
{
   rtbIncoming.Text = reply;
}));

This would appear to use a delegate similar to the one I am using the Data Recieved Event.
C#
    private void port_DataReceived_1(object sender,SerialDataReceivedEventArgs e)   
        {
           // MessageBox.Show("Here!!");
           int valueWait = WaitForData(12, 10);
          InputData = myComPort.ReadExisting(); 
          if (InputData != String.Empty)
          {
            this.BeginInvoke(new SetTextCallback(SetText), new object[]
{InputData});
            
          }
            /*
             this.Invoke(new MethodInvoker(delegate()
               {
                ReplyData=  rtbIncoming.Text;
               }));
             */
        }

I think the previous answer I had over looked this as I now have two delegates to the same thread with no success! HELP!!
Glenn
Posted
Comments
Sergey Alexandrovich Kryukov 24-Jan-12 23:02pm    
"With no success" is not a valid description of the problem. Why two delegates is a problem? What happens, exactly. How about exact error or exception dump? With comments in the source code at the places where something wrong happens? How about running under debugger?
--SA

1 solution

Solved! (I think) the question was a little a vague I grant you but it got me to thinking and placing the delegate after the send appeared to cure the problem yesterday. This morning my PC took a turn for the worse so thats a Windows reinstall, mutter, mutter. Can some one recomend a good source of reliable info regaurding threading and delegates as I get one source and then find another that does not contradict but comes near enough for question marks to form.

Glenn
 
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