【问题标题】:Splitting a comma-delimited string of integers拆分以逗号分隔的整数字符串
【发布时间】:2012-08-11 15:28:59
【问题描述】:

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

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

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

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

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

这是草图代码:

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;
}

假设我的电脑向 Arduino 发送字符串“@20,2000,5!”。我的草图最终得到一个包含“20,2000,5”的字符串变量(称为command),并且commandRecieved布尔变量设置为True,因此调用了handleCommand()函数。

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

我读过strtok()atoi(),但坦率地说,我无法理解它们和指针的概念。我相信我的 Arduino 草图也可以优化。

【问题讨论】:

  • 这两个是一个很好的方法来做到这一点。如果可用,您也可以尝试 sscanf
  • 您能提供任何示例代码吗?我一直在努力想出上面草图中的 C 代码。我说的这两种方法我真的不知道怎么用……

标签: c string arduino


【解决方案1】:

由于您使用的是 Arduino 内核 String 类型,strtok 和其他 string.h 函数不合适。请注意,您可以将代码更改为使用标准 C 以空字符结尾的字符串,但使用 Arduino String 可以让您在不使用指针的情况下执行此操作。

String 类型为您提供 indexOfsubstring

假设删除了 @! 的字符串,查找命令和参数将如下所示:

// 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] 处的命令号,而您已指定只有参数应在data 中。但必要的改动很小。

【讨论】:

  • 为我工作,但我不得不将最后一行调整为:` arg = command.substring(beginIdx); arg.toCharArray(charBuffer, 16);数据[numArgs] = atoi(charBuffer);`
【解决方案2】:

似乎工作,可能是错误的:

#include<stdio.h>
#include <string.h>

int main(){
char string[]="20,2000,5";
int a,b,c;
sscanf(string,"%i,%i,%i",&a,&b,&c);
printf("%i %i %i\n",a,b,c);
a=b=c=0;
a=atoi(strtok(string,","));
b=atoi(strtok(0,","));
c=atoi(strtok(0,","));
printf("%i %i %i\n",a,b,c);

return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-21
    • 2015-03-07
    • 1970-01-01
    • 2017-01-05
    • 2012-05-24
    相关资源
    最近更新 更多