Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arduino 无法将换行符添加到字符串_Arduino_Hid_Lf - Fatal编程技术网

Arduino 无法将换行符添加到字符串

Arduino 无法将换行符添加到字符串,arduino,hid,lf,Arduino,Hid,Lf,我有一个Arduino Leonardo,并试图将其用作串行到USB转换器。在Serial1上,我有一个以数字结尾的字符串。这个号码我正试图通过USB连接到电脑。它工作得很好,但我需要一个'\n'在最后,我不知道如何。当我在Keyboard.println或Keyboard.write行中尝试它时,我会得到一个不同数量的行,其中包含splitted中预期的数字 #包括 字符串myEAN=“”; 常数int mypoffergrosse=50; 字符串行缓冲区[MyPuffergross]; 无效

我有一个Arduino Leonardo,并试图将其用作串行到USB转换器。在
Serial1
上,我有一个以数字结尾的字符串。这个号码我正试图通过USB连接到电脑。它工作得很好,但我需要一个
'\n'
在最后,我不知道如何。当我在
Keyboard.println
Keyboard.write
行中尝试它时,我会得到一个不同数量的行,其中包含splitted中预期的数字

#包括
字符串myEAN=“”;
常数int mypoffergrosse=50;
字符串行缓冲区[MyPuffergross];
无效设置(){
键盘。开始();
Serial1.begin(9600);
延迟(1000);
}
字符串getEAN(char*stringWithInt)
//从字符串中返回一个数字(仅限正数!)
{
字符*尾部;
//跳过非数字
而(!isdigit(*stringWithInt))&&(*stringWithInt!=0))stringWithInt++;
返回(stringWithInt);
} 
void loop(){
//这是一个很好的例子
memset(serialBuffer,0,sizeof(mypuffergross));
if(Serial1.available()){
int incount=0;
while(Serial1.available()){
serialBuffer[incount++]=Serial1.read();
}
serialBuffer[incount]='\0';//结束字符串
myEAN=getEAN(serialBuffer);
//键盘。写入(0x0d);//这是一个CR
//键盘。write(0x0a);//这是一个LF
}
}

由于myEAN是一个字符串,只需添加字符

myEAN += '\n';
或者,对于全回车/换行组合:

myEAN += "\r\n";
见文件:

我建议您在getEAN函数中也使用String

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);
}
字符串getEAN(字符串s)
{
//返回在字符串中找到的第一个正整数。
int首先,最后;
for(first=0;first如果(“0”键盘正在发送键而不是字符。库只是将换行符转换为“Enter”键。欢迎使用堆栈溢出!在尝试提出更多问题之前,请先阅读。为什么要将最后一个字符设置为
null
,这就是
\0
您是否意识到这一点?这将分隔字符串而不是
\nde>您最有可能需要将
\n
放在
\0
之前。对不起,朋友,我很笨。故障是时间问题。我插入了延迟(50)从函数中获取字符串后,在发送到键盘之前。因此,字符串正确连接,我得到了可爱的1234567891011。感谢帮助。然后,您应该添加解决方案作为答案,并接受它供未来读者使用新行、新行、123、新行创建自己的解决方案。