Serial Communications

Thanks for reading my question, I’m developing a serial script with two elements

  1. Single Char naviation either int or alpha
  2. Fetch user input for values up to 128000

There is a roll over at 32767, which I thought was an odd number, I was expecting it to roll over at 32k or 32256 for my mico.

The issue I have is 2, SER_INT mode. I kind of understand why the rollover is happening but I’m not sure of the approach to fix it? and not sure why it’s rolling over @ 32767

Right now I can only do values up to 32767 and want to get up to 128k

#include <MemoryFree.h> //not required after Debug
#include <EEPROM.h>
#include <Event.h>
#include <Timer.h>
#include <Time.h> 




//Local Vars for development
const int Debug_IO11 = 11; //Debug IO13 pin 18
int tog = 0;
const int LED = 13;
int blnk1 = 0;
int blnk2 = 0;

//serial com vars and constants
int serIntIn          = 0; //used for numeric UI input values: selected with SER_INT
byte serByteIn        = 0; //used for single character menu navigation: selected with SER_BYTE
String serStringIn    = ""; //This value is always available no selection required. BECAREFUL
const int SER_INT = 1;
const int SER_BYTE= 2;


void setup() 
{
  pinMode(LED, OUTPUT);
  Serial.begin(19200);	// opens serial port, sets data rate to 9600 bps
 
}

void loop() {

rxSerial( SER_INT );
delay(100);
digitalWrite (Debug_IO11, tog); //rev 1.72 Blink the Green LED when doFreeRun is done. Debug_IO11 = 11; //Debug IO13 pin 18
tog = 1 - tog;
}

bool rxSerial(int retSel)
{
  char charArr[255];
  bool serExit = false;
  int idx1   = 0;
  int idx2   = 0;
  //clean globals 
  serIntIn    = 0;
  serByteIn   = 0;
  serStringIn = "";
  
      digitalWrite (LED, blnk1); //rev 1.72 Blink the Green LED when doFreeRun is done.
      blnk1 = 1 - blnk1; // rev 1.72 invert for next use replaces inverter mBlink = !mBlink; to make clearer?
      
      do{
          if (Serial.available() > 0){
              //delay(100);
              serByteIn = Serial.read();
              
              if (int(serByteIn) == 13){ // 13Dec - Carriage Return
                  serExit = true;
               }
               
               if ((int(serByteIn) == 8) && (idx1 > 0)){ //backspace or delete
                  idx1--;
               }else{
                  charArr[idx1++] = serByteIn;
               }
               switch (retSel){
                  case (1): 
                      Serial.write(serByteIn); //UI echo
                      break;
                  case (2):  
                      Serial.print(serByteIn); //UI echo
                      break;
               }
          }
      }while(!serExit);
      
      if (idx1 == 1){ 
          return false; //return if they only hit enter key
      }else{
          //convert char array to string
          for (idx2 = 0; idx2 < idx1; idx2++){ 
              serStringIn += charArr[idx2];
          }
          //Select the proper serial var to use
          switch (retSel){
              case (1): 
                  serIntIn = serStringIn.toInt();
                  doPrintMeInt(serIntIn);
                  break;
              case (2):  //use Print.write();
                  serByteIn = charArr[0];
                  doPrintMeByte(serByteIn);
                  break;
          }
          return true;
      }

  
}


void doPrintMeInt(int serByteIn)
{
      digitalWrite (LED, blnk2); //rev 1.72 Blink the Green LED when doFreeRun is done.
      blnk2 = 1 - blnk2; // rev 1.72 invert for next use replaces inverter mBlink = !mBlink; to make clearer? 
    Serial.println("");
		Serial.println("I received:");
    Serial.write("W Byte");
    Serial.print("\t");
    Serial.print("P Byte");
    Serial.print("\t");
		Serial.print("P DEC");
    Serial.print("\t");
    Serial.print("P BIN");
    Serial.print("\t");    
    Serial.print("P OCT"); 
    Serial.print("\t");
    Serial.write("W DEC"); 
    Serial.print("\t");
    Serial.print("P HEX");
    Serial.println("");
    Serial.write(serByteIn);
    Serial.print("\t");
    Serial.print(serByteIn);
    Serial.print("\t");
		Serial.print(serByteIn, DEC);
    Serial.print("\t");
    Serial.print(serByteIn, BIN);
    Serial.print("\t");    
    Serial.print(serByteIn, OCT); 
    Serial.print("\t");
    Serial.print(serByteIn, DEC); 
    Serial.print("\t");
    Serial.print(serByteIn, HEX);
    Serial.println("");
      digitalWrite (LED, blnk2); //rev 1.72 Blink the Green LED when doFreeRun is done.
      blnk2 = 1 - blnk2; // rev 1.72 invert for next use replaces inverter mBlink = !mBlink; to make clearer?       

}

