1

我正在尝试在 Arduino 上运行下面的代码,但是当我验证代码时,它显示“字符数组的初始化字符串太长”。尽管我已经阅读了有关类似问题的先前问题,但我不知道从我现在正在尝试的代码中从哪里开始。是的,,, 我对 C++ 世界很陌生,,, 如果你能给我一个自学的线索或直接的答案,那将非常感激。

最好的,

/**** SET YOUR MAC ADDRESS HERE ****/

char mac[13] = "74-E5-43-BE-42-10";

/***********************************/


#define LED 13
#define BLUESMIRFON 2

#define FACTORYRESETBAUD 57600
#define DEFAULTBAUD 115200

char str[3];
char passkey[5] = "0000";

boolean success = false;

int failOuts[10] = {3,4,5,6,7,8,9,10,11,12};

void setup() 
{ 
//Initialize pins
pinMode(LED, OUTPUT);
pinMode(BLUESMIRFON, OUTPUT);
for (int i=0; i<10; i++) {
pinMode(failOuts[i], OUTPUT);
}

// First reset to factory defaults
while (!success) {
RunBlueSmirfSetup(true);
}
success = false;
// Then set up with the correct mac address
RunBlueSmirfSetup(false);
} 

void loop() {   
if(success) {
digitalWrite(LED,LOW);
delay(1000);
digitalWrite(LED,HIGH);
delay(1000);
}    
} 

void RunBlueSmirfSetup(boolean factoryReset) {

//Initialize serial ports
if (factoryReset) {
Serial.begin(FACTORYRESETBAUD);   
} else {
Serial.begin(DEFAULTBAUD);   
}   

digitalWrite(BLUESMIRFON, LOW);
delay(2000);
digitalWrite(BLUESMIRFON, HIGH);  
delay(2000);              //Wait for BlueSMIRF to turn on

Serial.print('$');      //Send command to put BlueSMIRF into programming mode
Serial.print('$');
Serial.print('$');

delay(100);
Serial.flush();

//Reset the module
if (factoryReset) {
Serial.print('S');
Serial.print('F');
Serial.print(',');
Serial.print('1');
Serial.print('\r');  

while(Serial.available() < 3);
str[0] = (char)Serial.read();
str[1] = (char)Serial.read();
str[2] = (char)Serial.read();  
if(str[0] == 'A' && str[1] == 'O' && str[2] == 'K') {
  success = true;
} else {
  success = false;
  digitalWrite(failOuts[0],HIGH);
}
delay(100);
Serial.flush();
} else {
//Set the baudrate
Serial.print('S');
Serial.print('U');
Serial.print(',');
Serial.print('5');
Serial.print('7');
Serial.print('\r');  

while(Serial.available() < 3);
str[0] = (char)Serial.read();
str[1] = (char)Serial.read();
str[2] = (char)Serial.read();  
if(str[0] == 'A' && str[1] == 'O' && str[2] == 'K') {
  success = true;
} else {
  success = false;
  digitalWrite(failOuts[1],HIGH);
}
delay(100);
Serial.flush();

//Set the remote MAC address
Serial.print('S');
Serial.print('R');
Serial.print(',');
for(int i = 0; i < 12; i++) {
  Serial.print(mac[i]);
}
Serial.print('\r');  

while(Serial.available() < 3);
str[0] = (char)Serial.read();
str[1] = (char)Serial.read();
str[2] = (char)Serial.read();  
if(str[0] == 'A' && str[1] == 'O' && str[2] == 'K') {
  success = true;
} else {
  success = false;
  digitalWrite(failOuts[2],HIGH);
}
delay(100);
Serial.flush();

//Set the passkey
Serial.print('S');
Serial.print('P');
Serial.print(',');
for(int i = 0; i < 4; i++) {
  Serial.print(passkey[i]);
}
Serial.print('\r');  

while(Serial.available() < 3);
str[0] = (char)Serial.read();
str[1] = (char)Serial.read();
str[2] = (char)Serial.read();  
if(str[0] == 'A' && str[1] == 'O' && str[2] == 'K') {
  success = true;
} else {
  success = false;
  digitalWrite(failOuts[3],HIGH);
}
delay(100);
Serial.flush(); 

//Set the BlueSMiRF mode
Serial.print('S');
Serial.print('M');
Serial.print(',');
Serial.print('3');
Serial.print('\r');

while(Serial.available() < 3);
str[0] = (char)Serial.read();
str[1] = (char)Serial.read();
str[2] = (char)Serial.read();  
if(str[0] == 'A' && str[1] == 'O' && str[2] == 'K') {
  success = true;
} else {
  success = false;
  digitalWrite(failOuts[4],HIGH);
}
delay(100);
Serial.flush();

delay(100);
//Exit command mode
} 
Serial.print('-');
Serial.print('-');
Serial.print('-');
Serial.print('\r');

//delay(100);
//Serial.flush();
//delay(100);
//Serial.end();
//digitalWrite(BLUESMIRFON, LOW);
}
CleanProgramBlueSMiRF CleanProgramBlueSMiRF 
4

1 回答 1

0

如果您复制了包括行号在内的确切错误消息,将会有所帮助。

我认为它失败了:

char mac[13] = "74-E5-43-BE-42-10";

因为引号之间有 17 个字符,而您分配了 13 个字符。

于 2013-10-03T19:53:46.767 回答