Serial relay

Hello, I am trying to get my Arduino to relay the serial data it receives. Here is the sketch that I have:

#include <NewSoftSerial.h>
#define rxPin 2
#define txPin 3

byte incomingByte = 0;

NewSoftSerial nss(rxPin, txPin);

void setup() // run once, when the sketch starts
{
Serial.begin(19200);
nss.begin(19200);
}

void loop() // run over and over again
{

// Read from the hardware UART.
// If any data is available, write it out through the software serial

if (Serial.available() > 0) {
incomingByte = Serial.read();
nss.print(incomingByte);
}
}

I have a simple windows app that is sending the following text strings repeatedly with a carriage return after each string.
1I5ED
2I5ED
3I5ED
4I5ED
5I5ED
6I5ED

Monitoring what the Arduino is sending out, the first 340 times it's fine then it starts to miss characters. See below.
1I5ED
2I5ED
3I5ED
I5ED
5I5ED
6I5E
1I5ED
2I5ED
3IED
4I5ED
5I5ED
I5ED

Is it a buffer problem? Does anyone have any ideas on how best I should do this?

  • what are the devices connected?
  • which is receiving from Arduino?
  • which is sending to Arduino?
  • How fast are the characters generated that are sent to the Arduino ?

First thing I would try is to set the baudrate at 115200 or at least the highest speed the devices can handle, so the serial communication takes minimal time.

Succes,
Rob

Try to look at what he Arduino is recieving to isolate the problem to the HW serial or the soft serial.

Thanks to those that have replied. I have a Windows program that I wrote using AutoIt that outputs the serial stream at 19200 directly to the Arduinos serial port through the USB connection. I then use Hyperterminal to read the output from the Arduino using my laptops serial port. All communications are at 19200 because I am using software to simulate the output of a custom board that uses a propellar chip.

Jason

Serial data transmission follows the USPS model. The USPS does not guarantee to deliver the mail on time. They guarantee to try.

Serial data does get lost/corrupted. Your sender and receiver need to implement some sort of protocol to deal with that fact. Send something like <5:1ISED>, instead of 1ISED. 5 is the number of characters in the packet, < and > are end of packet markers, and : is a separator.

If a < or > is lost, that fact is easy to determine (<5:1ISED<5:2ISED><5:3ISED> is missing a <; <5:1ISED><5:2ISED<5:3ISED> is missing a >). If a : is missing, that is easy to detect.

If the value before the : is missing, that is easy to detect.

If a character after the colon is missing, the number of characters in the payload will not match the number expected, so discard the whole packet.

I then use Hyperterminal to read the output from the Arduino using my laptops serial port

In my experience this is exactly what happens with Hyperterminal, I would try something else.

Having said that receiving and transmitting art the same rate is going to drop characters eventually because the two rates while at the same speed are not synchronised and so will drift with relation to each other. When this happens the buffer slowly fills up and you end up missing stuff. It is always better if the input data rate is slower than the output data rate.

And anyway you could achieve good results if you replaced the arduino with a wire. Or are you planning to do anything else to the data as it goes through?

Try changing

if (Serial.available() > 0) {
incomingByte = Serial.read();
nss.print(incomingByte);
}

To

while( Serial.available() > 0 ) {
incomingByte = Serial.read();
nss.print( incomingByte );
}

So you don't risk overrunning the USART FIFO if the loop turnaround time get too long.
You should always drain the que this way to avoid problems.

HTH // L.