471,313 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,313 software developers and data experts.

Reading From Serial Port

I'm trying to read data from the Serial Port but I'm running in to a
strange problem. The data parses correctly in a Console Application,
but when I try to read the same way in a Windows Form Application it
drops data randomly.

In the windows form application I read data using the following code:
public Form1()
{
InitializeComponent();
serialPort1.Open();
}

private void Form1_FormClosing(object sender,
FormClosingEventArgs e)
{
if (serialPort1.IsOpen) serialPort1.Close();
}

private void serialPort1_DataReceived(object sender,
SerialDataReceivedEventArgs e)
{
this.Invoke(new EventHandler(DisplayText));
}

private void DisplayText(object sender, EventArgs e)
{
textBox1.AppendText(serialPort1.ReadExisting());
}

The output in the textbox is:
:E= PERSONAL MESSAGEMF
=$To:Sean Sims=:se*******@gmail.com=Sep 24 2008
11:43=Subject:Test
= text>

In the console application the same text appears as:
:E= PERSONAL MESSAGEMF
=$To:Sean Sims
=$From:se*******@gmail.com
=_Time:Sep 24 2008 11:43
=*Subject:Test
=#<Empty text>

It should appear like this:
PERSONAL MESSAGE
To:Sean Sims
From:se*******@gmail.com
Time:Sep 24 2008 11:43
Subject:Test
<Empty text>

First, what can I do to troubleshoot and/or prevent the lost data in
the middle of the message? Second, what can I do to get rid of the
strange characters that appear before each line? Thanks in advance for
any help you can offer.
Sep 25 '08 #1
3 3289
Data is not dropping. The only difference is the returns. If you ensure you
have both a CR and an LF, you should be fine here. You also have a couple of
characters that do not want to display, if that is what you mean by lost
data.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://feeds.feedburner.com/GregoryBeamer#

or just read it:
http://feeds.feedburner.com/GregoryBeamer

********************************************
| Think outside the box! |
********************************************
"Sean" <se*******@gmail.comwrote in message
news:c7**********************************@l64g2000 hse.googlegroups.com...
I'm trying to read data from the Serial Port but I'm running in to a
strange problem. The data parses correctly in a Console Application,
but when I try to read the same way in a Windows Form Application it
drops data randomly.

In the windows form application I read data using the following code:
public Form1()
{
InitializeComponent();
serialPort1.Open();
}

private void Form1_FormClosing(object sender,
FormClosingEventArgs e)
{
if (serialPort1.IsOpen) serialPort1.Close();
}

private void serialPort1_DataReceived(object sender,
SerialDataReceivedEventArgs e)
{
this.Invoke(new EventHandler(DisplayText));
}

private void DisplayText(object sender, EventArgs e)
{
textBox1.AppendText(serialPort1.ReadExisting());
}

The output in the textbox is:
:E= PERSONAL MESSAGEMF
=$To:Sean Sims=:se*******@gmail.com=Sep 24 2008
11:43=Subject:Test
= text>

In the console application the same text appears as:
:E= PERSONAL MESSAGEMF
=$To:Sean Sims
=$From:se*******@gmail.com
=_Time:Sep 24 2008 11:43
=*Subject:Test
=#<Empty text>

It should appear like this:
PERSONAL MESSAGE
To:Sean Sims
From:se*******@gmail.com
Time:Sep 24 2008 11:43
Subject:Test
<Empty text>

First, what can I do to troubleshoot and/or prevent the lost data in
the middle of the message? Second, what can I do to get rid of the
strange characters that appear before each line? Thanks in advance for
any help you can offer.

