Serial Help, been using putty to talk with Arduino Micro

So I’ve been using Putty to talk with Arduino Micro and I have only 4 Commands or actually Char X, S, C & R

R is a resume command and the X is the exit command, I echo the Serial.read to the terminal because I had automatic resumes that seem to occur without reason

The code which works resumes ckSerialIn() if it receives a char it does not understand, so it’s doing what it’s suppose to. However, I noticed on the output of the terminal when I physically send X to stop everything and don’t touch my keyboard or mouse the loop resumes again and the chars I get are

[
2
8
~
Is there some significance to this [28~???

void loop() 
{
  char keypress;
  int idx;
//digitalWrite(Debug_IO11, HIGH); //Debug IO13 pin 18
      //Serial.print("state in ");
      //Serial.println(state);

      //jumper not installed
      if (digitalRead(FREE_RUN)){      
          if (state == FREERUN){
              state = mIDLE;
              digitalWrite(OP_SHDN_L, OP_OFF);
              //Serial.println("enter state mIDLE"); //Debug
          }
      }else{
          if ((state == mIDLE) & !SERCONTROL){
               state = FREERUN;
               digitalWrite(OP_SHDN_L, OP_ON);
               //Serial.println("state mIDLE Entering FREERUN"); //Debug
          }
      }

      //SCK   Jumper J8 to force calibration;
      if (!digitalRead(CAL_MODE)){  
          
          if (state != CALMENU){
              state = initCALMENU; //cal mode
              //Serial.println("state initCALMENU"); //Debug
          }else{
              state != CALMENU;
              //Serial.println("state CALMENU"); //Debug
          }
      }

      if (Serial.available() > 0){
        //digitalWrite(Debug_IO11, HIGH);
       
          keypress = ckSerialIn( Serial.read() ); //Serial.read(); //rev 1.5
          //Serial.end();    // Ends the serial communication once all data is received
          //Serial.begin(19200);  // Re-establishes serial communication , this causes deletion of anything previously stored in the buffer or cache
          //if (keypress == 'F'){
          //    SERCONTROL = true;
         // }
          if (SERCONTROL == false){
              //Serial.println(state); //Debug 
              Serial.println("Timer Stopped V"+Version); 
              //Serial.println(keypress); //Debug
              Serial.println("cmd R resume, S reset or C cal status or move jumper from J9 to J8 for cal menu");
              //Serial.end();    // Ends the serial communication once all data is received
              //Serial.begin(19200);  // Re-establishes serial communication , this causes deletion of anything previously stored in the buffer or cache
          }
          Serial.println(FR_INTERVAL);
          SERCONTROL = true;
          t.stop(tickEvent);
          state = mIDLE;
          digitalWrite(OP_SHDN_L, OP_OFF);
          //keypress = Serial.read(); //rev 1.5 moved to serial available() above
          //digitalWrite(Debug_IO11, HIGH); //Debug IO13 pin 18
          //delay(100);
          //digitalWrite(Debug_IO11, LOW); //Debug IO13 pin 18
          
          switch(keypress){
              case ('R'): //resume rev 1.3
                    Serial.println("Freerun Started");
                    tickEvent = t.every(FR_INTERVAL, doFreeRun); 
                    state = FREERUN; 
                    digitalWrite(OP_SHDN_L, OP_ON);   
                    SERCONTROL = false;
                    pinMode(Background, OUTPUT);  //Pre //rev 1.4  //rev 1.71
                    digitalWrite(Background, HIGH); //rev 1.4
                    break;
              case ('S'):  //reset arduino stats rev 1.3
                    Serial.println("Reset Stats");
                    //reset usedCapacity to 0.0
                    for (idx = 0;idx < 8; idx++){
                        battery[idx].usedCapacity = 0.0;
                    }
                    cycles = 0;
                    break; //rev 1.2 added missing break
              case ('C'):  // get cal and status info rev 1.3   
                    doPrintCalib();
                    break;
          }
           //digitalWrite(Debug_IO11, LOW); //Debug IO13 pin 18 
      }
//digitalWrite(Debug_IO11, HIGH); //Debug IO13 pin 18      
      switch (state){
          case (initCALMENU): //Hardware selected Jumper
               delay(500); 
               LoadSettings();
               clearAndHome();
               drawCalMenu(0);
               state = CALMENU;
               digitalWrite(OP_SHDN_L, OP_OFF);
               break;
          case CALMENU:
               serviceCalMenu();
               break;
          case mIDLE:
               break;
          case PULSE:     // pin input trigger
         // digitalWrite(Debug_IO11, HIGH); //Debug IO13 pin 18
          //delay(100);
         // digitalWrite(Debug_IO11, LOW); //Debug IO13 pin 18
               doFreeRun();
               delay(10);  //100ms debounce/holdoff why are we adding a delay here?
               state = mIDLE;  //wait for the next trigger     
               break;
          case FREERUN:   //timer trigger
               t.update();
               break;
      }
digitalWrite(Debug_IO11, HIGH); //Debug IO13 pin 18      
digitalWrite(Debug_IO11, LOW); //Debug IO13 pin 18

}
/*
 * ckSerialIn ()
 * @Desc - Sanitizes the LabView input, by validation of characters 
 *            in the event of an invalid character this function cmd 
 *            "R" for Resume cmd.
 * @Parm - chr character input
 * @retn - returns character 
 *  
 */
char ckSerialIn( char chr )
{    
      Serial.println(chr); //Debug

      switch(chr){ //Sanitize USB Input
          case ('X'): return 'X'; break;// exit cmd for loop()
          case ('S'): return 'S'; break;// reset arduino stats 
          case ('C'): return 'C'; break;// get cal and status info      
          case ('R'): return 'R'; break;// resume cmd if sent   
          default:    return 'R'; break;// resume cmd for if anything else is sent
      }
      //new code, make this function universal for all user input
      //change parameter declaration to string and return will be string
      //I think we can cast from string to char or int but need to check.           
}

Sorry about the messy code, I inherited this from someone else and have been cleaning/debugging as I go a long

I can't think of anything significant about your delinquent characters.

It looks like there is a lot of the code you have not included. Perhaps the problem lies in it? Post the complete program. If it is too long, add it as an attachement - or better still write a short program that illustrates the problem.

You have a strange way of receiving data. Why not read the data in the ckSerialIn() function so it can live up to its name.

The way you are reading single characters is error prone. Have a look at the examples in serial input basics.

...R

Again, I inherited this code and you should have seen it when I got it, really horrible, no comments, very few {}, no indent or general code alignment .

Right now it’s almost working they way I want except I was trying to figure out why when I connect via putty, hit X which stops or halts the program and then it magically starts again. That’s when I found the problem with [28~ being sent without me touching the keyboard or mouse.

This program also has another issue which I’m still trying to track down which once I hit X in the terminal and it restarts or I send R then disconnect the terminal i.e. close putty while monitoring Debug_IO11 pin 11 with a scope the loop() stops intermittently.

apollo_pulse.ino (23.2 KB)

menu.ino (10.8 KB)

settings.ino (4.11 KB)

There is an awful lot of code there - it's late and I am not going to look at it now.

Can you provide a narrative that explains how it works (or how it is supposed to work) ?

...R

Robin2:
I can't think of anything significant about your delinquent characters.

It looks like there is a lot of the code you have not included. Perhaps the problem lies in it? Post the complete program. If it is too long, add it as an attachement - or better still write a short program that illustrates the problem.

You have a strange way of receiving data. Why not read the data in the ckSerialIn() function so it can live up to its name.

The way you are reading single characters is error prone. Have a look at the examples in serial input basics.

...R

Thanks for the link as well, I'm going to implement serial port in a similar way :wink:

Narrative:
The intent of this program is to apply a current to batteries, so each output
Background
Pulse
has a series variable resistor that is wire OR'd to an opAmp to convert the voltage to a current which is then applied as a load to the batteries. I'm attempting to characterize the manufactures battery with our load currents to ensure we get the mAh out of the battery that is predicted by the manufacture.

rr1024:
Narrative:
The intent of this program is to apply a current to batteries, so each output

Sorry. I meant that you should provide a section-by-section description of how the code is supposed to work so that I won’t have to try to figure it out for myself.

I have had a quick look through the code you attached to Reply#3 and (without looking further) I see this on line 4 of menu.ino

Serial.print("[2J"); // clear screen

That looks very similar to the stray characters you are seeing. I suspect you will find the problem somewhere there.

…R

Looking at that last function there, you have a bunch of return statements, so it returns, no need to follow any of those return statements with a "break" keyword.

What is the loop( ) function supposed to do if you send 'X' ?

You get keypress='X', and then what ?

rr1024:
2
8
~
Is there some significance to this [28~???

That looks like the argument to a VT100 CSI. I haven't looked at Putty for a long time but you need to tell Putty to emulate a dumb terminal and not an intelligent (VT100 or similar) terminal.