Problems communicating with other device using serial

Hi everyone.

I want to communicate with another device using serial (UART). The device has a baudrate of 57600.

I want to be able to send it data over serial and print what it sends to me. From what I read online, the way to do it is to use SoftwareSerial to communicate with the device and use Serial itself to communicate with the computer, thus allowing me to communicate with the device suing arduino.

When I just read the device's output with Serial (without SoftwareSerial), there're no problems. When I try it with SoftwareSerial it sometimes prints garbage instead of the real output. From what I've read online, the problem is that SoftwareSerial and Serial on high baudrate are really heavy on the arduino and it's probably a performance issue.

So my question is this: Is there a way to communicate with the device using arduino and be able to get good results (i.e. no garbled output)?

P.S. it's an Arduino Uno, if it matters.

tomer32:
Hi everyone.

I want to communicate with another device using serial (UART). The device has a baudrate of 57600.

I want to be able to send it data over serial and print what it sends to me. From what I read online, the way to do it is to use SoftwareSerial to communicate with the device and use Serial itself to communicate with the computer, thus allowing me to communicate with the device suing arduino.

When I just read the device's output with Serial (without SoftwareSerial), there're no problems. When I try it with SoftwareSerial it sometimes prints garbage instead of the real output. From what I've read online, the problem is that SoftwareSerial and Serial on high baudrate are really heavy on the arduino and it's probably a performance issue.

So my question is this: Is there a way to communicate with the device using arduino and be able to get good results (i.e. no garbled output)?

P.S. it's an Arduino Uno, if it matters.

IS there a way to communicate? Who knows, you have given ZERO information to base that decision. The baud rate is never a problem, but how many bytes are sent in a given message at that rate may be a problem or may not.

What is the device and a link to a data sheet, please. Is the communication RS-232 or RS-485, or 20 ma current loop or something else? Is the data ASCII or some other code, or no code at all? Does the device require a command to get it to respond? Does the device require an acknowledgment from the Arduino?

Paul

tomer32:
Hi everyone.

I want to communicate with another device using serial (UART). The device has a baudrate of 57600.

I want to be able to send it data over serial and print what it sends to me. From what I read online, the way to do it is to use SoftwareSerial to communicate with the device and use Serial itself to communicate with the computer, thus allowing me to communicate with the device suing arduino.

When I just read the device's output with Serial (without SoftwareSerial), there're no problems. When I try it with SoftwareSerial it sometimes prints garbage instead of the real output. From what I've read online, the problem is that SoftwareSerial and Serial on high baudrate are really heavy on the arduino and it's probably a performance issue.

So my question is this: Is there a way to communicate with the device using arduino and be able to get good results (i.e. no garbled output)?

P.S. it's an Arduino Uno, if it matters.

Apparently SoftwareSerial has an upper limit baudrate. I run mine at 9600.

If you want higher baudrate, maybe RS232 Shield V2 - DEV-13029 - SparkFun Electronics a RS232 shield will work for you.

Use AltSoftSerial on the two pins it requires (8 & 9 on an UNO).

-dev:
Use AltSoftSerial on the two pins it requires (8 & 9 on an UNO).

How fast can AltSoftSerial work at?

Hmm, I thought that was an easy answer. At the top of the page, it says 31250. Further down, it says 115200 is possible.

The difference is interrupt latency: other interrupts from other sources can prevent AltSoftSerial from reading the captured bit times correctly, corrupting the byte.

Since we can't see the OP's complete sketch, we can't assess whether 57600 is doable. If there are no other interrupt sources, it should work.

I always have to ask,
"why not use Serial?"
"are you sure you need 57600?"
"How about a Leo, Micro or Mega?"

If you are obliged to communicate at 57600 baud using an Uno another option would be to use Serial for communication with the device and use a USB-TTL cable and SoftwareSerial (at 9600 baud or maybe 38400 baud) to communicate with the PC.

You would need to disconnect the device from Pins 0 and 1 when uploading programs.

...R

Robin2:
SoftwareSerial (at 9600 baud or maybe 38400 baud)

Never use SoftwareSerial at 9600 to 38400 baud. It prevents anything else from happening for the entire duration of each character sent or received. That's 1ms of thumb-twiddling for every character at 9600. :stuck_out_tongue:

AltSoftSerial and NeoSWSerial are much better choices.

-dev:
Never use SoftwareSerial at 9600 to 38400 baud. It prevents anything else from happening for the entire duration of each character sent or received.

Would you explain this further?

Where is the documentation on this?

ieee488:
Citation needed.

:slight_smile:

Paul Stoffregen has a nice write-up about AltSoftSerial here, technical description starts in the Latency section.

You can read the SoftwareSerial source code here (RX) and here (TX). That's a for loop in the ISR (other interrupts blocked) and the write method (interrupts disabled before loop).

It's not that it wouldn't work -- it just wastes 95% of the CPU time waiting for each byte (10 bit times, actually). Disabling interrupts for 1ms (10bits/9600bps) is an eternity for a 16MHz Arduino. It can't do anything else but wait for the character to finish. It could have executed 10,000 instructions during that time.

And the lower the baud rate, the more time it wastes. This usually affects other parts of the sketch or other libraries. SoftwareSerial can't even transmit and receive at the same time.

Thanks for that information.

I am going to take a look at the alternatives to SoftwareSerial.

Thanks everyone :slight_smile:

Unfortunately, I cannot provide the datasheet since it's a proprietary device :frowning:
It's a device which allows processing data in a decentralized manner in which slave computation devices are connected to it through ethernet. It and has UART pins for debugging. Currently I have no way of changing the baudrate...

Since my last USB-TTL cable broke and I need to debug this device, I thought of debugging it using an Arduino Uno I have lying around until I get another.

Anyway, it's RS-232, the data is ASCII and it doesn't require an ack form arduino.

tomer32:
Unfortunately, I cannot provide the datasheet since it's a proprietary device :frowning:

That sort of thing come up here from time to time and it makes me think that someone is trying to get free advice for a commercial project. That''s not fair IMHO. Either pay for your advice or share your information.

...R

the data is ASCII and it doesn't require an ack form arduino.

Just use a simple echo program:

#include <AltSoftSerial.h>
AltSoftSerial secretDevice;

void setup()
{
  Serial.begin( 57600 );
  secretDevice.begin( 57600 );
}

void loop()
{
  if (Serial.available())
    secretDevice.write( Serial.read() );
  if (secretDevice.available())
    Serial.write();
}

It would probably work. NeoSWSerial and SoftwareSerial would not work.