In my last project I tried to send data between an Arduino UNO and my PC through and Adafruit HUZZAH ESP8266 module, reading sensors, controlling servos etc.
To allow for further error handling, both the cards processed the incoming string data, using its first char as mode selection for example, and at the end of each state, the data was reassembled and sent to the next device.
The problem was, that the reassembled string was different from the original one. For the reassembly I used the String() command, putting the pieces together from a bunch char and String constants and variables (by constant I mean "string" and 'char') using + operator inbetween them. The error was present both on the UNO and the ESP8266.
I created a test code especially to uncover what the problem was, and how it works. I think it is an inconsistency of how the + operator works within the String() command.
The code, containing the result of the Serial monitor too:
char ch = 'A'; // Variable stored char
String str = "asd"; // Variable stored string
String conc; // String for concatenation
bool test = true; // Run test bit
void setup() {
Serial.begin(115200);
}
void loop() {
if (test){ // Runs only once
//////////// COMPARISON OF PREMADE AND AUTO CONCATENATION //////////////////
conc = String(ch+'_'+str+"_");
Serial.print(conc); // BUG HERE
Serial.print('\n');
Serial.print(ch+'_'+str+"_"); // BUG HERE
Serial.print('\n');
/////////// FIX BY STRING CONVERSION /////////////////////
Serial.print(String(ch)+'_'+str+"_");
Serial.print('\n');
Serial.print('\n');
/////////// TESTING SINGLE CHAR CONVERSION //////////////////////
Serial.print('A');
Serial.print('\n');
Serial.print(ch);
Serial.print('\n');
Serial.write('A');
Serial.print('\n');
Serial.write(ch);
Serial.print('\n');
Serial.print('\n');
/////////// TESTING WITHOUT CHAR VARIABLE /////////////////////
Serial.print('A'+'_'+str+"_"); // BUG HERE
Serial.print('\n');
Serial.print('\n');
/////////// TESTING WITHOUT VARIABLES ///////////////////
//Serial.print('A'+'_'+"asd"+"_"); SYNTAX ERROR
//Serial.print('\n');
//Serial.print('\n');
/////////// TESTING WITH ONLY STRING CONSTANTS //////////
//Serial.print("A"+"_"+"asd"+"_"); SYNTAX ERROR
//Serial.print('\n');
//Serial.print('\n');
/////////// TESTING WITH ONLY CHAR CONSTANTS /////////////
Serial.print('A'+'_'+'a'+'s'+'d'+'_'); // BUG HERE
Serial.print('\n');
Serial.print('\n');
/////////// TESTING WITH NO ADJACENT CHARS /////////////
Serial.print('A'+"asd"+'B'+str); // BUG HERE
Serial.print('\n');
Serial.print('A'+"asd"+str+'B'); // BUG HERE
Serial.print('\n');
Serial.print('\n');
/////////// TESTING WITH STRING START /////////////
Serial.print(str+'A'+'_'+'a'+'s'+'d'+'_');
Serial.print('\n');
Serial.print("asd"+'A'+'_'+'a'+'s'+'d'+'_'); // BUG HERE
Serial.print('\n');
Serial.print(str+'A');
Serial.print('\n');
Serial.print("asd"+'A'); // BUG HERE
Serial.print('\n');
Serial.print(str+ch);
Serial.print('\n');
Serial.print("asd"+ch); // BUG HERE
Serial.print('\n');
Serial.print('\n');
////////// TESTING STRING CONST+VAR ///////////////
Serial.print("asd"+str);
Serial.print('\n');
Serial.print(str+"asd");
Serial.print('\n');
Serial.print('\n');
test = false;
}
}
// CONCLUSION_1: Starting String() with a char (variable or constant) will add further
// characters to it with byte value, instead of concatenation until the first String
// variable (but not constant).
// CONCLUSION_2: Concatenating string constant with char(s) (variable or constant) with
// the string and at least one char being befor any string variables will cause the data
// to disappear or collapse.
// CONCLUSION_3: Adding more than one string constant to concatenate before a string variable
// will result in a data type mismatch error.
/* SERIAL MONITOR RESULTS (115200 baud):
160asd_
160asd_
A_asd_
A
A
A
A
160asd_
567
A
A
A
A
160asd_
567
asd
asdB
asdA_asd_
}⸮N⸮⸮0⸮⸮⸮k⸮⸮⸮⸮⸮#⸮D⸮⸮⸮x
?⸮⸮⸮⸮⸮o⸮5⸮⸮⸮#⸮⸮⸮⸮ϭ|⸮Os+wE⸮?w?⸮⸮⸮⸮^⸮⸮⸮r⸮⸮⸮-|z⸮>⸮Fݻ⸮7⸮⸮tYx⸮⸮⸮d⸮⸮⸮=⸮2⸮{⸮
⸮⸮⸮⸮⸮%⸮ۛ⸮⸮⸮⸮⸮⸮r⸮⸮;>ڝ[_⸮⸮⸮⸮⸮⸮}⸮Wޡo͏⸮r⸮⸮⸮4⸮⸮⸮e⸮⸮}/Ͽ⸮⸮⸮w⸮⸮Y̞⸮⸮3j6⸮S`⸮⸮⸮⸮⸮⸮⸮⸮?⸮\t⸮ɴS⸮B⸮⸮r⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮Dm⸮⸮.x⸮!⸮⸮⸮⸮O⸮⸮⸮ ⸮_⸮⸮}⸮⸮⸮⸮+n⸮⸮⸮⸮⸮LM⸮L[⸮⸮⸮⸮⸮⸮⸮[?⸮⸮m⸮~}⸮J⸮⸮⸮C=⸮⸮⸮⸮o⸮⸮Cv~⸮⸮⸮;⸮h{{S⸮⸮⸮⸮⸮⸮⸮⸮k⸮⸮⸮w⸮⸮e~⸮x⸮3⸮⸮⸮j5⸮⸮⸮{⸮/ҭ⸮⸮⸮⸮⸮1⸮G⸮⸮⸮⸮\;⸮⸮Ce⸮⸮⸮܈⸮⸮⸮GA⸮ջ⸮+⸮-c⸮⸮7⸮V_⸮q⸮⸮Swj⸮⸮⸮⸮{{x⸮⸮⸮w⸮R;⸮⸮⸮⸮⸮wl⸮T⸮ݿ⸮<⸮⸮⸮Ǧ⸮⸮⸮⸮⸮d⸮ۼ⸮⸮w⸮⸮⸮⸮⸮⸮o⸮⸮⸮⸮;⸮W8⸮⸮{⸮⸮
⸮⸮⸮⸮e⸮⸮⸮⸮~t;⸮~⸮⸮n⸮⸮W⸮[⸮w⸮⸮N+R⸮u⸮|⸮9⸮r⸮⸮⸮⸮⸮⸮)⸮⸮⸮⸮⸮/i.⸮>w⸮.⸮_.⸮⸮⸮G⸮k~X⸮⸮o⸮⸮⸮⸮⸮⸮⸮~⸮⸮$⸮⸮⸮2⸮⸮⸮⸮}⸮⸮⸮⸮?⸮=⸮⸮⸮⸮z\_⸮_⸮⸮⸮⸮⸮ίʯ1⸮⸮⸮t⸮⸮s⸮d~⸮1⸮.⸮ӿ⸮}⸮?{⸮⸮E⸮݀nX⸮1⸮ͭ⸮⸮⸮⸮*⸮⸮'⸮{⸮⸮⸮o⸮?|⸮⸮⸮O⸮⸮⸮B,O⸮<⸮⸮j⸮⸮s?⸮Ol֗^⸮YW]⸮⸮?⸮o⸮⸮8⸮Y~wl⸮⸮em⸮⸮⸮⸮⸮⸮⸮⸮3⸮ݼ⸮⸮~⸮⸮w⸮攏⸮֯⸮⸮⸮⸮߯⸮/⸮k⸮⸮⸮G⸮⸮⸮7*?,⸮⸮⸮Զ~>⸮⸮Y⸮\⸮ޗ⸮⸮⸮⸮Պk⸮⸮=⸮⸮?⸮⸮⸮⸮⸮⸮⸮⸮⸮|⸮⸮⸮⸮⸮g⸮⸮⸮⸮⸮⸮⸮⸮v⸮⸮⸮_+⸮⸮⸮⸮⸮S⸮⸮;⸮R⸮⸮Ǎ⸮⸮⸮cѻ⸮/⸮⸮,⸮⸮⸮'⸮pgUsF⸮/⸮⸮;⸮⸮"ϝ⸮⸮⸮_F>ߒn⸮⸮u?m⸮⸮⸮⸮⸮?⸮YM뫠⸮⸮⸮⸮n}⸮⸮⸮⸮'⸮⸮⸮⸮⸮\⸮Z⸮⸮"w⸮⸮m⸮?⸮⸮⸮elk⸮9⸮⸮⸮⸮Ξ⸮s˻⸮⸮⸮⸮x3l⸮⸮⸮ߏ⸮⸮⸮⸮⸮Q⸮⸮g⸮L⸮E{⸮]⸮⸮⸮⸮⸮̵Ͽ'⸮'⸮k⸮⸮e⸮gm⸮٧⸮⸮Y⸮/⸮⸮{m⸮^⸮⸮⸮Blof⸮ݳ~⸮h⸮⸮⸮⸮o⸮f⸮k⸮pG⸮=⸮j⸮⸮7⸮?~⸮k⸮⸮-V⸮ԣ⸮⸮;⸮⸮ey⸮+x⸮]G⸮⸮=⸮⸮⸮i⸮n⸮b⸮4⸮k⸮⸮<⸮?⸮ߓ]⸮⸮⸮⸮;:>⸮⸮⸮;v⸮g⸮}⸮⸮⸮}⸮⸮⸮ݿ⸮⸮+⸮/⸮˾⸮f⸮⸮4w⸮⸮]⸮u⸮⸮⸮⸮⸮⸮⸮⸮?⸮⸮⸮⸮⸮ѹ⸮⸮q[⸮⸮⸮⸮⸮⸮⸮⸮v7⸮⸮⸮⸮⸮⸮⸮j⸮O⸮⸮⸮e⸮⸮⸮⸮⸮⸮q⸮⸮R⸮⸮ްLw⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮w⸮_⸮ys⸮k⸮32L.⸮⸮]⸮֞⸮{⸮⸮⸮⸮7⸮⸮⸮⸮⸮Χ⸮⸮⸮g⸮⸮IW{⸮~e⸮⸮⸮⸮u⸮⸮q⸮~⸮O=<~⸮⸮⸮⸮~⸮⸮;⸮⸮⸮⸮J⸮⸮⸮⸮⸮⸮u⸮⸮⸮⸮+oM⸮`⸮⸮⸮⸮N⸮ͯ7z⸮w⸮M⸮⸮⸮C⸮⸮⸮^⸮⸮Ȯ⸮⸮⸮⸮⸮~⸮⸮⸮5⸮⸮q⸮⸮⸮⸮,ai
asdA
asdA
asdasd
asdasd
*/
I think the main problem is, that before a string variable, or freshly converted string comes in the line, the + operator tries to add the components, instead of concatenating them. But after a string variable the + is always concatenating.
I made three observations in the test, which I repeat here:
-
Befor a string variable (or fresh convert) comes in the equation, all the char variables and constants will be added together with their byte value, instead of concatenation. This value is then turned into string, as in a number to string conversion.
-
A string constant can not stop the addition of chars, instead the whole data up until that point will vanish or break.
-
Only one string constant can come befor a string variable in the equation, othervise the translation will fail with a data type mismatch error.
I'm not really sure that all this is a bug, or simply a consequence of automatic format conversion conflicting with the concatenation.
Both being able to calculate equations, turning the result into string withing a single line, and concatenating lots of various data into a string in a single line make sense. However the two is obviously conflicting because the "+" operator they both use.
I since solved my problem by applying conversion to the first chars, but took quite some time to even find out what was going wrong (though that was partly because how inexperienced I am).