0

I have an Arduino Leonardo and trying to use it as a serial to USB converter. On Serial1 I have a string ending on a number. This number I'm trying to get via USB to the PC. It works very fine but I need a '\n' at the end and I don't know how. When I try it in the line Keyboard.println or Keyboard.write, I get a various number of lines with the expected number in splitted.

#include <Keyboard.h>
String myEAN ="";
const int myPuffergrosse = 50;
char serialBuffer[myPuffergrosse];
void setup() {
    Keyboard.begin();
    Serial1.begin(9600);
    delay(1000);
}
String getEAN (char *stringWithInt)
// returns a number from the string (positive numbers only!)
{
    char *tail;
    // skip non-digits
    while ((!isdigit (*stringWithInt))&&(*stringWithInt!=0)) stringWithInt++;
    return(stringWithInt);
} 

void loop() {   
    // Puffer mit Nullbytes fuellen und dadurch loeschen
    memset(serialBuffer,0,sizeof(myPuffergrosse));
    if ( Serial1.available() ) {
        int incount = 0;
        while (Serial1.available()) {
            serialBuffer[incount++] = Serial1.read();      
        }
        serialBuffer[incount] = '\0';  // puts an end on the string
        myEAN=getEAN(serialBuffer);
        //Keyboard.write(0x0d);  // that's a CR
        //Keyboard.write(0x0a);  // that's a LF
    }
}
gre_gor
  • 6,669
  • 9
  • 47
  • 52
toolsmith
  • 11
  • 1
  • 2
  • 1
    A keyboard is sending keys not characters. The library just translates line feed character into the 'Enter' key. – gre_gor Jun 25 '17 at 18:17
  • Welcome to Stack Overflow! Please read [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) before attempting to ask more questions. –  Jun 26 '17 at 16:53
  • why are you setting the last char to `null` that is what `\0` is you realize that right? That delimits the string not `\n` you need to put the `\n` BEFORE the `\0` most likely. –  Jun 26 '17 at 16:55
  • Sorry friends, I'm stupid. The fault is a timing problem. I Inserted a delay(50) after getting the string from the function and before sending to Keyboard. So the string is concatenated correctly and I get my lovely 1234567891011. Thanks for help. – toolsmith Jun 26 '17 at 17:08
  • 1
    then you should add the solution as an answer and accept it for future readers –  Jun 26 '17 at 18:37

1 Answers1

1

Since myEAN is a String, simply add the character...

myEAN += '\n';

Or, for a full carriage return/line feed combination:

myEAN += "\r\n";

See the doc: https://www.arduino.cc/en/Tutorial/StringAppendOperator

I suggest you use String in your getEAN function as well...

String getEAN(String s)
{
  // returns the first positive integer found in the string.

  int first, last;
  for (first = 0; first < s.length(); ++first)
  {
     if ('0' <= s[first] && s[first] <= '9')
       break;
  }
  if (first >= s.length())
    return "";

  // remove trailing non-numeric chars.
  for (last = first + 1; last < s.length(); ++last)
  {
     if (s[last] < '0' || '9' < s[last])
       break;
  }

  return s.substring(first, last - 1);
}
Michaël Roy
  • 6,338
  • 1
  • 15
  • 19