Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.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

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
C++ (Arduino)我可以跳过parseInt()中的多个字符吗?_C++_Arduino - Fatal编程技术网

C++ (Arduino)我可以跳过parseInt()中的多个字符吗?

C++ (Arduino)我可以跳过parseInt()中的多个字符吗?,c++,arduino,C++,Arduino,和标题差不多。从arduino网站: 语法 Serial.parseInt() Serial.parseInt(char skipChar) 参数 skipChar:用于跳过搜索中指定的字符。例如,用于跳过千位分隔符 我能用字符映射或类似的方法跳过多个字符吗?也许你可以从序列中读取,但只能读取数字?然后解析字符串?肮脏的例子: val = 0; while (Serial.available() > 0) { int c = Serial.read(); if (isDig

和标题差不多。从arduino网站:

语法

Serial.parseInt()

Serial.parseInt(char skipChar)

参数

skipChar:用于跳过搜索中指定的字符。例如,用于跳过千位分隔符


我能用字符映射或类似的方法跳过多个字符吗?

也许你可以从序列中读取,但只能读取数字?然后解析字符串?肮脏的例子:

val = 0;
while (Serial.available() > 0) {
    int c = Serial.read();
    if (isDigit(c)) {
        val = val * 10 + c; // Thanks to @Danny_ds for this
    }
}
// Print *val*

我最终使用了一种完全不同的方法,通过使用字符数组而不依赖于时间来让它工作。到目前为止,它工作得很好。我用这个让我的arduino做温度监视器


它如何传达温度

PC>OpenHardwaremonitor>WMI>批处理脚本(如下所示)>COM端口>Arduino>LCD

这是我能正确获取cpu温度的唯一方法,因为它太旧了

批次代码:

@echo off
mode COM3: baud=9600 data=8 >nul
wmic /namespace:\\root\openhardwaremonitor path sensor where "Identifier='/intelcpu/0/temperature/0' or Identifier='/nvidiagpu/0/temperature/0'" get Value /every:2|findstr [0-9]>COM3
Arduino代码:

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

bool sounded = false;
int cpu = 0;
int gpu = 0;

char invalids[3] = {10, 32, '\0'}; // line feed, space
boolean newData = false;
const byte numChars = 8;
char charMap[numChars];
char tempChars[numChars];

void setup() {
  pinMode(6, OUTPUT); // character intensity
  pinMode(8, OUTPUT); // buzzer
  pinMode(10, OUTPUT); // backlight
  lcd.begin(16, 2);
  Serial.begin(9600);
  analogWrite(6, 100); // set intensity without POT
  analogWrite(10, 168); // ~3.3v
  analogReference(EXTERNAL);
  lcd.print("CPU:   ");
  lcd.print((char)223);
  lcd.print("C  AIR:");
  lcd.setCursor(0, 1);
  lcd.print("GPU:   ");
  lcd.print((char)223);
  lcd.print("C     ");
  lcd.print((char)223);
  lcd.print("F");
}

void loop() {
  recvWithoutWhitespace();
  if (newData == true) {
    parseData();
    lcd.setCursor(4, 0);
    lcd.print(cpu);
    lcd.setCursor(4, 1);
    lcd.print(gpu);
    int reading = analogRead(A0);
    float degreesF = (((reading * 3.3 / 1024 - 0.5) * 100) * 1.8) + 32.0;
    lcd.setCursor(11, 1);
    lcd.print((int)(degreesF+0.5));
    if(!sounded && (cpu > 75 || gpu > 85)) { // used for buzzer alarm
      tone(8, 500);
      delay(250);
      noTone(8);
      delay(250);
      tone(8, 500);
      delay(250);
      noTone(8);
      delay(250);
      tone(8, 500);
      delay(250);
      noTone(8);
      sounded = true;
    } else if(sounded && (cpu <= 75 && gpu <= 85)) {
      sounded = false;
    }
    newData = false;
  } 
}
void recvWithoutWhitespace() {
    static byte ndx = 0;
    static byte control = 0; // switch control variable
    char endMarker = 13; // ASCII code for carriage return
    char rc;
    char * check;

    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();
        check=strchr(invalids,rc); //checks if any spaces or line feeds get in

            if (check==NULL){ 
              if (rc != endMarker) {
                  charMap[ndx] = rc;
                  ndx++;
                 if (ndx >= numChars) {
                      ndx = numChars - 1;
                  }
             }
              else {
                switch(control) { // expect 4 CRs in format: (num)CRCR(num)CRCR
                  case 0:
                    control = 1; // skip first of 2 CRs
                    break;
                  case 1:
                    charMap[ndx] = 44; // put comma in place of CR between numbers as delimeter
                    ndx++;
                      if (ndx >= numChars) {
                        ndx = numChars - 1;
                      }
                    control = 2;
                    break;
                  case 2:
                    control = 3; // skip first of 2 CRs
                    break;
                  case 3:
                    charMap[ndx] = '\0'; // string terminator in place of last CR
                    ndx = 0;
                    control = 0;
                    newData = true;
                    break;
                }
              }
            }
    }
}
void parseData() {

    strcpy(tempChars, charMap); //strtok is destructive so copy string temporarily

    char * strtokIndx; // this is used by strtok() as an index

    strtokIndx = strtok(tempChars, ",");
    cpu = atoi(strtokIndx);     // convert cpu to an integer

    strtokIndx = strtok(NULL, ",");
    gpu = atoi(strtokIndx);     // convert gpu to an integer

}
#包括
液晶显示器(12,11,5,4,3,2);
布尔=假;
int cpu=0;
int gpu=0;
字符无效[3]={10,32',\0'};//换行,空格
布尔newData=false;
常量字节numChars=8;
char charMap[numChars];
char tempChars[numChars];
无效设置(){
pinMode(6,输出);//字符强度
pinMode(8,输出);//蜂鸣器
pinMode(10,输出);//背光
lcd.begin(16,2);
Serial.begin(9600);
analogWrite(6100);//设置不带电位计的强度
模拟写入(10168);//~3.3v
类比参考(外部);
lcd.打印(“CPU:”);
lcd.print((char)223);
lcd.打印(“C空气:”);
lcd.setCursor(0,1);
lcd.print(“GPU:”);
lcd.print((char)223);
液晶显示。打印(“C”);
lcd.print((char)223);
液晶显示器。打印(“F”);
}
void循环(){
recvWithouthHiteSpace();
if(newData==true){
解析数据();
lcd.setCursor(4,0);
打印(cpu);
lcd.setCursor(4,1);
lcd.打印(gpu);
int读数=模拟读数(A0);
浮点数f=((读数*3.3/1024-0.5)*100)*1.8)+32.0;
lcd.setCursor(11,1);
液晶显示打印((内部)(F+0.5度));
如果(!响起&&(cpu>75 | | gpu>85)){//用于蜂鸣器报警
音调(8500);
延迟(250);
诺通(8);
延迟(250);
音调(8500);
延迟(250);
诺通(8);
延迟(250);
音调(8500);
延迟(250);
诺通(8);
听起来是真的;
}否则如果(cpu=numChars){
ndx=numChars-1;
}
}
否则{
开关(控制){//4个CR的格式为:(num)CRCR(num)CRCR
案例0:
control=1;//跳过两个CR中的第一个
打破
案例1:
charMap[ndx]=44;//在数字之间用逗号代替CR作为delimeter
ndx++;
如果(ndx>=numChars){
ndx=numChars-1;
}
对照组=2;
打破
案例2:
control=3;//跳过两个CR中的第一个
打破
案例3:
charMap[ndx]='\0';//替换最后一个CR的字符串终止符
ndx=0;
控制=0;
newData=true;
打破
}
}
}
}
}
void parseData(){
strcpy(tempChars,charMap);//strtok是破坏性的,所以临时复制字符串
char*strtokIndx;//这被strtok()用作索引
strtokIndx=strtok(tempChars,“,”);
cpu=atoi(strtokIndx);//将cpu转换为整数
strtokIndx=strtok(NULL,“,”);
gpu=atoi(strtokIndx);//将gpu转换为整数
}

根据为该函数提供的原型,我必须使用no。您可以指定一个或零个
字符
。可能还有另一个函数可以执行此操作,但更可能的情况是您必须执行繁重的工作,可能需要库中的帮助,如
std::remove\u if
,您自己。您也可以使用conv读取字节时,将ert设置为
int
val=val*10+c;
。这样就不需要缓冲区(当然int也可能溢出)。