Two equal programms

Hi guys

My problem is that I run out of SoftwareSerial pins on the Arduino Pro Micro, so I’ve to use Serial1 (TX and RX pin) which isn’t a problem because it’s seperated on the Arduiono Pro Micro. But somehow if I switch my programm to Serial1 it doesnt work anymore. If I’m using the SoftwareSerial pin it works (but then I need to disconnect my other module, so my project will not work completely)

Could you help me and find the error?

Working Code (SoftwareSerial):

/*For Arduino board with multiple serial ports such as DUE board, comment out the above two codes, and directly use Serial1 port*/
int dist;// LiDAR actually measured distance value
int strength;// LiDAR signal strength
int check;// check numerical value storage
int i;
int uart[9];// store data measured by LiDAR
const int HEADER=0x59;// data package frame header
#include <SoftwareSerial.h>
SoftwareSerial Serial2(8,8); 
void setup()
{
  Serial.begin(115200);//set the Baud rate of Arduino and computer serial port
  Serial2.begin(115200);//set the Baud rate of LiDAR and Arduino serial port
}
void loop()
{
  if (Serial2.available())//check whether the serial port has data input
  {
    if(Serial2.read()==HEADER)// determine data package frame header 0x59
    {
      uart[0]=HEADER;
      if(Serial2.read()==HEADER)//determine data package frame header 0x59
      {
        uart[1]=HEADER;
        for(i=2;i<9;i++)// store data to array
        {
          uart[i]=Serial2.read();
        }
      check=uart[0]+uart[1]+uart[2]+uart[3]+uart[4]+uart[5]+uart[6]+uart[7];
      if(uart[8]==(check&0xff))// check the received data as per protocols
      {
        dist=uart[2]+uart[3]*256;// calculate distance value
        strength=uart[4]+uart[5]*256;// calculate signal strength value
        Serial.print("dist = ");
        Serial.print(dist);// output LiDAR tests distance value
        Serial.print('\t');
        Serial.print("strength = ");
        Serial.print(strength);// output signal strength value
        Serial.print('\n');
       }
     }
   }
 }
}

Not working code (Hardware Serial):

/*For Arduino board with multiple serial ports such as DUE board, comment out the above two codes, and directly use Serial1 port*/
int dist;// LiDAR actually measured distance value
int strength;// LiDAR signal strength
int check;// check numerical value storage
int i;
int uart[9];// store data measured by LiDAR
const int HEADER=0x59;// data package frame header
void setup()
{
  Serial.begin(115200);//set the Baud rate of Arduino and computer serial port
  Serial1.begin(115200);//set the Baud rate of LiDAR and Arduino serial port
}
void loop()
{
  if (Serial1.available())//check whether the serial port has data input
  {
    if(Serial1.read()==HEADER)// determine data package frame header 0x59
    {
      uart[0]=HEADER;
      if(Serial1.read()==HEADER)//determine data package frame header 0x59
      {
        uart[1]=HEADER;
        for(i=2;i<9;i++)// store data to 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))// check the received data as per protocols
      {
        dist=uart[2]+uart[3]*256;// calculate distance value
        strength=uart[4]+uart[5]*256;// calculate signal strength value
        Serial.print("dist = ");
        Serial.print(dist);// output LiDAR tests distance value
        Serial.print('\t');
        Serial.print("strength = ");
        Serial.print(strength);// output signal strength value
        Serial.print('\n');
       }
     }
   }
 }
}

Somehow I’m thinking that the Micro reads something differrent on the Hardware Pins than on the Serial Pins. With SoftwareSerial setup I get the distance and the strength and with the HardwareSerial setup I get nothing because the data looks “different”

I am surprised that either of the programs work

SoftwareSerial is not reliable at 115200 baud and neither program checks whether the full amount of data is available before reading multiple bytes

My problem is that I run out of SoftwareSerial pins on the Arduino Pro Micro

What exactly does that mean ?

  if (Serial2.available())//check whether the serial port has data input

Change that to:

  if (Serial2.available() >= 9) //check whether the serial port has ENOUGH data input

johnwasser:

  if (Serial2.available())//check whether the serial port has data input

Change that to:

  if (Serial2.available() >= 9) //check whether the serial port has ENOUGH data input

I have tried this but still no success :frowning:
Any ideas?

Can you post an example of the message you are trying to read. Your code seems unnecessarily repetitive.

The most likely cause of the different behaviour (IMHO) is because HardwareSerial works better than SoftwareSerial.

...R