【发布时间】:2014-04-12 19:42:11
【问题描述】:
我在使用 Arduino IDE 编写的一些代码时遇到问题。
当我尝试将长度为 12 的 uint8_t 类型的数组传递给函数时,会出现问题。传递数组时,它的大小似乎减小了(从 12 减少到 4)。
我无法弄清楚这里发生了什么。任何帮助将不胜感激!
这是我的代码:
int Serialbaud=19200;
int byteCount;
uint8_t message[] = {0x06, 0x01, 0x08, 0x01, 0xF0, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01};
// Test the transmission of our message (uint8_t array)
void testFunc(uint8_t *MSG) {
uint32_t len= sizeof(MSG)/sizeof(uint8_t);
Serial.println();
Serial.println("After passing to function the message is " + String(len) + " bytes:");
for(int i=0; i<len; i++) {
Serial.print(MSG[i]);
Serial.print(", ");
}
Serial.println();
}//end function
//--------SETUP------------------
void setup()
{
delay(3000);//Give yourself time to open up the serial monitor
Serial.begin(Serialbaud); //Begin serial ommunication with Serial Monitor
//Report the original length and content of the message to the serial monitor
uint32_t len= sizeof(message)/sizeof(uint8_t);
Serial.println("Original message before passing to function is " + String(len) + " bytes:");
for(int i=0; i<len; i++) {
Serial.print(message[i]);
Serial.print(", ");
}
Serial.println();
//Pass the message to the test function
testFunc(message);
}
//--------MAIN LOOP-------MAIN LOOP-------MAIN LOOP-------MAIN LOOP-------MAIN LOOP-------MAIN LOOP--
void loop()
{
}//END LOOP-------------------
串行监视器输出如下所示:
传递给函数之前的原始消息是 12 个字节: 6, 1, 8, 1, 240, 1, 1, 1, 0, 0, 0, 1,
传递给函数后,消息为 4 个字节: 6, 1, 8, 1,
【问题讨论】:
-
请查看答案并验证它们是否能解决您的问题。如果是,请检查已回答。谢谢。
标签: c++ arrays arduino arduino-ide