我有一个 Arduino Leonardo 并试图将它用作串行到 USB 转换器。在Serial1
我有一个以数字结尾的字符串。这个号码我试图通过 USB 连接到 PC。它工作得很好,但我'\n'
最后需要一个,我不知道怎么做。当我在 lineKeyboard.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
}
}