拆分逗号分隔的整数串 [英] Splitting a comma-delimited string of integers

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

问题描述

我的背景是不是在C(它在真正的工作室 - 类似于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!

下面的草图code:

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和 commandRecieved 因此 handleCommand()函数被调用布尔变量设置为True。

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整数数组名为数据,即:数据[0] = 2000 数据[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的核心字符串键入 strtok的文件string.h 功能是不恰当的。请注意,您可以更改code使用标准的C语言的零终止字符串代替,但使用Arduino的字符串将让你这样做不使用指针。

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.

字符串 类型给你的 的indexOf 和的

假设用字符串 @ 扒掉,找到你的命令和参数会是这个样子

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

这会给你整个命令在数据阵列,包括命令数量数据[0] ,而您指定只有ARGS应该在数据。但是,必要的修改是次要的。

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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