下面的代码是调试我的实现,您可以复制到Arduino IDE中查看打印输出。
我不能理解的是,itoa如何创建一个大于声明大小的数组,该大小不能被sizeof()检测到。我声明的数组大小为,但itoa使用大小为的数组来存储其结果!
Serial.println("|----START DEBUG------|");
int Sensor1Data=-32760;
Serial.print("Sensor1Data: ");Serial.println(Sensor1Data);
byte Sensor1CharMsg[2];
Serial.println("|----STAGE 2------|");
Serial.print("Array Size b4 itoa: ");Serial.println(sizeof(Sensor1CharMsg));
itoa(Sensor1Data,(char*)Sensor1CharMsg,10);
Serial.print("Array Values up to 10 elements: ");Serial.write(Sensor1CharMsg,10); Serial.println("");
Serial.print("Array Size a4tr itoa: ");Serial.println(sizeof(Sensor1CharMsg));
Serial.println("||||||| ARRAY OUTPUT|||||||");
Serial.print("Sensor1CharMsg[0]): "); Serial.println(Sensor1CharMsg[0]);
Serial.print("Sensor1CharMsg[1]): "); Serial.println(Sensor1CharMsg[1]);
Serial.print("Sensor1CharMsg[2]): "); Serial.println(Sensor1CharMsg[2]);
Serial.print("Sensor1CharMsg[3]): "); Serial.println(Sensor1CharMsg[3]);
Serial.print("Sensor1CharMsg[4]): "); Serial.println(Sensor1CharMsg[4]);
Serial.print("Sensor1CharMsg[5]): "); Serial.println(Sensor1CharMsg[5]);
Serial.println("|||||||END ARRAY OUTPUT|||||||");
传输后:
int Sensor2Data = atoi((char*)Sensor1CharMsg);
Serial.print("Sensor2Data: ");Serial.println(Sensor2Data);
结果
|----START DEBUG------|
Sensor1Data: -32760
|----STAGE 2------|
Array Size b4 itoa: 2
-32760
Array Size a4tr itoa: 2
||||||| ARRAY OUTPUT|||||||
Sensor1CharMsg[0]): 45
Sensor1CharMsg[1]): 51
Sensor1CharMsg[2]): 50
Sensor1CharMsg[3]): 55
Sensor1CharMsg[4]): 54
Sensor1CharMsg[5]): 48
|||||||END ARRAY OUTPUT|||||||
传输
Sensor2Data: -32760
2013-04-02
frazras