Pages: [1]   Go Down
Author Topic: Serial relay  (Read 1356 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


- 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
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Copenhagen / Denmark
Offline Offline
Edison Member
*
Karma: 6
Posts: 2360
Do it !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 602
Posts: 33379
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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?
Logged

Tellus
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: