Serial1 to PC on Leonardo

I am getting random(ish) results when writing to Serial1... the board is a Leonardo to a standard 340 usb converter this is connected to pins 0 & 1. I know I am writing ASCII data... just using Hex to understand it!

The code;

int a=0;
void setup()  {   Serial1.begin(9600); }
void loop()  {
  while (a<10)   {
    Serial1.print(a);
    Serial.print(a);
    Serial1.print(",");
    Serial.print(",");
    a++;   
    }
  Serial.println();
  Serial1.println();
  delay(1000);
  a=0; 
}

Serial via USB is automatically initialized on boot, only serial1 needs to be initialized. serial via usb is fine anyway.

So one might expect the same results from both channels... so I thought...

Using 'simple term gold'
output from USB (serial): 0,1,2,3,4,5,6,7,8,9,<0D><0A> that's all good
for reference the hex is: 30 2C 31 2C 32 2C 33 2C 34 2C 35 2C 36 2C 37 2C 38 2C 39 2C 0D 0A

then from 'serial1' - the 'hardware serial port

76 EB A7 9B A7 99 A7 97 A7 95 A7 93 A7 91 A7 8F A7 8D A7 E5 EB 00

my first instinct was try different baud rates... no, both term and arduino are set to 9600.

then I remembered about bit inversion in RS232, so I tried that and the results are:
89 14 58 64 58 66 58 68 58 6A 58 6C 58 6E 58 70 58 72 58 1A 14 FF

Since both are from the same terminal program, I am thinking about the arduino, the rs232 -> usb converter is working it seems.

Have I missed something?

I did search, here and google, but nothing came near my issue...

BTW it is a genuine board, not a random clone!

Thanks in advance

Can you put your sketch between lines with three backslash-single-quotes ?

```
Your sketch
```

In your sketch, you use 'Serial' as well, but there is no Serial.begin(9600);.
Do you know what will happen ? Will the Leonardo stop or start smoking ?
Why not try something easy ?

void setup()
{
  Serial1.begin( 9600);
}

void loop()
{
  Serial1.print( "A");
  delay (100);
}

If you can see somehow that "A", then you can try: Serial1.println( "Hello");

Can you give a link to your usb-serial module ?

You have tested inverse signal and baudrate, but have you tested exchanging RX and TX ?

1 Like

Hi Koepel,

Thanks for your reply, did the sketch as suggested, and the results (in hex) are:
5F 5F 5F 5F 5F 5F 5F 5F 5F 5F 5F 5F 5F 5F 5F 5F

yes I did try swapping TX and RX, which as I expected made no difference.

link to the converters... hmmm not likely they are many many years old, I have worked on serial equipment to PC's for a very long time, and these were aquired somewhere along the way! I do know they are CH340 based. what I do know is they work in other applications, so I see no reason to doubt them. recently programmed a lithium battery BMS and solar controller with the same cable!

It really is a mystery how the chars are bing sent... there is a pattern, but I have not yet decoded it!

Ian

There is a pattern for sure:
send_____: 2C 30 31 32 33 34 35 36 37 38 39
received_: A7 EB 9B 99 97 95 93 91 8F 8D E5
I have been looking for a while at these numbers, but I don't see it.

Did you connect the grounds ?

What if you do: Serial1.print( "AA");
If that results in a consistent pattern, the perhaps the baudrate can be calculated with 9 bits low: Serial1.write( 0x00);
What if there is only one start-bit and everything else is high: Serial1.write( 0xFF);

A Serial.print() will not print a zero byte, but the Serial.write() can do that.

The next sketch has not been tested. Connect the Arduino Serial Monitor via the normal serial port, and Serial1 to the CH340G module to a terminal on the computer.
Sketch not tested:

unsigned long baudTable[] = {75, 100, 150, 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, 115200};

#define DEBUG_PORT SerialUSB
#define CH340_PORT Serial1

void setup() 
{
  DEBUG_PORT.begin( 9600);
  while( !DEBUG_PORT);
  DEBUG_PORT.println( "The sketch has started");
}

void loop() 
{
  for( auto a:baudTable)
  {
    DEBUG_PORT.print( "Testing ");
    DEBUG_PORT.print( a);
    DEBUG_PORT.println( " baud");

    CH340_PORT.begin( a);
    CH340_PORT.println();
    CH340_PORT.println( "-- Hello --");
    CH340_PORT.println();
    CH340_PORT.end();
    delay( 200);
  }
  delay( 2000);
} 

I hope these tests give more information, but something does not add up. The most logical explanation is that the CH340G is broken or has wrong firmware. Can you try a module that you know is working ?

Hi Koepel,

As you suspected the USB <-> serial was at fault, got an A2Z delivery module, and it works perfectly.

Back to writing the code for my project now!

Thanks for your help

Ian

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.