1

I am trying to do a loopback test using an Arduino Mega. What I am looking to do is send a byte over TX and receive said byte back at RX and verify that the byte is unchanged. More or less I just have a jumper wire connecting the TX and RX ports (In reality I have a circuit hooked up to the TX RX ports that effectively acts like a wire, I'm just trying to verify that the circuit traces are sound).

I am also doing the exact same thing over TX2/RX2 and TX3/RX3.

The issue I have run into is that I can't seem to get the data to send out the TX ports. In my attempt I used Serial objects where I would do the following:

void setup() {
Serial.begin(9600); //0 and 1
Serial2.begin(9600); //16 and 17
Serial3.begin(9600); //14 and 15
}

void loop() {

//RX TX
int bytesSent = Serial.write(1);
delay(500);
if(Serial.read()!=bytesSent){

  //Do Something

}

delay(500);

//RX2 TX2
bytesSent = Serial2.write(2);
delay(500);
if(Serial2.read()!=bytesSent){

   //Do Something

}

delay(500);

//RX3 TX3
bytesSent = Serial3.write(3);
delay(500);
if(Serial3.read()!=bytesSent){

   //Do Something

}

delay(500);

}

I'm aware that the use of "delay(500)" is not optimal nor good coding practice but is left here for now while I'm still trying to understand what's wrong with my transmit and receive.

I believe my misunderstanding comes in with the Serial objects; Am I not using them correctly in this case? I've referenced the Arduino Serial reference guide, so this seems like it would work, but does not.

Further details: I am currently powering my Arduino via USB (which I'm aware can be an issue, but I currently do not have the battery power source). Would switching to the other power source resolve my issue? Just to reiterate I'm not trying to communicate with the PC, I'm just trying to verify that this external circuit (which acts like a wire) is able to carry the signal.

2
  • you have a logic error ... your code assumes that bytesSent contains the data that was sent
    – jsotola
    Commented Apr 26, 2023 at 17:20
  • 2
    How exactly do you know, that it doesn't work? In the code above you aren't doing anything. You only have the comments in there.
    – chrisl
    Commented Apr 26, 2023 at 18:19

1 Answer 1

1

There seems to be a slight misunderstanding here:

bytesSent = Serial2.write(2);
// [...]
if (Serial2.read() != bytesSent) // [...]

On the serial ports:

  • the method write() returns the number of bytes that were written, not the value of a written byte

  • the method read() returns the byte that was read (or −1 if nothing was available), not the number of bytes read.

I suggest you modify your test like this:

uint8_t byte_sent = random(256);
Serial2.write(byte_sent);
delay(100);
if (Serial2.read() != byte_sent) // there was an error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.