我一直在玩arduino的编程,但今天我遇到了一个问题,我无法用我非常有限的C知识来解决。 这是怎么回事。 我正在创建一个将串行输入发送到arduino的pc应用程序(deviceID,command,commandparameters)。这个arduino将通过RF将该命令传送给其他arduino。取决于deviceID,正确的arduino将执行该命令。将字符串拆分为字符串数组
为了能够确定deviceID我想分割该字符串的“,”。 这是我的问题,我知道如何在java中轻松做到这一点(即使不使用标准拆分函数),但在C中它是一个完全不同的故事。
你们任何人都可以告诉我如何使这个工作?
感谢
/*
Serial Event example
When new serial data arrives, this sketch adds it to a String.
When a newline is received, the loop prints the string and
clears it.
A good test for this is to try it with a GPS receiver
that sends out NMEA 0183 sentences.
Created 9 May 2011
by Tom Igoe
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/SerialEvent
*/
String inputString; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
String[] receivedData;
void setup() {
// initialize serial:
Serial.begin(9600);
// reserve 200 bytes for the inputString:
inputString.reserve(200);
}
void loop() {
// print the string when a newline arrives:
if (stringComplete) {
Serial.println(inputString);
// clear the string:
inputString = "";
stringComplete = false;
}
}
/*
SerialEvent occurs whenever a new data comes in the
hardware serial RX. This routine is run between each
time loop() runs, so using delay inside loop can delay
response. Multiple bytes of data may be available.
*/
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
if (inChar == '\n') {
stringComplete = true;
}
// add it to the inputString:
if(stringComplete == false) {
inputString += inChar;
}
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
}
}
String[] splitCommand(String text, char splitChar) {
int splitCount = countSplitCharacters(text, splitChar);
String returnValue[splitCount];
int index = -1;
int index2;
for(int i = 0; i < splitCount - 1; i++) {
index = text.indexOf(splitChar, index + 1);
index2 = text.indexOf(splitChar, index + 1);
if(index2 < 0) index2 = text.length() - 1;
returnValue[i] = text.substring(index, index2);
}
return returnValue;
}
int countSplitCharacters(String text, char splitChar) {
int returnValue = 0;
int index = -1;
while (index > -1) {
index = text.indexOf(splitChar, index + 1);
if(index > -1) returnValue+=1;
}
return returnValue;
}
我已经决定,我将使用strtok
功能。 我现在正在遇到另一个问题。发生错误是
SerialEvent.cpp: In function 'void splitCommand(String, char)':
SerialEvent:68: error: cannot convert 'String' to 'char*' for argument '1' to 'char* strtok(char*, const char*)'
SerialEvent:68: error: 'null' was not declared in this scope
代码是这样,
String inputString; // a string to hold incoming data
void splitCommand(String text, char splitChar) {
String temp;
int index = -1;
int index2;
for(temp = strtok(text, splitChar); temp; temp = strtok(null, splitChar)) {
Serial.println(temp);
}
for(int i = 0; i < 3; i++) {
Serial.println(command[i]);
}
}
看那'的strtok()'函数。 – 2012-01-30 23:26:28
'strtok'折旧。使用'strsep'代替 – waspinator 2013-03-05 18:00:42
为了将来的参考,AFAIK'strtok()'不被弃用(或折旧)。 MS Visual C++编译器将其标记为不安全的广告提供和替代,正如GNU/POSIX(不同的替代方案)一样。正确使用和意识到它的缺点,它将按预期运行。 – Toby 2017-03-15 11:56:29