我一直在玩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