Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a C# Expert!

Join 244,284 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,038 people online right now. Registration is fast and FREE... Join Now!




How to avoid data loss using com port delegate?

 
Reply to this topicStart new topic

How to avoid data loss using com port delegate?

Bull Earwig
18 Dec, 2008 - 11:21 AM
Post #1

New D.I.C Head
*

Joined: 18 Dec, 2008
Posts: 2

I have a C# app that uses a COM port. To display data, I had to use a delegate to effectuate communication between the com port and GUI threads.

The relevant code:

CODE
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
  lock(this)
  {
    string incomingData = port.ReadExisting();
    string editedData = RemoveTrailingNullChars(incomingData);
    string displayText = "(" + editedData + ")\r\n";

    // Uncommenting the next line results in no problems whatsoever
    //string displayText = "ABCDEFGHIJK\r\n";
                
    Debug.WriteLine(displayText);
    object[] payload = new object[] { displayText };
    logRichTextBox.Invoke(new DisplayDelegate(UpdateDisplay), payload);
  }
}

The problem I am having is that data is getting lost/damaged. The Debug.WriteLine statement caused the following to appear in the Output window:

(S=8,P=326,D=32;)

(S=9,P=326,D=31;)

(S=10,P=325,D=33;(S=16,P=325,D=35;(S=24,P=325,D=31;)

(S=25,P=326,D=32;)

There should be a separate line for data containing S=11 through S=15.

Instead, it appears as if the delegate code wasn't reentrant, and the data for the S=16 record overwrote earlier data.

Also, notice the parentheses. There are no parentheses in the serial data stream. The only ones are in the line that sets displayText.

The lock statement was an attempt at fixing this. Didn't work.

All suggestions appreciated.

User is offlineProfile CardPM
+Quote Post


n8wxs
RE: How To Avoid Data Loss Using Com Port Delegate?
18 Dec, 2008 - 06:03 PM
Post #2

--... ...-- -.. . -. ---.. .-- -..- ...
Group Icon

Joined: 6 Jan, 2008
Posts: 1,609



Thanked: 223 times
My Contributions
Have a look at Communicating with Serial Port in C#
User is offlineProfile CardPM
+Quote Post

Bull Earwig
RE: How To Avoid Data Loss Using Com Port Delegate?
19 Dec, 2008 - 02:32 PM
Post #3

New D.I.C Head
*

Joined: 18 Dec, 2008
Posts: 2

Oops, my bad.

The problem was simple: some nulls ('\0') were in the data stream.

A null in the middle of a C# string has the same effect it would have in a C string: it terminates it.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 02:49PM

Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month