A question about the using of "serial"

I use Serial1.write() write some bytes, and then I use Serial1.read to read the reply data, but the it is the some as the bytes that I have written, Why??

Thank you very much!

My board is Arduino Mega, and the code like this:


byte Instrc0[] = {0xFF, 0xFF, 0x00, 0x04, 0x02, 0x24, 0x02, 0x00}; byte Instrc[] = {0xFF, 0xFF, 0x01, 0x07, 0x03, 0x1E, 0x68, 0x00, 0x00, 0x03, 0x00};

int val[20]; int i;

void setup()

{ Serial.begin(9600); Serial1.begin(500000); }

void loop() { i = 0;

Serial1.write(Instrc,11);

Serial1.write(Instrc0,8); delay(20);

if(Serial1.available() > 0) { val = Serial1.read(); _ Serial.println(val*);_ _ i++;_ _ }_ _*delay(50);_ * delay(2000);* } :)

When posting code, please place it in code tags (press the "#" button in the editor.)

Serial1.begin(500000);

Why such a strange baud rate?

I'm really not following what you are doing.

I use Serial1.write() write some bytes, and then I use Serial1.read to read the reply data, but the it is the some as the bytes that I have written, Why??

Can you try to explain, again, what you are doing? This doesn't make any sense.

How is the hardware connected?

First, thank you for your patients.

I use the Arduino Mega board to communicate with another Arduino board, the baud rate need 500,000bps.

In the code I first send the Instruction (some bytes):0xFF, 0xFF, 0x00, 0x04, 0x02, 0x24, 0x02, 0x00 and 0xFF, 0xFF, 0x01, 0x07, 0x03, 0x1E, 0x68, 0x00, 0x00, 0x03, 0x00 to the other board, then I try to read the reply data of the other board.

But the question is the data I see in the Arduino serial monitor is :0xFF, 0xFF, 0x00, 0x04, 0x02, 0x24, 0x02, 0x00 and 0xFF, 0xFF, 0x01, 0x07, 0x03, 0x1E, 0x68, 0x00, 0x00, 0x03, 0x00, just the same as I have written. Why ?

Thanks.

The code I use is:

byte Instrc0[] = {0xFF, 0xFF, 0x00, 0x04, 0x02, 0x24, 0x02, 0x00};  
byte Instrc[] = {0xFF, 0xFF, 0x01, 0x07, 0x03, 0x1E, 0x68, 0x00, 0x00, 0x03, 0x00};

int val[20];
int i;

void setup()

{
  Serial.begin(9600);      
  Serial1.begin(500000);    
}

void loop()
{
  i = 0;

  Serial1.write(Instrc,11);

  Serial1.write(Instrc0,8);
  delay(20);

 if(Serial1.available() > 0)
 {
   val = Serial1.read();
  Serial.println(val);
  i++;
  }
 delay(50);
    delay(2000);
}

You write out 19 bytes. You wait for a response and read one byte back. You then wait 2.050 seconds and do the same thing. You add one to i without using it anywhere. Why are you doing all this?

You haven't shown the code for the other board.

The code in the other board cannot be seen, but the protocol that they communicate is known to me, which is send a reply after it receive the instruction from the Arduino Mega.

elegance: The code in the other board cannot be seen ...

Oh?

Yes ? in fact, the "other board" is a servo motor, and it use the AVR ATmega as the microcontroller, and I use the Arduino Mega to communicate with it.

Serial1.write(Instrc,11);

The Print::write() function for outputting an array of bytes does not work the way I would have expected. It increments a pointer into an an array, and passes that pointer to a function that expects a NULL terminated array of chars.

You'd be a lot better off using Serial1.write() in a for loop, to write out the array one value at a time.

Where did you see that behaviour? Try this:

void setup()
{
  byte msg [] = "But does it get goat's blood out?";
  
  Serial.begin(115200);  
  Serial.write (msg, sizeof msg);  
}

void loop() {}

That outputs the whole message, as expected. Here's the implementation:

/* default implementation: may be overridden */
void Print::write(const uint8_t *buffer, size_t size)
{
  while (size--)
    write(*buffer++);
}

elegance: The code in the other board cannot be seen, but the protocol that they communicate is known to me, which is send a reply after it receive the instruction from the Arduino Mega.

Can you show your wiring?

You haven't responded to this:

[quote author=Nick Gammon link=topic=79830.msg603228#msg603228 date=1321942398] You write out 19 bytes. You wait for a response and read one byte back. You then wait 2.050 seconds and do the same thing. You add one to i without using it anywhere. Why are you doing all this? [/quote]

Sorry for my carelessness!

I use "i" to store the received byte into a array.

I write the code in the forum wrongly,The code I write in my Arduino board is:

byte Instrc0[] = {0xFF, 0xFF, 0x00, 0x04, 0x02, 0x24, 0x02, 0x00};  
byte Instrc[] = {0xFF, 0xFF, 0x01, 0x07, 0x03, 0x1E, 0x68, 0x00, 0x00, 0x03, 0x00};

int val[20];
int i;

void setup()

{
  Serial.begin(9600);      
  Serial1.begin(500000);    
}

void loop()
{
  i = 0;

  Serial1.write(Instrc,11);

  Serial1.write(Instrc0,8);
  delay(20);

 if(Serial1.available() > 0)
 {
   val[i] = Serial1.read();
  Serial.println(val[i]);
  i++;
  }
 delay(50);
    delay(2000);
}

And the same question still exist, why?

Thank you very much.

This question also still exists:

[quote author=Nick Gammon link=topic=79830.msg603677#msg603677 date=1321991540] Can you show your wiring? [/quote]

And this:

[quote author=Nick Gammon link=topic=79830.msg603228#msg603228 date=1321942398] You write out 19 bytes. You wait for a response and read one byte back. You then wait 2.050 seconds and do the same thing. You add one to i without using it anywhere. Why are you doing all this? [/quote]

Why write 19 bytes and then receive 1. What is your purpose in so doing?