我一直在玩arduino编程,但今天我遇到了一个问题,我不能用我非常有限的C知识解决。
事情是这样的。
我正在创建一个PC应用程序,将串行输入发送到arduino(deviceID、command、commandparameters)。该arduino将通过RF将该命令传输到其他arduino。根据设备ID,正确的arduino将执行命令。
为了能够确定设备ID,我想在“,”上拆分该字符串。
这就是我的问题,我知道如何在Java中轻松完成此操作(即使不使用标准的split函数),但是在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: 在函数 'void splitCommand(String, char)' 中:
SerialEvent:68: 错误: 无法将'String'转换为'char*',作为参数传递给'char* strtok(char*, const char*)'
SerialEvent:68: 错误: 'null' 在此范围内未声明
代码如下:
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()
函数。 - Kerrek SBstrtok
已过时,请使用strsep
。 - waspinatorstrtok()
并没有被弃用。MS Visual C++编译器将其标记为不安全,并提供了替代方案,GNU/POSIX也是如此(虽然提供的替代方案不同)。只要正确使用并意识到其缺点,它就能正常运行。 - Tobystrtok
不是解决这个问题的好方法:它会将任何连续的,
视为单个分隔符。此外,它可能在 Arduino 平台上不可用。 - chqrlie