Go Down

Topic: [REOPEN] : After 1 or 2 days serial port not working and need to restart device (Read 80 times) previous topic - next topic

jsinghvirdi

After 1 or 2 days serial port not working and need to restart device and again it will start communication. please read my code may be i have missed something.

Code: [Select]


/*-----( Import needed libraries )-----*/
#include <SoftwareSerial.h>

#include <Wire.h>
#include <LiquidCrystal_PCF8574.h>

/*-----( Declare Constants and Pin Numbers )-----*/
#define SSerialRX        10  //Serial Receive pin
#define SSerialTX        11  //Serial Transmit pin

LiquidCrystal_PCF8574 lcd(0x3F);

/*-----( Declare objects )-----*/
SoftwareSerial RS485Serial(SSerialRX, SSerialTX); // RX, TX

#define SSerialTxControl 36   //RS485 Direction control
#define RS485Transmit    HIGH
#define RS485Receive     LOW
#define Pin13LED         13

byte byte_receive;
byte  state=0;
byte  cont=1;
byte  trace_OK=0;
byte  Responce[22];
byte  dataSerial[12];
unsigned long  times = 0;
char company[] = "ABCD";



void setup()   
{

RS485Serial.begin(9600);
 
pinMode(Pin13LED, OUTPUT);   
pinMode(SSerialTxControl, OUTPUT);   

digitalWrite(SSerialTxControl, RS485Receive);     

Serial2.begin(9600);

 lcd.begin(20, 4); // initialize the lcd
  lcd.setBacklight(200);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(company);

 

}//--(end setup )---


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{



  // Show activity
if (Serial2.available())
{
 
  while (Serial2.available() > 0){

  byte_receive=Serial2.read();
 
   if (byte_receive == 58){ //if first byte is <
        state=1;
        cont=1;
        Responce[0] = byte_receive;
      } else if (state==1 && cont<22){ // if second byte received then save rest bytes in another array
        Responce[cont] = byte_receive;
        state=1;
        cont=cont+1;
     
      if(cont==22){
        cont=1;
        state=0;

       if(Responce[1] == 67 && Responce[4] == 80 && Responce[5]==73 && Responce[6]==78 && Responce[7]==71 && Responce[20] == 66 && Responce[21] == 59)
        {   
             Serial2.write((char)60);
             Serial2.write((char)65);
             Serial2.write((char)Responce[4]);
             Serial2.write((char)Responce[5]);
             Serial2.write((char)Responce[6]);
             Serial2.write((char)Responce[7]);
             Serial2.write((char)Responce[8]);
             Serial2.write((char)Responce[9]);
             Serial2.write((char)Responce[10]);
             Serial2.write((char)Responce[11]);
             Serial2.write((char)90);
             Serial2.write((char)62);
        }       
       
        else if(Responce[1] == 67 && Responce[20] == 66 && Responce[21] == 59)
        {
         
          lcd.setCursor(0,2);
          lcd.print("MCU RESPOND :");
         
          digitalWrite(SSerialTxControl, RS485Transmit);
         
          for(int i = 0; i < 22; i++){
           RS485Serial.write((char)Responce[i]);
               
          }
         
          digitalWrite(SSerialTxControl, RS485Receive);

           for(int i = 2; i < 20; i++){
             
             lcd.setCursor(i-2,3);
             lcd.print((char)Responce[i]);
                       
          }
           
        }
      }
    }

  }
     
 
     
}

if (RS485Serial.available())  //Look for data from other Arduino
 {
  while (RS485Serial.available() > 0){
  byte_receive=RS485Serial.read();
   
     if (byte_receive == 60){ //if first byte is <
        state=1;
        cont=1;
        dataSerial[0] = byte_receive;
      } else if (state==1 && cont<12){ // if second byte received then save rest bytes in another array
        dataSerial[cont] = byte_receive;
        state=1;
        cont=cont+1;
       
      if(cont==12){
        cont=1;
        state=0;

       
       if(dataSerial[1] == 65 && dataSerial[10] == 90 && dataSerial[11] == 62)
        {
          lcd.clear();
          lcd.setCursor(0,0);
          lcd.print("RS485 COMMAND :");

         
          digitalWrite(Pin13LED, HIGH);
         
          for(int i = 0; i < 12; i++){
             Serial2.write(dataSerial[i]);
             //Serial.write(dataSerial[i]);
          }
          digitalWrite(Pin13LED, LOW);
         
         
          for(int i = 2; i < 10; i++){
             
             lcd.setCursor(i-2,1);
             lcd.print((char)dataSerial[i]);
                       
          }
         
                         
           
        }
      }
    }

  }
   
 }



 

}//--(end main loop )---

MorganS

The Serial port on your PC? Then that's a PC problem.

Your code uses both SoftwareSerial and Serial2. Which one stops working?

Which Arduino? Why are you using SoftwareSerial on an Arduino which has more than one Serial port?
"The problem is in the code you didn't post."

jsinghvirdi

Thanks for reply, PCB is already designed so, we have to use it Software Serial for demo purpose,

and YES software Serial is not working it will stop listening RS485 commands and we need to restart device.

Right now i am testing the device on serial1, serial2 and serial3 port and its working fine.

Is it problem in Software Serial?

Means for production Software Serial is bad idea..


MCU = ATMEGA2560-AU

MorganS

Yes, SoftwareSerial is a bad idea for production.

Sounds like you need a new PCB for production. For testing, can you run jumper wires from pin 10 and 11 to one of the other hardware serial pairs?
"The problem is in the code you didn't post."

jsinghvirdi

Yes, we can. i will do it and let you know..

Thanks Brother...

Have a God Day...

Go Up