本文介绍了拆分以逗号分隔的整数字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的背景不是 C(它在 Real Studio - 类似于 VB)而且我真的很难拆分逗号分隔的字符串,因为我不习惯低级字符串处理.

My background is not in C (it's in Real Studio - similar to VB) and I'm really struggling to split a comma-delimited string since I'm not used to low-level string handling.

我正在通过串行向 Arduino 发送字符串.这些字符串是某种格式的命令.例如:

I'm sending strings to an Arduino over serial. These strings are commands in a certain format. For instance:

@20,2000,5!
@10,423,0!

'@' 是指示新命令的标题,'!'是标记命令结束的终止页脚.'@' 后的第一个整数是命令 id,其余整数是数据(作为数据传递的整数数量可以是 0 - 10 个整数).

'@' is the header indicating a new command and '!' is the terminating footer marking the end of a command. The first integer after '@' is the command id and the remaining integers are data (the number of integers passed as data may be anywhere from 0 - 10 integers).

我编写了一个草图,它获取命令(去掉了@"和!")并在有命令要处理时调用一个名为 handleCommand() 的函数.问题是,我真的不知道如何拆分这个命令来处理它!

I've written a sketch that gets the command (stripped of the '@' and '!') and calls a function called handleCommand() when there is a command to handle. The problem is, I really don't know how to split this command up to handle it!

这是草图代码:

String command; // a string to hold the incoming command
boolean commandReceived = false; // whether the command has been received in full

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
  // main loop
  handleCommand();
}

void serialEvent(){
  while (Serial.available()) {
  // all we do is construct the incoming command to be handled in the main loop

    // get the incoming byte from the serial stream
    char incomingByte = (char)Serial.read();

    if (incomingByte == '!')
    {
       // marks the end of a command
       commandReceived = true;
       return;
    }
    else if (incomingByte == '@')
    {
       // marks the start of a new command
       command = "";
       commandReceived = false;
       return;
    }
    else
    {
      command += incomingByte;
      return;
    }

  }
}

void handleCommand() {

  if (!commandReceived) return; // no command to handle

  // variables to hold the command id and the command data
  int id;
  int data[9];

  // NOT SURE WHAT TO DO HERE!!

  // flag that we've handled the command
  commandReceived = false;
}

假设我的 PC 向 Arduino 发送字符串@20,2000,5!".我的草图以包含20,2000,5"的字符串变量(称为 command)结束,并且 commandRecieved 布尔变量设置为 True,因此 handleCommand() 函数被调用.

Say my PC sends the Arduino the string "@20,2000,5!". My sketch ends up with a String variable (called command) that contains "20,2000,5" and the commandRecieved boolean variable is set to True so the handleCommand() function is called.

我想在(目前无用的)handleCommand() 函数中做的是将 20 分配给名为 id 的变量,将 2000 和 5 分配给一个整数数组称为data,即:data[0] = 2000data[1] = 5

What I would like to do in the (currently useless) handleCommand() function is assign 20 to a variable called id and 2000 and 5 to an array of integers called data, i.e: data[0] = 2000, data[1] = 5, etc.

我已经阅读了关于 strtok()atoi() 的内容,但坦率地说,我无法理解它们和指针的概念.我相信我的 Arduino 草图也可以优化.

I've read about strtok() and atoi() but frankly I just can't get my head around them and the concept of pointers. I'm sure my Arduino sketch could be optimised too.

推荐答案

由于您使用的是 Arduino 核心 String 类型,strtok 和其他 string.h 函数不合适.请注意,您可以将代码更改为使用标准的 C 以空字符结尾的字符串,但使用 Arduino String 将使您无需使用指针即可做到这一点.

Since you're using the Arduino core String type, strtok and other string.h functions aren't appropriate. Note that you can change your code to use standard C null-terminated strings instead, but using Arduino String will let you do this without using pointers.

String 类型为您提供 indexOfsubstring.

假设一个去掉了 @! 的字符串,找到你的命令和参数看起来像这样:

Assuming a String with the @ and ! stripped off, finding your command and arguments would look something like this:

// given: String command
int data[MAX_ARGS];
int numArgs = 0;

int beginIdx = 0;
int idx = command.indexOf(",");

String arg;
char charBuffer[16];

while (idx != -1)
{
    arg = command.substring(beginIdx, idx);
    arg.toCharArray(charBuffer, 16);

    // add error handling for atoi:
    data[numArgs++] = atoi(charBuffer);
    beginIdx = idx + 1;
    idx = command.indexOf(",", beginIdx);
}

data[numArgs++] = command.substring(beginIdx);

这将为您提供 data 数组中的整个命令,包括 data[0] 处的命令编号,而您已指定只有 args 应该是在 data 中.但必要的改动很小.

This will give you your entire command in the data array, including the command number at data[0], while you've specified that only the args should be in data. But the necessary changes are minor.

这篇关于拆分以逗号分隔的整数字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 09:00