receive-forward data stream in arduino mega 2560

Hello,

I’m currently doing arduino mega 2560 communication project with atmega128 via UART. i get some problems then i check the communication system with proteus simulator (put a virtual monitor on each UART lane) so i can find out which lane the problem stay in. here the complete details:

purpose :
the atmega128 need to send 24 data of adc (from 24 analog sensors) value through UART1 of arduino 2560 after receiving command from arduino. i did it well so far as the virtual monitor on each UART lane (UART1 of mega2560 which is used to send command and receive the data) show complete data stream except for UART0 of arduino mega that i use to display the data into serial monitor (i use tera term) or kind of GUI app.

problem :
i did some works to display all of the data but the serial monitor only show about 11 or sometimes 12 data instead of 24.

i just use simple code to read and write the data :

int thisByte, thisByteReply;
byte thisByteIndex[6] = "";
byte thisByteIndexReply[24] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};


int flag_device1, flag_device2, flag_device3, flag_device4;

int index = 0;
int indexReply = 0;




void setup()
{

Serial.begin(9600);  
Serial1.begin(9600); 

}

void loop()
{
  //evaluate the command from PC and forward to atmega128
  while (Serial.available()) 
  {
    thisByte = Serial.read();
    thisByteIndex[index] = thisByte;
    if (index < 6) 
    {
      Serial.println(thisByte, HEX); // sending echo to GUI
      index++;
      if (index > 5)
      {
        index = 0;
        thisByte = 0;
      }
    }

    detFlag();
    execute();

  }
  
  //read value from atmega and display to GUI/tera term
  while (Serial1.available())
    {
        thisByteReply = Serial1.read();
        thisByteIndexReply[indexReply] = thisByteReply;
	//Serial.println(thisByteReply);
	Serial.write(thisByteIndexReply, 24);
	//Serial.println("");
    }
}

i just start to learn arduino code into further and need help now
i appreciate any useful help
thanks

i check the communication system with proteus simulator

That was a waste of time.

You need to explain what you are trying to accomplish. For every byte received on Serial1, you send 24 bytes. That hardly makes sense.

You do something when you receive 6 bytes. That doesn't make much sense.

You forgot to post the code for the other Arduino.

thank you for quick reply

i am trying to do a kind of communication such this

  1. user gives character command ‘1qqqqz’ from pc keyboard to arduino via Serial com, then arduino will evaluate if the first char is ‘1’ and the last one is ‘z’ arduino will store that command in thisByteIndex variable and forward to atmega via serial1 (uart1). i use codevisionAVR to program the atmega.
  2. atmega receive the forwarded command and evaluate the command precisely like what arduino do before to access the 24 analog sensor and send the value of all adc data back to arduino via the same serial which is serial1 (uart1 of arduino).
  3. arduino receive all of the value of these 24 adc and display all of them to serial monitor. the problem occurs in this stage i think.

here my complete code :

int thisByte, thisByteReply;

byte thisByteIndexReply[6] = {0,0,0,0,0,0};
byte thisByteIndexReply[24] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};


int flag_device1, flag_device2, flag_device3, flag_device4;

int index = 0;
int indexReply = 0;


void setup()
{

Serial.begin(9600);  
Serial1.begin(9600); 
Serial3.begin(9600); 
}

void loop()
{
  while (Serial.available()) 
  {
    thisByte = Serial.read();
    thisByteIndex[index] = thisByte;
    if (index < 6) 
    {
      index++;
      if (index > 5)
      {
        index = 0;
        thisByte = 0;
      }
      Serial3.println(index); //just to show the index number running
    }

    detFlag();
  
    execute();

  }
  
  storeDisplay();
  
  delay(50);
}

void detFlag ()
{
    //evaluate command through thisByteIndex
    if (thisByteIndex[5] == 0x7A)
    {
        if (thisByteIndex[0] == 0x31) {flag_device1 = true;}
        else if (thisByteIndex[0] == 0x32) {flag_device2 = true;}
        else if (thisByteIndex[0] == 0x33) {flag_device3 = true;}
        else if (thisByteIndex[0] == 0x34) {flag_device4 = true;}
        else {
            index = 0;
            memset(thisByteIndex, 0, sizeof(thisByteIndex));
        }
    }
  
}

void execute ()
{
    //execute command
    if(flag_device1 == true) 
    {
        //access device 1
        Serial1.write(thisByteIndex, sizeof(thisByteIndex));

        thisByte = 0;
        flag_device1 = false;   
    }  
         
}

void storeDisplay()
{
    for (int i=0; i<24; i++)
  {
      while (Serial1.available())
    {
        thisByteReply = Serial1.read();
        thisByteIndexReply[indexReply] = thisByteReply;
        Serial.write(thisByteIndexReply, 24);
    }
  }
}

the atmega128 send this which supposed to be like what i want :

1023
1023
1023
1023
1023



1023 → the 24th data

while the arduino show this on serial monitor :

1023
1023
1023

1023 → the 11th data

and nothing appears after that 11th data

while i try to decrease the amount of the sensor from 24 to 8 and it goes well as arduino show all of the 8 values

so far i still couldnt find the mistake i made in that code
please kindly help figure it out
huge thanks

What is each of Serial, Serial1 and Serial3 connected to?

It is usual for Serial to be connected to the Serial Monitor.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. You can easily make a copy of the appropriate function for each serial port.

...R