Trouble in serial read

SYSTEM_MODE(MANUAL);
#include "Serial4/Serial4.h"
#include "Serial5/Serial5.h"

/// variables //////////////////////

boolean lock5 = false;
boolean lock4 = false;
boolean lock1 = false;
int ready = 0;

unsigned char rec_serial1[100],rec_serial5[100],rec_serial4[100];
char string1[200];
static int m=0,n5=0,n4=0;

char actualwt[7];
char actualwt1[7];
char actualcardidout[25];
char actualcardidin[25];
int terminal;

////////////////////////////////////


void setup() 
{
  //Particle.subscribe("hook-response/weight", myHandler, MY_DEVICES);   
  pinMode( D7, OUTPUT);
  pinMode( D4, OUTPUT);
  pinMode( D0, OUTPUT);
  pinMode( D2, OUTPUT);    
  
  Serial.begin(9600);
  Serial1.begin(2400);  weigh machine
 Serial4.begin(57600);//RFID card out 57600
  Serial5.begin(57600);//RFID card in 57600
  
}


void loop() {
   if(ready == 1)
   {
	   send_in();
	   
	   ready = 0;
   }
   else if(ready == 2)
   {
	  send_out();
	   
	  ready = 0; 
   }
   else 
   {
	  //ready = 0; 
   }
}

void send_in()
{
	  digitalWrite(D7, HIGH);
	 
	  memset(string1,0x00,sizeof(string1));
	 
	  sprintf(string1,"{\"terminal\":\"%d\",\"cardid\":\"%s\",\"weight\":\"%s\"}",terminal,actualcardidin,actualwt1);
                               
    
	 Serial.println(string1);                            
     
	 
	  memset(actualcardidin,0x00,sizeof(actualcardidin));  
      memset(actualwt1,0x00,sizeof(actualwt1)); 
                                     
      int lm=Serial4.available();
      for(int im=0;im<=lm;im++)
      Serial4.read();
                                     
      lm = 0;
      lm=Serial5.available();
      for(int im=0;im<=lm;im++)
      Serial5.read();
                                     
      lm=0;
      lm=Serial1.available();
      for(int im=0;im<=lm;im++)
      Serial1.read();
      
	  lock4 = false;
	  
	  lock5 = false;
	  
	  lock1 = false; 
	  
	  ready = 0;
	  
	  digitalWrite(D7, LOW);
}

void send_out()
{
	  digitalWrite(D7, HIGH);
	 
	  memset(string1,0x00,sizeof(string1));
	 
	  sprintf(string1,"{\"terminal\":\"%d\",\"cardid\":\"%s\",\"weight\":\"%s\"}",terminal,actualcardidout,actualwt1);
                
      
	   Serial.println(string1);				  
     
	  memset(actualcardidout,0x00,sizeof(actualcardidout));  
      memset(actualwt1,0x00,sizeof(actualwt1)); 
                                     
      int lm=Serial4.available();
      for(int im=0;im<=lm;im++)
      Serial4.read();
                                     
      lm = 0;
      lm=Serial5.available();
      for(int im=0;im<=lm;im++)
      Serial5.read();
                                     
      lm=0;
      lm=Serial1.available();
      for(int im=0;im<=lm;im++)
      Serial1.read();
      
      lock4 = false;
      
	  lock5 = false;
	  
	  lock1 = false; 
	  
	  ready = 0;
	  
	  digitalWrite(D7, LOW);
}


void serialEvent1()
{    
    if(lock1 == true)
    {
        unsigned char a = Serial1.read();
        
	  	if((strlen(actualcardidin)>=20) || (strlen(actualcardidout)>=20))
        {
            rec_serial1[m] = a;
           
           m++;
			Serial.println(rec_serial1[m]);
                     Serial.println("in if condition");
         Serial.printlnf("m=%d",m);
            if(m>=10)
            {
                        m=0;
                 
                        sprintf(actualwt,"%02X%02X%02X%02X%02X%02X",rec_serial1[2],rec_serial1[3],rec_serial1[4],rec_serial1[5],rec_serial1[6],rec_serial1[7]);
                        Serial.println(actualwt);
						memset(rec_serial1,0x00,sizeof(rec_serial1));
                       
                         if(strlen(actualcardidin)>=20)
                           {
                               Serial.println("in in uhf");
                               if(strlen(actualwt)>=6)
                              {  
                                    terminal = 1;
									ready = 1;
									
									
								  strncpy(actualwt1, actualwt, 7);
								  memset(actualwt,0x00,sizeof(actualwt)); 
								  		lock1 = false; 
                              }
                           }
						   else{
							   Serial.println("no_weight");
						   }
                          if(strlen(actualcardidout)>=20)
                           {
                              if(strlen(actualwt)>=6)
                              {  
                                     terminal = 2;
                                     ready = 2;
                                     	Serial.print("out");
                                     	Serial.println(actualwt);
                                
								strncpy(actualwt1, actualwt, 7);
								  memset(actualwt,0x00,sizeof(actualwt)); 
                                     lock1 = false; 
                              }
                           }
							else{
							   Serial.println("no_weight");
						   }
            }
        }
    }
    else
    {
       int l1=Serial1.available();
       Serial.println(l1);
       for(int im=0;im<=l1;im++)
             Serial1.read();
    }
}


