Serial communication

Hi, I tried communication between two uC. Steps are
1) uC1 sends a request (1 byte) to uC2.
2) On receiving this byte, uC2 sends three bytes of data to uC1.
3) uC1 waits for completion of the data reception and stores the data in an array.

All the three steps works ( I am able to display the received bytes correctly using LEDs)
But the problem is I need to add about 1sec delay before every time request is sent from uC1.

(I set a flag to low before sending the request, this flag (global bit) is set to one by the UART ISR when 3 bytes are received. After sending the request, the main program waits for this bit to become high, and then proceeds.Without the additional delay, this bit does not seem to change.)

Is there anything wrong in my method? Why this delay becomes necessary?

Parents
  • I don't know why you would need a 1-second delay.

    But you do need a delay since it takes more time to send 3 characters than it takes to send 1 character. So the processor sending 1 character have to spend some time waiting until 3 characters have been received before starting a new request - or the other processor would not be able to pick up all characters and tripple the number of characters to return.

    Another thing - your code should contain a repair timer, i.e. if you haven't received all three characters within a reasonable time, then something must have failed so it isn't meaningful to wait anymore. So throw away any partial result you may have received (zero, one or two characters) and send out your one-byte command again hoping for better luck this time.

    A third thing - when having variables processed by both interrupt handlers and main loop: Do you remember to set them as volatile to force the compiler to always generate a physical read? Without the volatile keyword, the compiler may decide that it already knows the value of the variable.

Reply
  • I don't know why you would need a 1-second delay.

    But you do need a delay since it takes more time to send 3 characters than it takes to send 1 character. So the processor sending 1 character have to spend some time waiting until 3 characters have been received before starting a new request - or the other processor would not be able to pick up all characters and tripple the number of characters to return.

    Another thing - your code should contain a repair timer, i.e. if you haven't received all three characters within a reasonable time, then something must have failed so it isn't meaningful to wait anymore. So throw away any partial result you may have received (zero, one or two characters) and send out your one-byte command again hoping for better luck this time.

    A third thing - when having variables processed by both interrupt handlers and main loop: Do you remember to set them as volatile to force the compiler to always generate a physical read? Without the volatile keyword, the compiler may decide that it already knows the value of the variable.

Children
More questions in this forum