using mega 2560 as USB to UART

Hi,

I have been trying to use the mega2560 as USB to SERIAL

but I have a problem while the RX is empty I get garbage in ?

the code is very simple:

void setup() {

Serial.begin(9600);

Serial1.begin(9600);

Serial.println(“By Roee Serial read write to port1 data on port 0”);

}

void loop() {

/*

serial read from port 0 write to port 1

and then read from port 1 and writes back to port 0

*/

if (Serial.available() >0)

{

my_delay();

Serial1.write(Serial.read());

}

if (Serial1.available()>0)

{

Serial.print(char(Serial1.read()));

my_delay();

}

}

void my_delay()

{

delay(1);

}

Now when I short TX1 to RX1 I should get back what I am sending - this is working ok

But when disconnect jumper I get square sign all the time?

Does any one have any idea why?

Thanks,

Roee

roeebloch:
Now when I short TX1 to RX1 I should get back what I am sending - this is working ok

But when disconnect jumper I get square sign all the time?

With the short you are completing the loop. Characters coming into serial0 (from the USB/PC) go out on serial1 and come back in on serial1 to go out on serial0 to the USB/PC. Open the loop and you've got noise coming in on serial1 (Rx) and being sent to the USB/PC. There the data received is interpreted into some odd ASCII character, perhaps a '254' in your case.

http://www.asciitable.com/

If you wanted to go beyond the “Arduino language” you could check for UART errors and I’m sure you’d see them on serial1 w/o the short. If you want, try this … connect the serial1 Rx pin to Vcc. Now you won’t have noise fooling the UART into thinking data is incoming. Perhaps you could just enable the internal pull-up for that pin instead.

Hi,

Nop - I have tried pull up 10K before I sent this question.

And BTW I have tried pull up and this did not work, so later I have tried pull down with same results

I put it on RX1 only (because I don’t see why TX1 will do such a problem)

Bottom line did not work - you can try the code and the short yourself - this is very easy experiment to do

Thanks,

Roee :geek:

roeebloch:
Bottom line did not work - you can try the code and the short yourself - this is very easy experiment to do

Hmmmm ... it would be an easy experiment ... if I had a 2560. :mrgreen:

With the RX1 input tied HIGH, I can’t see how the UART1 is “detecting” a start bit. That it all works with TX1 tied to RX1 would seem to indicate that whatever is coupling into the RX1 input is coupled loosely but then tying it HIGH or LOW should also work (no data). So perhaps the problem isn’t there but in the link btw the 2560 and the serial/USB translator ??

I might have put the delays slightly differently but since the code works when wrapped around, that can’t be an issue.

void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);
  Serial.println("By Roee Serial read write to port1 data on port 0");
}

void loop() {
  /*
  serial read from port 0 write to port 1
  and then read from port 1 and writes back to port 0
   */

  if (Serial.available() >0)
  {
    my_delay();
    Serial1.write(Serial.read());
  }
  if (Serial1.available()>0)
  {
    Serial.print(char(Serial1.read()));
    my_delay();
  }
}

void my_delay()
{
  delay(1);
}