Reading From Serial Port

S

Sean

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=:[email protected]=Sep 24 2008
11:43=Subject:Test
= text>

In the console application the same text appears as:
:E= PERSONAL MESSAGEMF
=$To:Sean Sims
=$From:[email protected]
=_Time:Sep 24 2008 11:43
=*Subject:Test
=#<Empty text>

It should appear like this:
PERSONAL MESSAGE
To:Sean Sims
From:[email protected]
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.
 
C

Cowboy \(Gregory A. Beamer\)

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! |
********************************************
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=:[email protected]=Sep 24 2008
11:43=Subject:Test
= text>

In the console application the same text appears as:
:E= PERSONAL MESSAGEMF
=$To:Sean Sims
=$From:[email protected]
=_Time:Sep 24 2008 11:43
=*Subject:Test
=#<Empty text>

It should appear like this:
PERSONAL MESSAGE
To:Sean Sims
From:[email protected]
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.
 
S

Sean

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
 
P

Pete Kane

Sean said:
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=:[email protected]=Sep 24 2008
11:43=Subject:Test
= text>

In the console application the same text appears as:
:E= PERSONAL MESSAGEMF
=$To:Sean Sims
=$From:[email protected]
=_Time:Sep 24 2008 11:43
=*Subject:Test
=#<Empty text>

It should appear like this:
PERSONAL MESSAGE
To:Sean Sims
From:[email protected]
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 )
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top