Leonardo Pin 0 & 1 Serial1 com weird problem cutting of first chars every string

Hi everyone. First post on the forums and yes I have searched extensively on this problem via google and on this site. Nothing I have tried from what I have read has solved the problem.

What I am trying to do:
I am building a "Light Tree" for my sons cub scout pack pinewood derby race track. It has a microwizard "Finish Line" timer that communicates via a 9 pin serial cable to a computer program that records race times.

The leonardo is going to receive a signal on an alternate, non serial tx/rx pin, set to high to notify "Start Race" and when that signal is received the program will trigger a series of LED's to flash like a racing light sequence in front of each lane and then activate a solenoid via relay to open the gate and start the race. I have all that code worked out down to using an amp circuit to drive the LED's because their are 8 per output pin. My original plan was once the race started I was going to wait for the "End of Race" high signal to flash the green start LED's when the race ended.

Where things are going wrong is that I had to go and think "What if I could tell who won the race and blink just their green leds at the end? There is an end of race signal on another pin and then a serial string is sent to the computer with race data including a special character for the winner.

How I thought I could make this happen:
My plan was simple. Use pins 0 and 1 on the leonardo to receive the timer serial TX coms on the RX pin on the arduino, read the stream and replay it back out the TX pin and back out to the computer. Sort of a man in the middle. Once I had successfully set up my man in the middle position I was going to then build a string of the input chars and search for the special character to know who won and blink the appropriate LED group.

Where things are going wrong:
I have captured a sample end of race string sent to the computer without the arduino in the middle via a terminal emulator and it looks like this:

A=0.992! B=1.164 C=1.237 D=1.383 E=0.000  F=0.000

When I put the arduino in the middle this is what I get:

  PO2J92! B=1.164 C=1.237 D=1.383 E=0.000  F=0.000

If I do it 5 times in a row it is inconsistent. At least one of those times has more going on at the start of the string.

I can take out the timer all together and just hook the arduino directly to the computers com port, open a terminal and send that string and what I get back is exactly as above. I can change the string to say: This is a test of the serial coms and it changes the first 4 characters every time. If I use a serial terminal that I can just "free type" in the window not one single character comes back as what I type. Q goes out and comes back as G etc...

I have two leonardos. One that is 2 years old and one I received 2 days ago that I was going to give to the scout pack once programmed. They both behave exactly the same!

Serial Coms Test Sketch and what I have tried:

int led = 13;
byte byteRead;

void setup() {
  // initialize both serial ports:
  Serial1.begin(9600);
  if (!Serial1) {}
  pinMode(led,OUTPUT);
}

void loop() {

  while (Serial1.available() > 0) {
    byteRead = Serial1.read();
    Serial1.write(byteRead);
    digitalWrite(led, HIGH);
    digitalWrite(led, LOW);
  }
}

The sketch above is pretty much exactly what I am testing. I had to remove some added code this morning for other tests to make this post and could not try it on the arduino :frowning:

I have tried using the softserial class and it behaves exactly the same way. I have tried different data types, different sketches that build the whole string before re transmitting it but in the end, the above should do the job right? I have tried different types of wires, twisted, untwisted pair etc... It always does the same thing.

There just seems to be some inherent action at the start of a string with the arduino that causes this behavior. I have tried sending/receiving the string via the serial monitor com port via USB and it comes back just fine.

The connections are:
Arduino powered via USB

Pin 0 (RX) to TX on the computers serial port
Pin 1 (TX) to RX on the computers serial port
Arduino ground to pin 5 on the serial port (also ground)

If anyone can shed any light on this I would truly appreciate it. Thanks!

Welcome to the forum.

You have to narrow it down somehow, because I have no idea what is going on.
Can you make a test sketch ?
The sketch that you uploaded does not show the led turning on, it is too short.
If you add delays, the serial communication could stop working.

I don't understand how you have connected it.
RS-232 has voltage levels of -3/-15 to +3/+15V, you can not connect that to the Arduino.

I don't understand this: if (!Serial1) {}
If you want to use the serial monitor, you have to use 'Serial', and not 'Serial1'. And I'm used to see there an empty statement with ';'.

Please start again, make a test sketch, just for the serial problem. Upload the test sketch, and tell us how you have connected everything.

That is the test sketch that only performs serial relaying. The built in LED does "flicker" quickly when data flows. I only added early on to confirm data was received because I was transferring one byte at a time, and not sending a whole string. If I remove the LED flicker commands nothing changes.

Serial1 on a leonardo IS the hardware serial port. If i use Serial that is the usb com port.

This line: if (!Serial1) {} is recommended over and over for the leonardo to wait for the serial port to become ready because it is a software serial port, not a true hardware port.

So again,

I am connecting TX/RX of a computer serial port directly to RX Pin 0 and TX Pin 1 of the arduino and ground (serial pin 5) to the arduino ground. I was unaware I "couldn't" do it so I just did it and the above is what happens. I assumed serial communication was serial communication?!

No, serial communication is a big mess :frowning:

