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 readString();代码运行缓慢_Arduino_Arduino Ide - Fatal编程技术网

Arduino readString();代码运行缓慢

Arduino readString();代码运行缓慢,arduino,arduino-ide,Arduino,Arduino Ide,我有下面的代码,我需要快速执行,但它需要花费大量的时间来更改值,无论如何,通过使这个任务更快的方式 我正在使用indexOf()和substring()来完成这个任务。 这用于更改条形LED颜色 //声明LED系列A引脚R-G-B(PWM引脚) int const AledRedPin=6; int const AledGreenPin=5; int const AledBluePin=3; //声明LED系列B引脚R-G-B(PWM引脚) int const BledRedPin=10; in

我有下面的代码,我需要快速执行,但它需要花费大量的时间来更改值,无论如何,通过使这个任务更快的方式

我正在使用
indexOf()
substring()
来完成这个任务。 这用于更改条形LED颜色

//声明LED系列A引脚R-G-B(PWM引脚)
int const AledRedPin=6;
int const AledGreenPin=5;
int const AledBluePin=3;
//声明LED系列B引脚R-G-B(PWM引脚)
int const BledRedPin=10;
int const BledGreenPin=11;
int const BledBluePin=9;
//串行输入变量&字符串
//初始化LED系列A针脚R-G-B(PWN值:0到255)
//初始值=255
int AledRed=255;
int-AledGreen=255;
int-AledBlue=255;
//初始化LED系列A针脚R-G-B(PWN值:0到255)
//初始值=255
int BledRed=255;
int BledGreen=255;
int BledBlue=255;
//串行输入
String命令=”;
//字符串操作
int cmdindexval=0;
字符串CommandType=“”;
int CommandValue=0;
字符串系列=”;
无效设置(){
//将安装代码放在此处,以便运行一次:
//启动序列号
Serial.begin(9600);
而(!串行){
;//等待串行端口连接。本机USB需要
}
//将LED系列A引脚设置为输出R-G-B
pinMode(AledRedPin,输出);
pinMode(AledGreenPin,输出);
pinMode(AledBluePin,输出);
//将LED系列B引脚设置为输出R-G-B
pinMode(BledRedPin,输出);
pinMode(BledGreenPin,输出);
pinMode(BledBluePin,输出);
}
void循环(){
//将主代码放在此处,以便重复运行:
//从序列号读取(如果可用)
如果(Serial.available()>0){
Command=Serial.readString();//从串行监视器读取字符串
cmdindexval=Command.indexOf('=');//读取字符直到'=',然后分配值
CommandType=Command.substring(0,cmdindexval);//将值从0分配给cmdindexval
//Series=Command.substring(0,1);//读取第一个字符
CommandValue=Command.substring(cmdindexval+1.toInt();//在“=”之后赋值,并将字符串转换为Int
Serial.println(CommandType+),等于“+CommandValue+”,Series:“+Series);
//如果(系列==“A”){
如果(CommandType==“ACledRed”){
AledRed=命令值;
}
else if(CommandType==“ACledGreen”){
AledGreen=CommandValue;
}
else if(CommandType==“ACledRedBlue”){
AledBlue=命令值;
}
//}
//否则如果(系列==“B”){
如果(CommandType==“BCledRed”){
BledRed=命令值;
}
else if(CommandType==“BCledGreen”){
BledGreen=命令值;
}
else if(CommandType==“BCledBlue”){
BledBlue=命令值;
}
//}
}//结束序列号
模拟写入(AledRedPin,AledRed);
模拟写入(AledGreenPin,AledGreen);
模拟写入(AledBluePin,AledBlue);
模拟写入(BledRedPin,BledRed);
模拟写入(BledGreenPin,BledGreen);
模拟写入(BledBluePin,BledBlue);
}
来自Arduino:

Serial.readString()将串行缓冲区中的字符读入字符串。如果函数超时,它将终止(请参见setTimeout()

Serial.setTimeout()设置使用Serial.readbytesintil()、Serial.readBytes()、Serial.parseInt()或Serial.parseFloat()时等待串行数据的最大毫秒数。默认值为1000毫秒

这意味着
readString
始终等待1秒,以确保字符串的发送已完成并具有完整的字符串。
不幸的是,这意味着它的反应很慢。您可以使用
setTimeout
来降低超时时间,但仍然会有一些延迟,或者如果您将其设置得太低,则可能会导致不完整的刺痛

最好的解决方案是使用,这样当您得到一个终止符字符(如换行符)时,您就知道您有一个完整的字符串

替换

Command=Serial.readString();

Command=Serial.readStringUntil('\n');

并确保设置了串行监视器,以便发送换行符。

编辑:请参阅结尾的重要更新

这可以大大加快速度,但首先让我们看看在使用当前代码的每个循环迭代中必须完成的工作:

  • 同样,在
    readString()
    中可能会损失一些时间
  • 对于每个值,必须发送、读取、解析15到20个字节并转换为
    int
  • 对于每个接收到的值(R、G或B),
    analogWrite()
    被调用6次(并且
    analogWrite()
    不是很快)。这意味着为了更改这两个系列,
    analogWrite()
    被调用36次(这可能是浪费时间最多的地方)。如果没有可用的串行数据,
    analogWrite()
    仍被调用6次
  • 而且,在本例中,每次都会调用
    Serial.println()
    ,因此最好将其关闭
为了加快速度,RGB值可以在一个小的缓冲区中发送(假设您也可以控制发送端),然后使用读取

如果A和B的值一起发送,则6个RGB值可以作为6个字节发送:

字节rcvBuffer[7];
void循环(){
如果(Serial.available()>0){
//消息:RGBRGBx-但请参阅下面的更新
int numRead=Serial.readBytesUntil(0x78,rcvBuffer,7);//0x78是“x”
如果(numRead==7){//或6,请参见下文
模拟写入(AledRedPin,rcvBuffer[0]);
模拟写入(AledGreenPin,rcvBuffer[1]);
模拟写入(AledBluePin,rcvBuffer[2]);
模拟写入(BledRedPin,rcvBuffer[3]);
模拟写入(BledGreenPin,rcvBuffer[4]);
模拟写入(BledBluePin,rc