PC Review


Reply
Thread Tools Rate Thread

COM port works in debug mode, not in free run mode...

 
 
jodleren
Guest
Posts: n/a
 
      4th Apr 2011
Hi

My code is below. My problem is that I am trying to receive data from
some hardware, and it works when I am debugging it. But when I run it
in released mode, I get junk data - my strings get mixed up.
And now when writing this, I just checked something - and found, that
a break point in runtime, and I see that my received data is ok, but
my readout (the 2 subsrings) causes the problems. Like they cannot
handle the amount of data. Any ideas?

My problem:
line = ReceivedLine.Substring(0, i).Trim();
string tmp = ReceivedLine.Substring(i + 1);
ReceivedLine = tmp;


Entire code:

Conn.NewLine = '\r'.ToString(); //set packet terminator
Conn.DataBits = 8;
Conn.BaudRate = 115200;
Conn.Parity = System.IO.Ports.Parity.None;
Conn.StopBits = System.IO.Ports.StopBits.One;
Conn.DataBits = 8;
Conn.DtrEnable = true;
Conn.RtsEnable = true;
Conn.ReadTimeout = Conn.WriteTimeout = 500;//ms
Conn.DataReceived += Conn_DataReceived;


// handle data received
string ReceivedLine;
private void Conn_DataReceived(object sender,
System.IO.Ports.SerialDataReceivedEventArgs e)
{
string line;

ReceivedLine = Conn.ReadExisting();// reader.ReadLine();

while((i = ReceivedLine.IndexOf('\r')) != -1)
{
line = ReceivedLine.Substring(0, i).Trim();
string tmp = ReceivedLine.Substring(i + 1);
ReceivedLine = tmp;

__Log2(line);
if (line != "")
{
if (line.Substring(0, 1) == "!")
__Log2("dataline");
}
}
 
Reply With Quote
 
 
 
 
James A. Fortune
Guest
Posts: n/a
 
      4th Apr 2011
On Apr 4, 12:01*pm, jodleren <sonn...@hot.ee> wrote:
> Hi
>
> My code is below. My problem is that I am trying to receive data from
> some hardware, and it works when I am debugging it. But when I run it
> in released mode, I get junk data - my strings get mixed up.
> And now when writing this, I just checked something - and found, that
> a break point in runtime, and I see that my received data is ok, but
> my readout (the 2 subsrings) causes the problems. Like they cannot
> handle the amount of data. Any ideas?
>
> My problem:
> * * * * * * * * line = ReceivedLine.Substring(0, i).Trim();
> * * * * * * * * string tmp = ReceivedLine.Substring(i +1);
> * * * * * * * * ReceivedLine = tmp;
>
> Entire code:
>
> * * * * * * Conn.NewLine = '\r'.ToString(); //set packet terminator
> * * * * * * Conn.DataBits = 8;
> * * * * * * Conn.BaudRate = 115200;
> * * * * * * Conn.Parity = System.IO.Ports.Parity.None;
> * * * * * * Conn.StopBits = System.IO.Ports.StopBits.One;
> * * * * * * Conn.DataBits = 8;
> * * * * * * Conn.DtrEnable = true;
> * * * * * * Conn.RtsEnable = true;
> * * * * * * Conn.ReadTimeout = Conn.WriteTimeout = 500;//ms
> * * * * * * Conn.DataReceived += Conn_DataReceived;
>
> * * * * // handle data received
> * * * * string ReceivedLine;
> * * * * private void Conn_DataReceived(object sender,
> System.IO.Ports.SerialDataReceivedEventArgs e)
> * * * * {
> * * * * * * string line;
>
> * * * * * * ReceivedLine = Conn.ReadExisting();// reader.ReadLine();
>
> * * * * * * while((i = ReceivedLine.IndexOf('\r')) != -1)
> * * * * * * {
> * * * * * * * * line = ReceivedLine.Substring(0, i).Trim();
> * * * * * * * * string tmp = ReceivedLine.Substring(i +1);
> * * * * * * * * ReceivedLine = tmp;
>
> * * * * * * * * __Log2(line);
> * * * * * * * * if (line != "")
> * * * * * * * * {
> * * * * * * * * * * if (line.Substring(0, 1) == "!")
> * * * * * * * * * * * * __Log2("dataline");
> * * * * * * * * }
> * * * * * * }


This is a shot in the dark. The fact that the debug and release
versions do different things suggests a possible timing problem.
I.e., a breakpoint might give an operation time to complete that
wouldn't normally have time to complete. Microsoft sometimes tries to
improve the performance of certain operations by letting them run
asynchronously and naively assume that they will be done before you
try to access the returned value. I know that it doesn't seem logical
for the code not to wait until the entire value is returned, but that
is exactly what happens with some DAO operations in Access databases.
Perhaps getting data from a COM port is enough like getting data from
a database for the same philosophy to be in play. To test out that
hypothesis, put in a delay after the line:

ReceivedLine = Conn.ReadExisting();// reader.ReadLine();

to ensure that the entire value has been read before trying to create
your substrings. If an adequate delay doesn't fix the substrings,
then the problem lies elsewhere. If you determine that your code is
actually timing sensitive, then you can't even be sure that the
correct value you get during debugging means that the code for that
result is correct! Curiosity might have killed the cat, but
Schrödinger might have brought it back :-). You just need to use some
logic to come up with tests that rule out potential timing issues.

James A. Fortune
(E-Mail Removed)
 
Reply With Quote
 
jodleren
Guest
Posts: n/a
 
      5th Apr 2011
This was it:

> ReceivedLine = Conn.ReadExisting();// reader.ReadLine();



should be '

> ReceivedLine += Conn.ReadExisting();// reader.ReadLine();


seems like working late at night is not the best....



On Apr 4, 7:01*pm, jodleren <sonn...@hot.ee> wrote:
> Hi
>
> My code is below. My problem is that I am trying to receive data from
> some hardware, and it works when I am debugging it. But when I run it
> in released mode, I get junk data - my strings get mixed up.
> And now when writing this, I just checked something - and found, that
> a break point in runtime, and I see that my received data is ok, but
> my readout (the 2 subsrings) causes the problems. Like they cannot
> handle the amount of data. Any ideas?
>
> My problem:
> * * * * * * * * line = ReceivedLine.Substring(0, i).Trim();
> * * * * * * * * string tmp = ReceivedLine.Substring(i +1);
> * * * * * * * * ReceivedLine = tmp;
>
> Entire code:
>
> * * * * * * Conn.NewLine = '\r'.ToString(); //set packet terminator
> * * * * * * Conn.DataBits = 8;
> * * * * * * Conn.BaudRate = 115200;
> * * * * * * Conn.Parity = System.IO.Ports.Parity.None;
> * * * * * * Conn.StopBits = System.IO.Ports.StopBits.One;
> * * * * * * Conn.DataBits = 8;
> * * * * * * Conn.DtrEnable = true;
> * * * * * * Conn.RtsEnable = true;
> * * * * * * Conn.ReadTimeout = Conn.WriteTimeout = 500;//ms
> * * * * * * Conn.DataReceived += Conn_DataReceived;
>
> * * * * // handle data received
> * * * * string ReceivedLine;
> * * * * private void Conn_DataReceived(object sender,
> System.IO.Ports.SerialDataReceivedEventArgs e)
> * * * * {
> * * * * * * string line;
>
> * * * * * * ReceivedLine = Conn.ReadExisting();// reader.ReadLine();
>
> * * * * * * while((i = ReceivedLine.IndexOf('\r')) != -1)
> * * * * * * {
> * * * * * * * * line = ReceivedLine.Substring(0, i).Trim();
> * * * * * * * * string tmp = ReceivedLine.Substring(i +1);
> * * * * * * * * ReceivedLine = tmp;
>
> * * * * * * * * __Log2(line);
> * * * * * * * * if (line != "")
> * * * * * * * * {
> * * * * * * * * * * if (line.Substring(0, 1) == "!")
> * * * * * * * * * * * * __Log2("dataline");
> * * * * * * * * }
> * * * * * * }


 
Reply With Quote
 
James A. Fortune
Guest
Posts: n/a
 
      5th Apr 2011
On Apr 5, 3:56*am, jodleren <sonn...@hot.ee> wrote:
> This was it:
>
> > * * * * * * ReceivedLine = Conn.ReadExisting();// reader.ReadLine();

>
> should be '
>
> > * * * * * * ReceivedLine += Conn.ReadExisting();// reader..ReadLine();

>
> seems like working late at night is not the best....
>
> On Apr 4, 7:01*pm, jodleren <sonn...@hot.ee> wrote:
>
> > Hi

>
> > My code is below. My problem is that I am trying to receive data from
> > some hardware, and it works when I am debugging it. But when I run it
> > in released mode, I get junk data - my strings get mixed up.
> > And now when writing this, I just checked something - and found, that
> > a break point in runtime, and I see that my received data is ok, but
> > my readout (the 2 subsrings) causes the problems. Like they cannot
> > handle the amount of data. Any ideas?

>
> > My problem:
> > * * * * * * * * line = ReceivedLine.Substring(0, i).Trim();
> > * * * * * * * * string tmp = ReceivedLine.Substring(i+ 1);
> > * * * * * * * * ReceivedLine = tmp;

>
> > Entire code:

>
> > * * * * * * Conn.NewLine = '\r'.ToString(); //set packet terminator
> > * * * * * * Conn.DataBits = 8;
> > * * * * * * Conn.BaudRate = 115200;
> > * * * * * * Conn.Parity = System.IO.Ports.Parity.None;
> > * * * * * * Conn.StopBits = System.IO.Ports.StopBits.One;
> > * * * * * * Conn.DataBits = 8;
> > * * * * * * Conn.DtrEnable = true;
> > * * * * * * Conn.RtsEnable = true;
> > * * * * * * Conn.ReadTimeout = Conn.WriteTimeout = 500;//ms
> > * * * * * * Conn.DataReceived += Conn_DataReceived;

>
> > * * * * // handle data received
> > * * * * string ReceivedLine;
> > * * * * private void Conn_DataReceived(object sender,
> > System.IO.Ports.SerialDataReceivedEventArgs e)
> > * * * * {
> > * * * * * * string line;

>
> > * * * * * * ReceivedLine = Conn.ReadExisting();// reader.ReadLine();

>
> > * * * * * * while((i = ReceivedLine.IndexOf('\r')) != -1)
> > * * * * * * {
> > * * * * * * * * line = ReceivedLine.Substring(0, i).Trim();
> > * * * * * * * * string tmp = ReceivedLine.Substring(i+ 1);
> > * * * * * * * * ReceivedLine = tmp;

>
> > * * * * * * * * __Log2(line);
> > * * * * * * * * if (line != "")
> > * * * * * * * * {
> > * * * * * * * * * * if (line.Substring(0, 1) =="!")
> > * * * * * * * * * * * * __Log2("dataline");
> > * * * * * * * * }
> > * * * * * * }


jodleren,

I'm glad you found the problem. But please explain how that change
fixed the problem. It doesn't seem possible to me that such a change
would account for it working correctly in debug mode but not in
release. Shouldn't the bug have affected both builds?

James A. Fortune
(E-Mail Removed)
 
Reply With Quote
 
Phil Hunt
Guest
Posts: n/a
 
      5th Apr 2011
If the issue is timing as you offered, the change can still make a
difference. Because, in debug mode, the receive event may have enough time
to read in all the data.

"James A. Fortune" <(E-Mail Removed)> wrote in message
news:2c870dae-24b1-4115-be7f-(E-Mail Removed)...
On Apr 5, 3:56 am, jodleren <sonn...@hot.ee> wrote:
> This was it:
>
> > ReceivedLine = Conn.ReadExisting();// reader.ReadLine();

>
> should be '
>
> > ReceivedLine += Conn.ReadExisting();// reader.ReadLine();

>
> seems like working late at night is not the best....
>
> On Apr 4, 7:01 pm, jodleren <sonn...@hot.ee> wrote:
>
> > Hi

>
> > My code is below. My problem is that I am trying to receive data from
> > some hardware, and it works when I am debugging it. But when I run it
> > in released mode, I get junk data - my strings get mixed up.
> > And now when writing this, I just checked something - and found, that
> > a break point in runtime, and I see that my received data is ok, but
> > my readout (the 2 subsrings) causes the problems. Like they cannot
> > handle the amount of data. Any ideas?

>
> > My problem:
> > line = ReceivedLine.Substring(0, i).Trim();
> > string tmp = ReceivedLine.Substring(i + 1);
> > ReceivedLine = tmp;

>
> > Entire code:

>
> > Conn.NewLine = '\r'.ToString(); //set packet terminator
> > Conn.DataBits = 8;
> > Conn.BaudRate = 115200;
> > Conn.Parity = System.IO.Ports.Parity.None;
> > Conn.StopBits = System.IO.Ports.StopBits.One;
> > Conn.DataBits = 8;
> > Conn.DtrEnable = true;
> > Conn.RtsEnable = true;
> > Conn.ReadTimeout = Conn.WriteTimeout = 500;//ms
> > Conn.DataReceived += Conn_DataReceived;

>
> > // handle data received
> > string ReceivedLine;
> > private void Conn_DataReceived(object sender,
> > System.IO.Ports.SerialDataReceivedEventArgs e)
> > {
> > string line;

>
> > ReceivedLine = Conn.ReadExisting();// reader.ReadLine();

>
> > while((i = ReceivedLine.IndexOf('\r')) != -1)
> > {
> > line = ReceivedLine.Substring(0, i).Trim();
> > string tmp = ReceivedLine.Substring(i + 1);
> > ReceivedLine = tmp;

>
> > __Log2(line);
> > if (line != "")
> > {
> > if (line.Substring(0, 1) == "!")
> > __Log2("dataline");
> > }
> > }


jodleren,

I'm glad you found the problem. But please explain how that change
fixed the problem. It doesn't seem possible to me that such a change
would account for it working correctly in debug mode but not in
release. Shouldn't the bug have affected both builds?

James A. Fortune
(E-Mail Removed)


 
Reply With Quote
 
Phil Hunt
Guest
Posts: n/a
 
      5th Apr 2011
Sorry, hit enter too soon.

If the issue is timing as you offered, the change can still make a
difference. Because, in debug mode, the receive event may have enough time
to read in all the data, and never need to concatenate.


 
Reply With Quote
 
James A. Fortune
Guest
Posts: n/a
 
      5th Apr 2011
On Apr 5, 4:50*pm, "Phil Hunt" <a...@aaa.com> wrote:
> Sorry, hit enter too soon.
>
> If the issue is timing as you offered, the change can still make a
> difference. Because, in debug mode, the receive event may have enough time
> to read in all the data, and never need to concatenate.


Thanks for the explanation. Did you try using "volatile" as Peter
suggested? If so, was there a difference?

James A. Fortune
(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Code only works in Debug mode. Why? Ayo Microsoft Excel Programming 6 21st Oct 2009 04:04 PM
User Control access fails in debug mode, works in release mode rbg Microsoft C# .NET 0 17th Jan 2007 10:51 PM
User Control access fails in debug mode, works in release mode rbg Microsoft ASP .NET 0 17th Jan 2007 10:51 PM
SP2 works only in debug mode Chris Hengeveld Windows XP General 6 28th Nov 2004 12:12 PM
error when compiled in release mode works in debug mode. =?Utf-8?B?SmlnYXI=?= Microsoft Dot NET Framework 4 27th Jun 2004 04:51 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:28 PM.