Wrong output on serial

Hi,

I'm having some trouble with sending data over a serial port.

I use:

#include <SoftwareSerial.h>
#define rxPin 10
#define txPin 11
SoftwareSerial mySerial(rxPin, txPin); // RX, TX

void setup()
{

// define pin modes for tx, rx:
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
mySerial.begin(115200);

}

void loop()
{
uint8_t buff_serial[3];
size_t len_serial;

buff_serial[0] = 0xDE;
buff_serial[1] = 0x44;
buff_serial[2] = 0x05;
len_serial = 3;

mySerial.write(buff_serial, len_serial);

}

I'm testing this using a serial monitor program on my PC, set up to 115200,8,1.

Bu the monitor shows

EF D7 7D

instead of

DE 44 05

If I set:

buff_serial[2] = 0x06;

I get:

EF D7 BF

So, 5 translates to 125 (1111101) and 6 to 191 (10111111).

What I am doing wrong?

Lower the Baud rate to something more reasonable. Software serial receive does not work at all at 115200 Baud, and at that rate, you are taking your chances with transmit.

Thanks, but at 56000, I get exactly the same results.

At 38400, I read one character instead of 3, and below (9600 for example), it does not work at all!

Besides, I need to communicate with a device which speed is 115200 and cannot be changed.

How do you connect the Arduino to the PC? RS232 level converter? FTDI cable?

How are you viewing the serial output ?

I connect using a simple serial cable, using a 9 pins RS232 connector.

On this connector, I connected pin 5 to ground, pin 2 (RX) to Arduino pin 11 (txPin) and pin 3 (TX) to Arduino pin 10 (rxPin).

I'm using Realterm (Terminal Software)

Results change depending on the port I use.

If I send

for(int i=0; i<255; i++)
mySerial.write(i);

on my PC native port COM1, I get:

EFEFF7DFD7EFF7BFB7AFB7DFD7EFF77F776F775F576F77BFB7AFB73FAFDFF7FFF7EFF7DFD7EFF7BF
BBEDB56BDB6DB76F77F7BB77DD75BB77EFEFBD7BDF7DBF7FDDBDDD7D5D3DDDFDDDBDDD7D5D7DBDFD
75F575F5D555AB57EBEBAD5BD75DAF5FBB7BBBFBDB6DB76FEDEDB56BDB6DB76F77F777F7DD75BB77
EFEFBD7BDF7DBF7FFFDFEFBFAFDFEF7F6F5FDFBFAFDFEFFFEFDFEFBFBFFBDBB7F7F7DDBBEFBDDFBF
BD7DBDFDBD7DBDFDF5ABD5ABEBADD7AF7BFB7BFBEDB5DBB7F7F7DDBBEFBDDFBFDFBFDFFFBFBF9FFF
DFBFDF7F5FBFDFFF7DFD7DFDF5D5EBD7FBFBEDDBF7DDEFDFBF7FBFFFBFFBF7EFFDFDF5EBFBEDF7EF
7FFF7FFFFDF5FBF7FFFBFDFBFFFDFF

On an USB to serial prolific COM3 device, I get:

0000E8ECE4DCD4CCC4BCB458A49C948C847C746C64B8544C443C342C2438281804FCF4ECE4DCD4CC
C478A38D356A1A69346846063366196532648C8C316218613060D1B19171516111F1D1B191715161
21E145C545C515552A548A0A295214512850A2622282134D264C8989254A1249244844C444C41145
22448808214210412040D0D8C8B8A898887868589038281808F8E8D8C8B8A0831A34868619320C31
1830A16121E1A16121E1052B152A0A29142842C282C2092512248484112208211020D0B090E0A030
20F0D0B09070503010F081C141C105150A148282091204110810A06020E04003060C8181050A0209
040840C040C00105020400030102000100

Sendind back with Realterm bytes to the Arduino works with the Prolific, but not with the native COM1 port, and results are also incorrect.

The prolific does work at 9600, but still with garbled results.

I forgot, my sketch does other things in fact, it deals wit IR signals and uses an Adafruit 7-segment backpack with:

IRdecode results;
IRrecv Receiver(RECV_PIN); //Create the receiver. Use pin RECV_PIN
IRsend Sender;

#include <Wire.h>
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"
Adafruit_7segment matrix = Adafruit_7segment();

Maybe this interferes with the serial processing somehow?

I'll try a basic serial sketch without anything else, and post if it works better.

I connect using a simple serial cable, using a 9 pins RS232 connector.

The Arduino uses TTL levels on its pins and these are incompatible with an RS232 connection. You need something, such as an FTDI cable to connect the PC serial port to the Arduino.

You can't directly connect the Arduino's pin to a RS232 port on a PC. Arduino levels are 0V and 5V, matching RS232 levels are 12V and -12V.

If you indeed connected it directly without a level converter, you might very well be in the process of blowing up your Arduino; the Arduino does not appreciate -12V on any of its inputs.

As from the first paragraph, the signals are inverted. Hence the PC does not sync correctly on the startbit and you get garbage. It will see some databit as a startbit and counts bits from there.

I sincerely hope you do have a level converter in that cable.

sterretje:
You can't directly connect the Arduino's pin to a RS232 port on a PC. Arduino levels are 0V and 5V, matching RS232 levels are 12V and -12V.

If you indeed connected it directly without a level converter, you might very well be in the process of blowing up your Arduino; the Arduino does not appreciate -12V on any of its inputs.

Aaah!

I did not know that, it's my first time using an Arduino.

I will have to get an FTDI cable (and maybe another Arduino if mine is blown...), thanks.

I'll post back here once I get it.

I took a peek, and only found USB to serial FTDI cables.

What I need is something to connect the Arduino RX/TX pins to a device that does not have USB, just an RS323 port.

Is there such an adapter to convert 0/+5 to -12/+12 directly?

If not, I'll have to make one myself.

This should do the trick:

Thanks to all that helped!

Bob8K:
This should do the trick:

http://www.amazon.fr/Convertisseur-RS232 vers-Ultra-Compact-DB9 femelle/dp/B00OPTOKI0/ref=sr_1_90?ie=UTF8&qid=1455010655&sr=8-90&keywords=ftdi

Yep, looks like it.