Sep 25 '08 #2
I don't have any control over the output. I can only parse it. Why
is the text displayed correctly in the console application, but when I
shove it into a textbox the same data does not display properly?
Also, is there any way to filter special characters (I'm getting a lot
of symbols and other strange characters that won't display here)?

-Sean

On Sep 24, 10:14*pm, "Cowboy \(Gregory A. Beamer\)"
<NoSpamMgbwo...@comcast.netNoSpamMwrote:
Data is not dropping. The only difference is the returns. If you ensure you
have both a CR and an LF, you should be fine here. You also have a coupleof
characters that do not want to display, if that is what you mean by lost
data.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my bloghttp://feeds.feedburner.com/GregoryBeamer#

or just read it:http://feeds.feedburner.com/GregoryBeamer

********************************************
| Think outside the box! * * * * * * * * * * * * * * * |
********************************************"Sean" <sean.s...@gmail.comwrote in message

news:c7**********************************@l64g2000 hse.googlegroups.com...
I'm trying to read data from the Serial Port but I'm running in to a
strange problem. *The data parses correctly in a Console Application,
but when I try to read the same way in a Windows Form Application it
drops data randomly.

In the windows form application I read data using the following code:
* * * * public Form1()
* * * * {
* * * * * * InitializeComponent();
* * * * * * serialPort1.Open();
* * * * }

* * * * private void Form1_FormClosing(object sender,
FormClosingEventArgs e)
* * * * {
* * * * * * if (serialPort1.IsOpen) serialPort1.Close();
* * * * }

* * * * private void serialPort1_DataReceived(object sender,
SerialDataReceivedEventArgs e)
* * * * {
* * * * * * this.Invoke(new EventHandler(DisplayText));
* * * * }

* * * * private void DisplayText(object sender, EventArgs e)
* * * * {
* * * * * * textBox1.AppendText(serialPort1.ReadExisting());
* * * * }

The output in the textbox is:
*: E *= * *PERSONAL MESSAGE M F
* =$To:Sean Sims *=:sean.s...@gmail.com *=Sep 24 2008
11:43 *=Subject:Test
* = text>

In the console application the same text appears as:
*: E *= * *PERSONAL MESSAGE M F
* =$To:Sean Sims
* =$From:sean.s...@gmail.com
* =_Time:Sep 24 2008 11:43
* =*Subject:Test
* =#<Empty text>

It should appear like this:
PERSONAL MESSAGE
To:Sean Sims
From:sean.s...@gmail.com
Time:Sep 24 2008 11:43
Subject:Test
<Empty text>

First, what can I do to troubleshoot and/or prevent the lost data in
the middle of the message? Second, what can I do to get rid of the
strange characters that appear before each line? Thanks in advance for
any help you can offer.
Sep 25 '08 #3
Sean wrote:
I'm trying to read data from the Serial Port but I'm running in to a
strange problem. The data parses correctly in a Console Application,
but when I try to read the same way in a Windows Form Application it
drops data randomly.

In the windows form application I read data using the following code:
public Form1()
{
InitializeComponent();
serialPort1.Open();
}

private void Form1_FormClosing(object sender,
FormClosingEventArgs e)
{
if (serialPort1.IsOpen) serialPort1.Close();
}

private void serialPort1_DataReceived(object sender,
SerialDataReceivedEventArgs e)
{
this.Invoke(new EventHandler(DisplayText));
}

private void DisplayText(object sender, EventArgs e)
{
textBox1.AppendText(serialPort1.ReadExisting());
}

The output in the textbox is:
:E= PERSONAL MESSAGEMF
=$To:Sean Sims=:se*******@gmail.com=Sep 24 2008
11:43=Subject:Test
= text>

In the console application the same text appears as:
:E= PERSONAL MESSAGEMF
=$To:Sean Sims
=$From:se*******@gmail.com
=_Time:Sep 24 2008 11:43
=*Subject:Test
=#<Empty text>

It should appear like this:
PERSONAL MESSAGE
To:Sean Sims
From:se*******@gmail.com
Time:Sep 24 2008 11:43
Subject:Test
<Empty text>

First, what can I do to troubleshoot and/or prevent the lost data in
the middle of the message? Second, what can I do to get rid of the
strange characters that appear before each line? Thanks in advance for
any help you can offer.
Hi there, have you set the AcceptsReturn property of the textbox to true
? ( worth a try )
Sep 25 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by ^CeFoS^ | last post: by
1 post views Thread by ORC | last post: by
13 posts views Thread by Al the programmer | last post: by
13 posts views Thread by Rob | last post: by
16 posts views Thread by bloggsfred00 | last post: by
9 posts views Thread by Hal Vaughan | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.