Getting "char touper" from Serial.read() and passing byte to Serialport (sensor)

Hi,

This code was functioning somehow two days ago, but not any more.

I want to receive char c = toupper(Serial.read()); from Serial input and pass the correct command to the TF-luna sensor. If I write “1”, then arduino Nano should pass byte message110 = {0x5A, 0x06, 0x03, 0x0A, 0x00, 0x00}; to the sensor.

Now, when I write “1” or “T”, nothing happens with the sensor output. It should be changing the frequency, but isnt.
Thanks for the help.

#include <SoftwareSerial.h> //header file of software serial port
SoftwareSerial Serial1(3, 2); //define software serial port name as Serial1 and define pin2 as RX and pin3

/* For Arduinoboards with multiple serial ports like DUEboard, interpret above two pieces of code and
  directly use Serial1 serial port*/
int dist; //actual distance measurements of LiDAR
int strength; //signal strength of LiDAR
float temprature;
int check; //save check value
int i;
int uart[9]; //save data measured by LiDARss
const int HEADER = 0x59; //frame0 header of data package
byte message00[] = {0x5A, 0x06, 0x03, 0x00, 0x00, 0x00};//00
byte message110[] = {0x5A, 0x06, 0x03, 0x0A, 0x00, 0x00};//10hz
byte message120[] = {0x5A, 0x06, 0x03, 0x14, 0x00, 0x00};//20hz
byte message130[] = {0x5A, 0x06, 0x03, 0x1E, 0x00, 0x00};//30hz
byte message160[] = {0x5A, 0x06, 0x03, 0x3C, 0x00, 0x00};//60hz
byte message1100[] = {0x5A, 0x06, 0x03, 0x64, 0x00, 0x00};//100hz
byte message1200[] = {0x5A, 0x06, 0x03, 0xC8, 0x00, 0x00};//200hz

byte messageTrigger[] = {0x5A, 0x04, 0x04, 0x00};// triggermode
byte messagePower[] = {0x5A, 0x06, 0x35, 0x0A, 0x00, 0x00}; // powersave and 10hz
byte messageSave[] = {0x5A, 0x04, 0x11, 0x00}; // Save 



void setup() {

  Serial.begin(115200); //set bit rate of serial port connecting Arduino with computer
  Serial1.begin(115200); //set bit rate of serial port connecting LiDAR with Arduino
}
void loop() {
  if (Serial1.available()) { //check if serial port has data input
    if (Serial1.read() == HEADER) { //assess data package frame header 0x59
      uart[0] = HEADER;
      if (Serial1.read() == HEADER) { //assess data package frame header 0x59
        uart[1] = HEADER;
        for (i = 2; i < 9; i++) { //save data in array
          uart[i] = Serial1.read();
        }
        check = uart[0] + uart[1] + uart[2] + uart[3] + uart[4] + uart[5] + uart[6] + uart[7];
        if (uart[8] == (check & 0xff)) { //verify the received data as per protocol
          dist = uart[2] + uart[3] * 256; //calculate distance value
      
          Serial.print("dist = ");
          Serial.println(dist); //output measure distance value of LiDAR
         {
          if (Serial.available()>0){
           
          char c = toupper(Serial.read());
         
             if ( c == '0'){Serial1.write(message00, sizeof(message00));}
     if ( c =='1'){Serial1.write(message110, sizeof(message110));}
     if ( c == '2'){Serial1.write(message120, sizeof(message120));}
     if ( c == '3'){Serial1.write(message130, sizeof(message130));}
     if ( c == '6'){Serial1.write(message160, sizeof(message160));}
     if ( c == '7'){Serial1.write(message1100, sizeof(message1100));}
     if ( c == '8'){Serial1.write(message1200, sizeof(message1200));}
     if ( c == 'T'){Serial1.write(messageTrigger, sizeof(messageTrigger));}
     if ( c == 'P'){Serial1.write(messagePower, sizeof(messagePower));}
     if ( c == 'S'){Serial1.write(messageSave, sizeof(messageSave));}
      }
        }
        }
      }
    }
  }
}

Generally software serial isn’t reliable at such high speeds.

Why are you checking for two header bytes?

Also, you have no guarantee that there is anything available to read when you do the second one.

  if (Serial1.available())   //check if serial port has data input

You then go on to read 9 characters. You should change this to:

  if (Serial1.available() >= 9)   //check if serial port has enough data input
        check = uart[0] + uart[1] + uart[2] + uart[3] + uart[4] + uart[5] + uart[6] + uart[7];
        if (uart[8] == (check & 0xff))   //verify the received data as per protocol

You can simplify this to:

        byte checksum = uart[0] + uart[1] + uart[2] + uart[3] + uart[4] + uart[5] + uart[6] + uart[7];
        if (uart[8] == checksum)   //verify the received data as per protocol

By using a byte for 'checksum' you don't need to mask off the upper bits to get a byte.

            if (Serial.available() > 0)
            {


              char c = toupper(Serial.read());


              if ( c == '0')
              {
                Serial1.write(message00, sizeof(message00));
              }
              if ( c == '1')
              {

The switch/case statement is designed to replace this common string of 'if' statements:

            if (Serial.available() > 0)
            {
              switch (toupper(Serial.read()))
              {
                 case '0': Serial1.write(message00, sizeof(message00)); break;
                 case '1': Serial1.write(message110, sizeof(message110)); break;