C# serialPort dataReceived event sent acknowledge?

How to send data back from the dataReceived even?

Page 1 of 1

6 Replies - 10470 Views - Last Post: 26 October 2010 - 06:53 PM Rate Topic: -----

#1 megac#  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 15-October 10

C# serialPort dataReceived event sent acknowledge?

Posted 26 October 2010 - 12:30 PM

I am working with DataReceived event and it works fine. I get data inputted into COM port every 5 seconds. I would like to send few bytes back only when data is received. When I try serialPort1.Write(c, 0, c.Length); the application starts to hang and send doesn't seem to work all the time. Any clues?

Thanks


 byte[] c = new byte[] {
        0x14,
        0x20,
        0x30,
        0x12,
        0x37,
        0x33,
        0x04,
        0x03
        };



   private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            string strRE = serialPort1.ReadExisting();
            this.BeginInvoke(new EventHandler(delegate
            {
                SetTheText(strRE);
            }));
            Application.DoEvents();

        }

        private void SetTheText(string strText)
        {
            txtReceive.Text += strText;
            serialPort1.Write(c, 0, c.Length);
        }




Is This A Good Question/Topic? 0
  • +

Replies To: C# serialPort dataReceived event sent acknowledge?

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5479
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: C# serialPort dataReceived event sent acknowledge?

Posted 26 October 2010 - 01:04 PM

Why are you doing this with BeginInvoke? Just call the method SetTheText.

How much text is in the textbox when it starts to hang? SInce you are just adding and adding and adding every 5 seconds the GUI delay could be an issue.
Was This Post Helpful? 0
  • +
  • -

#3 megac#  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 15-October 10

Re: C# serialPort dataReceived event sent acknowledge?

Posted 26 October 2010 - 01:46 PM

I am using BeginInvoke as this was provided by the USB dongle manufacturer (Virtual Com) code example.


http://www.ftdichip....ples/CSharp.htm
(Example 5)

I think it is supposed to prevent application from hanging while it is receiving data in a separate thread but it is already hanging anyways :-)

Okay I tried again with your suggestion, it's the same problem.

When I comment this line it works fine. It appears writing to serial port starts filling the whole text box with garbage character and legit packet. I want to write only one time upon receiving of the txt.

// serialPort1.Write(c, 0, c.Length);

Thanks very much for your time and suggestion

This post has been edited by megac#: 26 October 2010 - 01:52 PM

Was This Post Helpful? 0
  • +
  • -

#4 megac#  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 15-October 10

Re: C# serialPort dataReceived event sent acknowledge?

Posted 26 October 2010 - 01:56 PM

Ok found the problem the write was writing whole array whereas I just needed to send 8 bytes.

Thanks it's fixed now.
Was This Post Helpful? 0
  • +
  • -

#5 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: C# serialPort dataReceived event sent acknowledge?

Posted 26 October 2010 - 02:59 PM

View PosttlhIn, on 26 October 2010 - 04:04 PM, said:

Why are you doing this with BeginInvoke? Just call the method SetTheText.

How much text is in the textbox when it starts to hang? SInce you are just adding and adding and adding every 5 seconds the GUI delay could be an issue.


The event is fired on a background thread. Therefore, BeginInvoke is necessary....though a check to make sure an Invoke is required would help.
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5479
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: C# serialPort dataReceived event sent acknowledge?

Posted 26 October 2010 - 03:07 PM

View Posteclipsed4utoo, on 26 October 2010 - 01:59 PM, said:

View PosttlhIn, on 26 October 2010 - 04:04 PM, said:

Why are you doing this with BeginInvoke? Just call the method SetTheText.

How much text is in the textbox when it starts to hang? SInce you are just adding and adding and adding every 5 seconds the GUI delay could be an issue.


The event is fired on a background thread. Therefore, BeginInvoke is necessary....though a check to make sure an Invoke is required would help.


The OP's code doesn't show it really is on a background thread, though I thought it possible too. But if it really is on a background thread then the architecture has more issues. It shouldn't be trying to directly alter the text of the form. The class on the other thread should be raising its own event saying "New message, here it is, do what you want about it" and leaving the GUI transformations to the subscribing control.

But more importantly the OP says they have it working now.
Was This Post Helpful? 0
  • +
  • -

#7 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: C# serialPort dataReceived event sent acknowledge?

Posted 26 October 2010 - 06:53 PM

View PosttlhIn, on 26 October 2010 - 06:07 PM, said:

View Posteclipsed4utoo, on 26 October 2010 - 01:59 PM, said:

View PosttlhIn, on 26 October 2010 - 04:04 PM, said:

Why are you doing this with BeginInvoke? Just call the method SetTheText.

How much text is in the textbox when it starts to hang? SInce you are just adding and adding and adding every 5 seconds the GUI delay could be an issue.


The event is fired on a background thread. Therefore, BeginInvoke is necessary....though a check to make sure an Invoke is required would help.


The OP's code doesn't show it really is on a background thread, though I thought it possible too. But if it really is on a background thread then the architecture has more issues. It shouldn't be trying to directly alter the text of the form. The class on the other thread should be raising its own event saying "New message, here it is, do what you want about it" and leaving the GUI transformations to the subscribing control.

But more importantly the OP says they have it working now.


The DataReceived event of a SerialPort will always happen on a background thread.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1