The RS-232 is the com port that was on computers 10 years ago, and also used by (industrial) machines.
It uses voltage levels of -15/-3 to +3/+15V.

Those voltages could have damaged your Arduino board. You better continue testing with a new board.

The 'serial communication' between microcontrollers is sometimes called serial UART ttl-level communication, and is with 0V and 5V (or 3.3V).
Somtimes it is inverted, sometimes not.

You test sketch is not okay.
The special startup for the serial communication is for the 'Serial' to the computer serial monitor only. It is to start the virtual serial port via the usb bus, which uses software emulation. The hardware 'Serial1' is a normal serial port and doesn't need that.
And I would expect a ';' as I wrote before.

Read your previous post again. I'm not aware that I wrongly mixed 'Serial' with 'Serial1', but you did :wink:

Please fix your sketch and upload it.

I actually started this project without the if (!Serial1) line and added it later while trying to track this down when I saw it in several projects stating that the Leonardo does not have any kind of dedicated serial chip but only uses virtual serial.

It runs either with or without it the same way as well as trying about 20 other peoples sketches for reading in serial data and sending it back out.

The connection to a computer is via a new USB to serial device (http://www.amazon.com/gp/product/B00425S1H8/ref=oh_details_o02_s00_i00?ie=UTF8&psc=1), a double ended female adapter (gender changer) with some solid copper phone wire inserted in to the female plug and the leonardo headers.

I have considered noise, baud, stop bits, parity and the list goes on and on. I have banged my head on the wall for 4 days searching and trying other peoples work to no avail.

I have learned much about the way serial works. Transferring ones and zeros as anything over +3 V or under -3V. As this is a USB to serial adapter it runs at 5V so I think the voltage is ok.

One other thing worth mentioning.... You see the return result in my first post? The first few characters are messed up.  PO2J92! B=1.164 C=1.237 D=1.383 E=0.000  F=0.000 Most of the data comes through, just not the first byte/bits.

If I open the USB serial port and send the same exact characters with Serial.write to it, then monitor it with the IDE serial monitor I get nothing even close to what the first post shows as result. Its just complete gibberish and odd characters.

I have even pondered hooking up an external power source instead of the USB one to see if maybe the two connections to the computer are causing issue :frowning:

Posting on here was a last ditch effort to understand what I am doing wrong otherwise I will go with my original plan instead of trying to figure anything out from serial port data.

I actually started this project without the if (!Serial1) line and added it later while trying to track this down when I saw it in several projects stating that the Leonardo does not have any kind of dedicated serial chip but only uses virtual serial.

You mix things up. The virtual serial device of the Leonardo is Serial not Serial1, Serial1 is a standard UART with TTL level (0V low, 5V high). It doesn't have a dedicated serial chip because the UART is integrated into the main processor as with any Arduino.

I have learned much about the way serial works. Transferring ones and zeros as anything over +3 V or under -3V. As this is a USB to serial adapter it runs at 5V so I think the voltage is ok.

Another thing you mix up: +3V and -3V are the lower limits for RS232, don't connect a RS232 device directly to a Leonardo, you may even damage it.

Start by posting a link to the device you want to connect to the Arduino and draw the schematics how you wired your setup.

Ok, just looked at this. More trouble than it is worth. I thought I could get away with a quick connection but I see now that is not possible.

Still no link to that mystery device. Maybe we find another solution?

Its not a mystery device. I have said multiple times it is a computer serial port from USB to RS232 serial. Once I looked that up and saw the circuit above......

The device is a microwizard fast track finish line timer controller: Micro Wizard Offical Web Site

With such USB-to-serial converters, about everything is a mystery.
They say it contains a PL2303HX chip, but that chip converts the USB to a ttl-level signal. So there must be something in that converter to work with RS-232 voltage levels.
I'm serious: serial communication is a big mess :frowning:

Can you make the led display work with that usb-to-serial converter and control it directly from the computer ?
Most of those displays have a program on the computer to make texts and so on.
Or perhaps you can test it with a serial monitor. You can use the one that is part of the Arduino IDE, or use Hyperterm, or program like QST: http://qst.sourceforge.net/

My goal is that you end up with a good sketch, and you still have a bad sketch.
If a good sketch is not working, the Arduino board might be damaged.
So far this thread contains only half-baked solutions and is going nowhere.

I don't know if there is a language barrier here or what but I already said that I could run the lights just fine for the race track. I just wanted to read a string from the serial port to see which racer won the race and do something different.f

Post a picture or schematics of your wiring. I still don't understand why you don't just use the USB serial connection the Leonardo offers. Why are you using a USB-to-serial converter connected to the hardware serial port if you have the same functionality built into the Leonardo? That's one of the advantages of the Leonardo.

Are you sure that the driver for your USB-to-serial converter in the PC is working correctly? Many are a piece of crap as I had to experience more than once.

Thanks for the offer to help but I am abandoning this line of thinking as connecting RS232 communications is more trouble than it is worth.

I have found out how to make it happen with a chip but I just don't have the time right now to mess with it for the little return it was goin to yield. What I have is good enough.