Before logging an issue, please update to the latest release of Visual Micro from the Downloads Page.

When Logging a Support Issue in the Forum, please ensure you have also:-

  • Enabled vMicro > Compiler > Show Build Properties
  • Re-Compile your program with these settings enabled
 
Save the new Output to a Text File and....
  • Click the Reply button and attach as .txt file OR
  • Click here to Email us with the file attached, and a link to your post
Support requests without the output above may be impossible to answer, so please help us to help you
 
Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic Serial.Read or String += problem? (Read 7376 times)
j_witt
Newbies
*
Offline


Posts: 4
Joined: Feb 18th, 2014
Serial.Read or String += problem?
Feb 18th, 2014 at 5:42pm
Print Post  
Hey guys, 
I`m not new to Arduino and already programmed a whole lot of things, but this is the first time I need to consult the forum because I`m running nuts.

I`ve a pice of code, which perfectly works in the Arduino IDE. Here I can read out the serial bus 1 and the strMessage variable contains all input afterwards. By using Serial.print() the output gets printed without a problem.

Code
Select All
#define PIN_READ_WRITE 7

void setup()
{
	Serial.begin(115200);
	Serial1.begin(115200);
	pinMode(PIN_READ_WRITE, OUTPUT);
}

String strMessage = "";
void loop()
{
	String strMessageTmp = strMessage;
	while(Serial1.available())
	{
	    char c = Serial1.read();
		strMessage += c;
	}

	if (strMessageTmp != strMessage)
	{
		Serial.print(strMessage);
		delayMicroseconds(80);
	}

}
 



Example: I run the firmware, send "ABCDE" to the Serial port 1 and receive "ABCDE" on the other Serial output 0.

Now here is the problem:

Due to the fact that I`d like to code more C++ sytle, I installed VisualMicro on my visual studio and I run exactly the same code.
but the result ist not "ABCDE", it gets all chars delivered to the Serial port but when I print the result, I only get A on the out.  But on each output iteration only a single "A".

Is there anybody with experience in using VisualMicro and has already has such problems before?
  
Back to top
 
IP Logged
 
Tim@Visual Micro
Administrator
*****
Offline


Posts: 12203
Location: United Kingdom
Joined: Apr 10th, 2010
Re: Serial.Read or String += problem?
Reply #1 - Feb 18th, 2014 at 7:16pm
Print Post  
Hi,

Thanks for the info. While the debugger serial is active and shared with the arduino code the serial reader prefers a linefeed at the end of strings.

println(strMessage)

Having said that it should still show all chars but possibly delayed by a couple of seconds (the debugger waits to see if a line feed arrives).

We can test this but first please confirm if the serial displays correctly using Serial.println() instead of print()

Thanks
  
Back to top
IP Logged
 
j_witt
Newbies
*
Offline


Posts: 4
Joined: Feb 18th, 2014
Re: Serial.Read or String += problem?
Reply #2 - Feb 18th, 2014 at 8:14pm
Print Post  
I spend some time now, trying out some things and realized the following behaviour:

1. I have changed the serial.print() to serial.println()

Now, the following things happen:

Case 1: (the one before)
I transmit a single char and get: there is always a single char output, but always the first one I transmitted:
Example:
I transmit one char after another ( with a few seconds time inbetween ) 'A', 'B', 'C', 'D' and get output: AAAAAAAAA

Case 2:
I use println() instead of print()
I transmit a single char and end up with the same result as before, but after the first char delivered the println() command jumps into the next line after 
result:
A
AAAAAAAAAAAAAAA


Case 3:
I use println() instead of print()
I transmit 'ABDC' as one command and get the output:
A
ABCD

Now I change the code a bit to:

Code
Select All
while(Serial1.available())
	{
	    char c = Serial1.read();
            Serial.print(c);
	}
 


 
which works fine, but only if I output one char after another. As soon as I want to do something with the received char ( to += onto a String for example ) I`m not able to do that ( it just doesn`t appear in the string, the compiler takes it, but the char doesn`t get added to the string )

This lead me to another test:

Code
Select All
while(Serial1.available())
	{
	    int c = (int) Serial1.read();
            Serial.println(c);
	}
 

  

And: surprise surprise, all chars I transmit get received and printed, BUT for each line of a char, I also get a second line with a 0 in it.

Example: 
I transmit 'a', 'b', 'c', 'd' with some time inbetween and get:
97
0
98
0
99
0
100
0

So, for every char I receive, I also get a '0' and that seems to be the problem for the += operator in the string.


- Any thoughts what I can do?
- any workaround?
- can I shut off the debugger to solve the problem?


  
Back to top
 
IP Logged
 
Tim@Visual Micro
Administrator
*****
Offline


Posts: 12203
Location: United Kingdom
Joined: Apr 10th, 2010
Re: Serial.Read or String += problem?
Reply #3 - Feb 18th, 2014 at 8:59pm
Print Post  
Hi,

You can easily switch the debugger off of make it only enable for "Debug" or "Release" configuration or "Debug>Start without debugging".

However I do not thing this is a problem with the debugger. I would like to see the full sketch code so that I can try it myself in the Arduino Ide. I would expect the same results except that the .NET serial reader might handle double byte differently but also we need to consider the data types you are using.

"int" is different to "char", "char[]" is different to "String". 

Certainly in the case of int we know it can store more than 255 we should consider if it contains 1 or two bytes? For strings we have to consider if the /0 terminator is being shuffled correctly when you use +=, if the string is a pointer we also have to consider that.

Can you please email a simply example to info [at] visualmicro.com. 

Thanks



  
Back to top
IP Logged
 
j_witt
Newbies
*
Offline


Posts: 4
Joined: Feb 18th, 2014
Re: Serial.Read or String += problem?
Reply #4 - Feb 19th, 2014 at 10:15am
Print Post  
By playing around a bit, I found a workaround which seems to work:

I changed the while Loop a bit and read out an int value, because then I get the 0 delivered as well. After reading and attaching the int value (converted to char) I check for the zero and throw it away if it is a zero. By using this detour I can guarantee, that the string never gets destroyed by the 0.

Code
Select All
String strMessage = "";
while(Serial1.available())
	{
	    int c = (int)Serial1.read();
	    if (c!=0)
		{
			strMessage += (char)c;
		}
	}
 



It works fine, but this "zero"-check is not needed in the Arduino IDE but in Visual Micro.

I also send you the lines of code, but as written in the eMail, the code posted on top is actually everything that is needed to reproduce the problem any time.

The .Net SerialReader double Byte seems to have something to do with it.

Thank you for everything. I`m looking forward to further Progress on VisualMicro.





  
Back to top
 
IP Logged
 
Tim@Visual Micro
Administrator
*****
Offline


Posts: 12203
Location: United Kingdom
Joined: Apr 10th, 2010
Re: Serial.Read or String += problem?
Reply #5 - Feb 19th, 2014 at 1:39pm
Print Post  
Thanks for the update and well found! I will keep this in mind for others and will lookout for a .net workaround.

Would a zero in a string normally be the string terminator?
  
Back to top
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint