将 255 以上的 "String" 转换为精确的 "Integer" 或 "Long" 输入 Arduino IDE

Convert "String" above 255 to exact "Integer" or "Long" type in Arduino IDE

感谢您的宝贵时间。

我正在尝试将从 Arduino IDE 的 serialEvent() 中的串行端口读取的 "String" 转换为具有精确表示的整数值。

例如,如果 String myString = 200,则 int myInt 应为 200。

我取得了一些成功,但无法将 String 转换为超过 255 的精确 int 表示形式。

我尝试过的解决方案:

1) 在 Arduino 中使用了 .toInt() 函数。

2) 使用了 "atoi" 和 "atol" 函数。

3) Serial.parseInt() 在循环中().

所有这些方法在每 255 个值后从 0 开始重新计数。

我不能使用 parseInt,因为它只能在 loop() 内部使用。我的应用程序需要永久存储变量值,直到通过端口给出另一个值。为此,使用了 Arduino Due 的闪存。

内存存储代码似乎只能在 serialEvent() 内部工作。

代码片段如下:

#include <stdlib.h>
#include <DueFlashStorage.h>
DueFlashStorage memory;

String x = " ";
int x_size;
int threshold;

void setup(){
  Serial.begin(115200);
}

void loop{
  Serial.println(memory.read(0));
}

void serialEvent(){

  while(Serial.available()){

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

    x_size = x.length();
    char a[x_size+1];

    x.toCharArray(a, x_size+1);

    threshold = atoi(a);

    memory.write(0, threshold);
  }
}

1) Function .toInt() returns LONG 并且你想要 INT(老实说我不知道​​为什么,但它在 documentation 中)...你需要像这样转换(在 Arduino ATMEGA 上试过并且有效):

#include <stdlib.h>

String x = "1000";
int x_ = 0;

void setup() {
  Serial.begin(9600);

}

void loop() {
  x_ = (int) x.toInt();

  Serial.println(x_);
  delay(1000);
}

2) 本人不专业...不过serilEvent()确实是老套路了,不推荐使用。您可以在 loop() 函数中完成 "manually"。

您一次只能转换 1 个字符,这就是限制为 255 的原因。 如果您不做任何其他事情,您可以停留在 serial.read 循环中,直到读取所有字符。例如:

void loop() {
  if(Serial.available()) {
    byte count = 0;
    char number[10]; // determine max size of array
    bool wait = true;
    while(wait) { // stay in this loop until newline is read
      if(Serial.available()) {
        number[count] = Serial.read();
        if (number[count] == '\n') {
          wait = false; // exit while loop
        }
        count++;
      }
    }  
    int threshold = atoi(number);
    memory.write(0, threshold);
  }
}

由于 Arduino IDE 中缺少 char/String 类型到 int 类型转换(限制为 255)的良好功能,我编写了自己的转换代码,似乎可以完美运行。

int finalcount=0;


void setup(){

Serial.begin(115200);
}



void loop(){
   if(Serial.available()) {
    int count = 0;   
    char number[5];         // determine max size of array as 5

    bool wait = true;
    while(wait) {                // stay in this loop until newline is read
      if(Serial.available()) {
        number[count] = Serial.read();

        if (number[count] == '\n') {
          finalcount = count;         //max array size for integer; could be less than 5
          wait = false; // exit while loop
        }


        count++;
      }
    }  

    int val[finalcount];   //array size determined for integer
    int placeValue;         
    int finalval[finalcount];  
    int temp=0;
    int threshold;

    for(int i = 0; i<finalcount; i++){

        val[i] = (int)number[i]-48;        //convert each char to integer separately
        placeValue = pow(10,(finalcount-1)-i);  //calculate place value with a base of 10

        finalval[i] = val[i]*placeValue;      //update integers with their place value

        temp += finalval[i] ;            //add all integers 
        threshold = temp;              //resulting number stored as threshold             
    }

        Serial.println(threshold);     //prints beyond 255 successfully !


  }
}

我使用 Arduino 的 highByte 和 lowByte 函数解决了这个问题。完美运行。

#include <DueFlashStorage.h>
DueFlashStorage m;
byte a1,a2;
int val;

void setup() {
   
 Serial.begin(115200);           //start the serial communication
}

void loop()
{
 
 if (Serial.available()>0)
    {  

     val = Serial.parseInt();     //read the integer value from series

    if(val>0){
     a1 = highByte(val);          //get the higher order or leftmost byte
     a2 = lowByte(val);           //get the lower order or rightmost byte
    
     m.write(0,a1);              //save in arduino due flash memory address 0
     m.write(1,a2);              //save in arduino due flash memory address 1

    }
  
  int myInteger;

   myInteger = (m.read(0)*256)+m.read(1);     //convert into the true integer value
                                
   Serial.println(myInteger); 
      
 }