473,493 Members | 2,265 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 3375
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
9060
by: ^CeFoS^ | last post by:
Hello to everybody, I've done an application that draws in a frame the trajectory of a robot. The robot position is readed through the serial port, and several commands are wrote through the...
1
4564
by: ORC | last post by:
I have made a serial port class inspired bu the MTTTY example and the serial port class from OpenNETCF . below is the code from the readfile method in which there is a problem. The code calls the...
13
4793
by: Al the programmer | last post by:
I need to access the serial ports on my webserver from an asp.net page. I have no problem accessing the serial ports from a windows form application, but the code doesn't work in asp.net. I have...
8
16321
by: Vivek Menon | last post by:
Hi, I am using a C program to write/read from a serial port. The writing part is working perfectly fine. However, I am not able to read the values correctly and display them. To debug this issue I...
13
6163
by: Rob | last post by:
Hi all, I am fairly new to python, but not programming and embedded. I am having an issue which I believe is related to the hardware, triggered by the software read I am doing in pySerial. I...
16
4130
by: bloggsfred00 | last post by:
I need to read incoming bytes on a COM port but I do not want to have the script hang if there is nothing to read. Is there any way to have PHP interrogate a COM port buffer to see if there is...
0
1984
by: RG | last post by:
I am trying to read from my serial port a 24 bit binary number. I was able to read this number as a HEX but I was getting errors as at times using the vBCrLf indicator. I also can read it as an...
9
14345
by: Hal Vaughan | last post by:
I've done a fair amount of Googling for information on reading the serial port in C++ (and in Linux). Unfortunately, out of every 4 hits, 1 seems to be an unanswered question, 1 is someone saying,...
1
11500
by: ghjk | last post by:
I'm trying to read sms from GSM modem using c#. my code is only working for at command. When I enter at+cmgr=1 it says"ERROR". but when i typr it hyperterminal. It is working.Could you please tell me...
6
8411
by: anu29dolly | last post by:
Hello everyone... I have written a program to write and read data from serial port.... I am able to write 80(in binary)..and is expecting 1B but i am uable to read it... My code goes as...
0
7119
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6989
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7195
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
4889
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4579
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3088
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
644
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
285
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.