Go Down

Topic: system hangs while uploading (Read 802 times) previous topic - next topic

AlyTech

Something quite strange is happening to me : the code hereunder makes the system hang (see attached picture) when I try to upload it on a Mega2560.
The code compiles ok.

Code: [Select]

bool rdmOn;
int txEnabledPin = 7;
int ledPin = 2;
uint8_t msg[32];
uint8_t nbrOct_msg;
uint8_t pos_msg;

uint8_t cmd[32];
uint8_t nbrOct_cmd;

unsigned long timeDeb;
unsigned long timeFin;

unsigned short measure[20];
int posRDM,nbrRDM;

void setup()
{
  pinMode(ledPin,OUTPUT);
 
  Serial.begin(9600); //57600);
  rdmOn=false;

  pinMode(txEnabledPin,OUTPUT);
  digitalWrite(txEnabledPin,LOW);
 
  nbrOct_msg = 0;
  pos_msg = 0;
 
  nbrRDM = 2;
  posRDM = 0;
}

void loop()
{
  posRDM++; if (posRDM == nbrRDM) posRDM = 0;

  while (Serial.available())
  {
    if (nbrOct_msg == 0)
    {
      nbrOct_msg = (uint8_t)Serial.read();
      pos_msg = 0;
    }
    else
    {
       msg[pos_msg] = (uint8_t)Serial.read();
       pos_msg++;
    }
  }
 
  if (nbrOct_msg == 0)
    return;
  if (pos_msg < nbrOct_msg)
    return; 
 
  nbrOct_cmd = nbrOct_msg + 2; // CRC
  for (uint8_t i=0; i<nbrOct_msg; i++)
    cmd[i] = msg[i];

  Serial.print("???");
  for (uint8_t i=0; i<nbrOct_cmd; i++)
    Serial.write(cmd[i]);
  Serial.print("!!!");

  nbrOct_msg = 0;
}


Note that other codes can be uploaded without any problem (as the one hereunder), so I cannot beleive the Mega2560 is crashed :
Code: [Select]

int indexRelai;
bool relaiOn;

void setup() {
  // initialize both serial ports:
  Serial.begin(9600);
  indexRelai=3;
  relaiOn=false;
  pinMode(7,OUTPUT);
  digitalWrite(7,HIGH); // pour forcer sortie à zéro...
  //pinMode(14,OUTPUT);
  //digitalWrite(14,LOW);
  Serial3.begin(9600);
  pinMode(4,OUTPUT);
}

void loop() {
  indexRelai++;
  if (indexRelai==4)
  {
    indexRelai = 0;
    relaiOn=(!relaiOn);
  }
  uint8_t msg[8];
  msg[0]='#';msg[1]='0';msg[2]='1'; msg[3]='1';
  msg[4]='0'+indexRelai;
  msg[5]='0';
  if (relaiOn) msg[6]='1'; else msg[6]='0';
  msg[7]=13;
  digitalWrite(7,HIGH);
  delay(10);
  for (int i=0; i<8; i++)
  {
    Serial3.write(msg[i]);
    //delay(5);
  }
  //Serial3.write(msg,8);
  delay(10);
  digitalWrite(7,LOW);
  Serial.write(msg,8);
 
  if (relaiOn)
    digitalWrite(2,HIGH);
  else
    digitalWrite(2,LOW);
  delay(1000);
}


I tried to reduce the amount of memory used by global variables... no effect.

Does anyone have any idea ?
When such troubles appear, where can I find some resource to help me (discussions on the way the code is generated, maybe ?).

Thank you very much in advance !

AWOL

Remove the print with three exclamation marks.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

AlyTech

...This effectively solves my problem... but it looks a bit crazy to me, isn't it ?
Any good reason for such a problem ? How can I crash the system by simply writing some text ? Where can I find some resources describing these strange traps, so I can escape from them ?

Thank You for Your help !

AWOL

It's an escape code to put the Mega's boot loader into debug mode.
Obviously, no-one would ever dream of putting three exclamation marks into a string.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

AlyTech

Sorry if I did !!! And I really hope that this message will appear correctly, even if I wrote the forbidden three characters, shame on me, I am certainly not a human bean to do such strange things...

Go Up