void doPrintMeByte(byte serByteIn)
{
    digitalWrite (LED, blnk2); //rev 1.72 Blink the Green LED when doFreeRun is done.
    blnk2 = 1 - blnk2; // rev 1.72 invert for next use replaces inverter mBlink = !mBlink; to make clearer?
    Serial.println("");
		Serial.println("I received:");
    Serial.write("W Byte");
    Serial.print("\t");
    Serial.print("P Byte");
    Serial.print("\t");
		Serial.print("P DEC");
    Serial.print("\t");
    Serial.print("P BIN");
    Serial.print("\t");    
    Serial.print("P OCT"); 
    Serial.print("\t");
    Serial.write("W DEC"); 
    Serial.print("\t");
    Serial.print("P HEX");
    Serial.println("");
    Serial.write(serByteIn);
    Serial.print("\t");
    Serial.print(serByteIn);
    Serial.print("\t");
		Serial.print(serByteIn, DEC);
    Serial.print("\t");
    Serial.print(serByteIn, BIN);
    Serial.print("\t");    
    Serial.print(serByteIn, OCT); 
    Serial.print("\t");
    Serial.print(serByteIn, DEC); 
    Serial.print("\t");
    Serial.print(serByteIn, HEX);
    Serial.println("");   
      digitalWrite (LED, blnk2); //rev 1.72 Blink the Green LED when doFreeRun is done.
      blnk2 = 1 - blnk2; // rev 1.72 invert for next use replaces inverter mBlink = !mBlink; to make clearer?
}

32k = 0 - 32767

int (16 bit) - signed number from -32768 to 32767. unsigned int (16 bit) or 0 - 65535 long (32 bit) - signed number from -2,147,483,648 to 2,147,483,647. unsigned long (32 bit) - unsigned number from 0-4,294,967,295.

See: https://learn.sparkfun.com/tutorials/data-types-in-arduino

Thanks Larry, very helpful...now I know why and thought I could just change

long int serIntIn = 0; but that didn't seem to work, I thought it should have?

What happens if you change:

              if (int(serByteIn) == 13){ // 13Dec - Carriage Return

to

              if (serByteIn == '\n') { // 13 Dec - Carriage Return

The Serial object likes a newline character when the user presses Enter.

Actually tried that idea, and it won't see the Carriage Return at all because it's a byte, I think that is why

...it won't see the Carriage Return at all because it's a byte, I think that is why.

Really? Then shit-can your input routine and the String class variable and try:

  //String serStringIn    = ""; get rid of this, it's the lazy man's way of doing things and very expensive
  char serStringIn[50];    // Make whatever length seems appropriate

  // Your input code block

  int charsRead;

  if (Serial.available() > 0) {
     charsRead = readBytesUntil('\n', serStringIn, sizeof(serStringIn) - 1);  // Save room for null
     serStringIn[charsRead] = '\0';
  }

After the if statement block has executed, serStringIn[] contains all of the input from the user as a null-terminated string. (The newline character was overwritten by the null string termination character.) You should be able to post-process the string as you need to.

Have a look at the examples in serial input basics. Simple and reliable and non-blocking.

Don't try to analyze the data until after you have received all of it.

...R

Did some more trouble shooting and found the following:

toInt() returns long int and I had serIntIn and doPrintMeInt(int serByteIn) both as int

That is why it would choke

So to solve the problem all I had to do

Find

unsigned int serIntIn  = 0; //used for numeric UI input values: selected with SER_INT

Replace With

unsigned long serIntIn  = 0; //used for numeric UI input values: selected with SER_INT

Find

void doPrintMeInt(int serByteIn)

Replace With

void doPrintMeInt(unsigned long serByteIn)

Works as intended :)

"Typing" is important.

I think you have learned something important. ;)