Reading string from Serial : very strange apparition

Hi everyone
I’m working on a project making an Arduino and a computer communicating together. Actually, the Arduino sends a signal (integer) via Serial port to a VB .NET app, this app interprets (I don’t know if the word is correct I’m french) it and then sends a string back to the Arduino which displays it on a LCD Screen (using LiquidCrystal library). Now the “sending to the PC” part is ok, everything works perfectly. But I need to send a string to the Arduino, here’s what I’ve done :
Testing string reception sketch :

#include <LiquidCrystal.h>
int incomingByte = 0;   // for incoming serial data
String chaine = "";
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
  Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
  lcd.begin(16, 2);
}

void loop() {
  if (Serial.available() > 0) {
    incomingByte = Serial.read();

    char fromSerial = incomingByte;
    if (fromSerial != 13) {
      chaine = chaine + fromSerial;
    }  
    else {
      lcd.setCursor(0, 0);
      lcd.clear();
      lcd.print(chaine);
      String chaine2 = "";
      chaine = chaine2;
    }
  }
}

And here’s the VB .NET code (I’ve posted it to pastebin because of syntax highlighting), even if you’re not a pro in .NET programming (or even a noob ^^) it shouldn’t be too difficult to understand : http://pastebin.com/YcUa5Liw
And what is the problem ? I run my VB .NET app. It sends to the Arduino aaaand the Arduino displays it totally correctly. Now I stop my VB .NET app and I start it again, and a new char appears at the beginning of the line : [image]. Do you know why it comes and how to resolve this problem ?
Thank you in advance
Thomas

You notice it is always on the start of the line if I am correct.
Simplest is to flush your serial buffers at the start.

void setup()
{
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
while (Serial.available() ) Serial.read(); // flush input buffer
lcd.begin(16, 2);
}

But I need to send a string to the Arduino

Then, why don’t you receive a string? Instead, you piss away resources receiving a String.

Arduino sends a signal (integer) via Serial port to a VB .NET app

How do you have your arduino tx setup wired? Is the signal integer also being sent to the LCD as well as the pc? Could the signal integer be the mystery character showing up on the LCD?

This happens when you restart the vb net app on the computer ?

Do you also restart the arduino ?

I am failing to see how the suggestion to exhaust the serial buffer in the arduino setup( ) function, will actually help here, unless you also reset the arduino.

I have had similar strange characters show up at the start of new lines when using lcd.print with ascii char arrays. The control characters for tab,line feed and carriage return have no representation in the HD44780 correspondence between character codes and character patterns.

I see that you are excluding the CR 13 from the chaine. There may be a LF 10 hiding in there as well.

If you can provide the string of actual char values you are trying to print on the lcd they can be referenced to the HD44780 data sheet.

Hi
Sorry I’m late…

PaulS:

But I need to send a string to the Arduino

Then, why don’t you receive a string? Instead, you piss away resources receiving a String.

Actually that’s what I was trying to do… I searched on the Internet how to do it and it was more or less my method.

zoomkat:
How do you have your arduino tx setup wired? Is the signal integer also being sent to the LCD as well as the pc? Could the signal integer be the mystery character showing up on the LCD?

This is just via USB. And no it shouldn’t be this because it’s not sent. And if I try to display the integer it shows up correctly.

And no I did not restart the Arduino so modifying the setup() wouldn’t change anything.

But I found my solution. Yeah it’s not what we can call a clean solution but… I simply filtered every non-printable character (if char < 32), if my memory’s good and now everything works fine !
Thank you anyway for your answers.

PS : and if someone’s interested : http://pastebin.com/1vnVTA0G