0

私はArduino Leonardoを持っていて、それをシリアルからUSBへのコンバーターとして使用しようとしています。数字Serial1で終わる文字列があります。この番号を USB 経由で PC に取得しようとしています。それは非常にうまく機能しますが'\n'、最後に が必要で、方法がわかりません。行Keyboard.printlnまたはKeyboard.writeで試してみると、予想される数が分割されたさまざまな数の行が得られます。

#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
    }
}
4

1 に答える 1