void serialEvent4()
{
      Serial.println("in uhf");
     if(lock4==false)
     {
        unsigned char b = Serial4.read();

        rec_serial4[n4]=b;
       // n4=n4+1;
        n4++;
        
        if(n4>=10)
           {
              sprintf(actualcardidin,"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",rec_serial4[0],rec_serial4[1],rec_serial4[2],rec_serial4[3],rec_serial4[4],rec_serial4[5],rec_serial4[6],rec_serial4[7],rec_serial4[8],rec_serial4[9]);
              memset(rec_serial4,0x00,sizeof(rec_serial4));
              memset(actualcardidout,0x00,sizeof(actualcardidout));
		      Serial.println(actualcardidin);
			  n4=0;
              lock4=true;
              ///blinkled2();
              lock1=true;
           }  
     }
     else
     {
      int l4=Serial4.available();
      Serial.println(l4);
      for(int im=0;im<=l4;im++)
           Serial4.read();
            lock5=false;     ////viraj 
            
     }           
}


void serialEvent5()
{
    Serial.println("out uhf");
     if(lock5==false)
     {
        unsigned char c = Serial5.read();
        rec_serial5[n5]=c;
       n5++;
       if(n5>=10)
           {
             sprintf(actualcardidout,"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",rec_serial5[0],rec_serial5[1],rec_serial5[2],rec_serial5[3],rec_serial5[4],rec_serial5[5],rec_serial5[6],rec_serial5[7],rec_serial5[8],rec_serial5[9]);
			 memset(rec_serial5,0x00,sizeof(rec_serial5));
			 memset(actualcardidin,0x00,sizeof(actualcardidin));
		     Serial.println(actualcardidout);
         	 n5=0;
             lock5=true;
           ///  blinkled4();
             lock1=true;
           }  
     }
     else
     {
      int l5 = Serial5.available();
      Serial.println(l5);
      for(int im=0;im<=l5;im++)
           Serial5.read();
       lock4=false;     ////viraj
       
     }
}

the code in void loop() checks for if condition sends data to serial port of electron of serial 4 & 1 OR
serial 5 &1
the code reads any UHF data Serial event 4 or 5 at 57600 baud rate , then only seial event 1 at (2400 baud rate) read is considered
but serial event 1 gets always garbage value in variable actualwt sometimes its correct

anybody can help me please

1 Like
  lm=0;
  lm=Serial1.available();
  for(int im=0;im<=lm;im++)
  Serial1.read();

Are you actually attempting to just empty the Serial1 RX buffer and move on?

How is the data that comes from the “weigh machine” actually formatted?

yes i’m emptying serial buffer in loop() , if data come in serial buffer ,while i am in my function

wieght machine data is in ascii format with 11 ascii values at one time comes in

in above code
sprintf(actualwt,"%02X%02X%02X%02X%02X%02X",rec_serial1[2],rec_serial1[3],rec_serial1[4],rec_serial1[5],rec_serial1[6],rec_serial1[7]);

is changed to
sprintf(actualwt,"%c%c%c%c%c%c",rec_serial1[2],rec_serial1[3],rec_serial1[4],rec_serial1[5],rec_serial1[6],rec_serial1[7]);

but no results

then why are you arbitrarily emptying the buffer? wouldn’t some of that be considered part of the data you want to recieve

It is difficult to understand what you are actually trying to do in serialEvent1()

how often does the weigh machine transmit the data?

It is difficult to understand what you are actually trying to do in serialEvent1()
—> if any uhf reader data is received in serial event 4 or 5 it makes lock1 variable true and will consider then weight data otherwise just emptying data in else condition

and checking if uhf data is length is >= 20 then storing in weight array and if weight data is >= 10 then making ready = 1 or 2 to print both UHF reader data and weight data

how often does the weigh machine transmit the data?

–> it depends on vehicles present at site nearly 60 seconds of interval.

–> the flow is like vehicles comes, uhf reader detects the tag in serial event 4 or 5 and then weight is sent by weight machine in event serial 1

FYI, you need not perform this memset() before the sprintf() when you are working with C strings:

	  memset(string1,0x00,sizeof(string1));
	 
	  sprintf(string1,"{\"terminal\":\"%d\",\"cardid\":\"%s\",\"weight\":\"%s\"}",terminal,actualcardidin,actualwt1);

sprintf() will place the null terminator at the end of the string.

I’d refactor that code (getting away from the serialEvent paradigm with all those global variables) and put your logic into your loop function, as it ought to be.

from an outside perspective, it is not easy to see what you are doing with each serial input/output.