0
문제가 있습니다. 날 믿어, 나는이 strtok() 함수를 1 또는 2AM에서 사용하기 시작했다. 오전 4시 22 분입니다. >. <strtok이 잘 작동하지 않습니다.
문제는 다음과 같습니다. 1. "청색"을 입력 할 때 세 개의 토큰은 괜찮습니다. 2. 언제 입력에 "빨간색"또는 "녹색", 세 개의 토큰 :(NULL입니다
내가 함수 경화제를 호출하는 방법const char s[2] = "~"; //for cutting;
char inData[100]; // Allocate some space for the string
char *token;
char *token2;
char *token3;
char x1[100];
char x2[100];
char x3[100];
char Comp(char* This) {
while (Serial.available() > 0) // Don't read unless
// there you know there is data
{
if(index < 99) // One less than the size of the array
{
inChar = Serial.read(); // Read a character
inData[index] = inChar; // Store it
index++; // Increment where to write next
// inData[index] = '\0'; // Null terminate the string
}
}
token = strtok(inData, s);
strcpy(x1,token);
token2 = strtok(NULL, s);
strcpy(x2,token2);
token3 = strtok(NULL, s);
strcpy(x3,token3);
if (strcmp(x1,This) == 0) {
Serial.println(x1);
Serial.println(x2);
Serial.println(x3);
for (int i=0;i<99;i++) {
inData[i]=0;
}
index=0;
return(0);
}
else {
return(1);
}
}
:.
if(Comp("blue")==0){
Serial.println("BLUE SELECTED");
analogWrite(9, 0);
analogWrite(10,255);
analogWrite(11,255);
}
if(Comp("green")==0){
Serial.println("GREEN SELECTED");
analogWrite(10,0);
analogWrite(11,255);
analogWrite(9,255);
}
if(Comp("red")==0){
Serial.println("RED SELECTED");
analogWrite(10,255);
analogWrite(11,0);
analogWrite(9,255);
}
출력 : 때 입력 : "블루 ~ lalalalal ~ kakakakaekkewew" 그것을 출력한다 : I가 INPU 때 블루 lalalalal kawawawawewew 블루
선택한 t : "레드 ~ 123 ~ 321 가 출력 : 적색
레드
을 선택하면 I 입력"녹색 ~ 123 ~ 321 " 가 녹색녹색
루프 읽기가 끝나면 직렬 읽기 작업이 버퍼를 종료하지 않습니다. 그것이 끝나면'inData [index] = 0;'을 넣으십시오. 즉,'index'가 실제로 처음에'0'으로 초기화된다고 가정하십시오. – WhozCraig
적절한 들여 쓰기는 가독성을 크게 높여줍니다. 특히이 많은 코드가 관련되어 있습니다. – Borgleader
답장을 보내 주셔서 감사합니다! WhozCraig : 저는이 그림을 첨부했습니다 : https://fbcdn-sphotos-f-a.akamaihd.net/hphotos-ak-prn2/t1/1796613_10201504171471088_2143310897_n.jpg 정말 마지막 두 개의 토큰에 두 개의 문자열이 필요합니다. – Glenn