Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
XML
Hi.,

I have written a code to read data from the serial continuously. But when running the code for long duration like 3-4 hours serial data receiving is not in corresponding to the data that is sent by the sender. If the same code is run for few min. it does not show any problem of that sort i.e, what the sender sends is immediately reflected at the serialport receive. please give any solution if available that does not make lose of data. here is the code for serial data receive

<pre lang="c#">th = new System.Threading.Thread(serialPort1_DataReceived);
                        th.IsBackground = true;
                        th.Start();


in serialPort1_DataReceived method
C#
RxString += serialPort1.ReadLine();
               DisplayText(RxString);


in "DisplayText" method I do some operations to split "rxstring" and display it in a text box.

Also I need to know why this problem does not occurs when using hyper-terminal like "putty"

thanks in advance
Posted
Updated 15-May-14 21:35pm
v2
Comments
CPallini 16-May-14 3:57am    
Probably you just handle incorrectly the received data. However, without actually seeing your code there's a little we can do to help you.
Sachinsnagaraj 16-May-14 5:58am    
i putting my code to displaytext<br>
 <br>
try<br>
{<br>
if (this.chart1.InvokeRequired)// i am using some charts to plot some //points on graph<br>
{<br>
SetTextCallback x = new SetTextCallback(this.DisplayText);<br>
this.Invoke(x, new object[] { rxt });<br>
}<br>
else<br>
{<br>
if (status)<br>
{<br>
string deviceid = string.Empty;<br>
string devicename = string.Empty;<br>
string power = string.Empty;<br>
 <br>
TextBox.CheckForIllegalCrossThreadCalls = false;<br>
//RxString = RxString.Remove(RxString.Length - 1, 1);<br>
string[] cntstring = rxt.ToString().Split(':');<br>
if (cntstring.Length == 2)<br>
{<br>
deviceid = cntstring[0];<br>
power = cntstring[1].Remove(cntstring[1].Length-1,1);<br>
devicename = deviceid;<br>
dataflag = true;<br>
}<br>
 <br>
if (deviceid != "" && !(deviceslist.Contains(deviceid)) && deviceid.Length == 5)<br>
{<br>
try<br>
{<br>
deviceslist.Add(deviceid);<br>
devicename = deviceid;<br>
if (!deviceidlist.Contains(deviceid))<br>
{<br>
deviceidlist.Add(deviceid);<br>
}<br>
}<br>
catch<br>
{<br>
 <br>
}<br>
}<br>
 <br>
<br>
double po = 0;<br>
try<br>
{<br>
po = double.Parse(power);<br>
}<br>
catch<br>
{<br>
po = 0;<br>
}<br>
 <br>
//Checklog(po.ToString(), deviceid);<br>
if (deviceslist.Contains(deviceid) && po >= threshold && po <= 5000)<br>
{<br>
if (dataflag == true)<br>
{<br>
try<br>
{<br>
devicename = getdeviceName(deviceid);<br>
if (devicename == "" || devicename == null)<br>
{<br>
try<br>
{<br>
devicename = deviceid;<br>
// addtochartseries(deviceslist.IndexOf(deviceid), "device : " + devicename);<br>
 <br>
}<br>
catch<br>
{<br>
 <br>
}<br>
}<br>
 <br>
}<br>
catch<br>
{<br>
devicename = deviceid;<br>
}<br>
if (g1.Visible

1 solution

Without the code fro teh "DisplayText" method, we have to guess - but I'd bet it has to do with the "+=" part:
C#
RxString += serialPort1.ReadLine();
Given that the DataRecieved event is handled on a non-UI thread, if your DisplayText method is using controls (as I would assume it does or you'd get cross threading errors) then there is a good chance that the UI thread is also modifying RxString at the same time - this is dangerous and can lead to all sorts of intermittent weird problems.

I would suggest that at a very minimum, you need to look at making all your code thread safe.
 
Share this answer
 
Comments
Sachinsnagaraj 16-May-14 3:48am    
my problem is not displaying but running the serial port for long duration is making some old data present in the serial port to appear which i am not able to display <br>
ex: if i run the program say for 3 hrs then switch of the data sender for serial, but serial port data receive still shows their some data in the serial port, where as the sender is already off.
Sachinsnagaraj 16-May-14 4:57am    
the sender sends data every 50 millisecond. help needed badly...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900