Serial communicating trash

I’m getting some weird gibberish into my serial monitor. This is happening with three different sketches.

Note that I’m actually connecting to a RS232 device and giving it the command to show me the time. Any idea what is happening and how do I fix this?

This kind of relates to post: http://forum.arduino.cc/index.php?topic=443366.0

I’ve based the third sketch of of Robin2’s serial tutorial…so yeah, I’ve looked through it.
I still have to test Nick Gammons tutorial / code though, but I suspect that the outcome will probably be the same.

Sketch one:

#include <SoftwareSerial.h>
SoftwareSerial portOne(2, 3); // RX, TX

//zoomkat 3-5-12 simple delimited 'X' string
//from serial port input (via serial monitor)

//and print result out serial port

String readString;
String toMachine;
String fromMachine;

void setup() {
  Serial.begin(9600);
  Serial.println("serial delimit test 1.0"); // so I can keep track of what is loaded
  portOne.begin(57600);
  portOne.print("show time");
  portOne.print('\r');
}

void loop() {

  if (portOne.available())  {
    char c = portOne.read();  //gets one byte from portOne
    if (c == '>') {
      //do stuff
      Serial.print("Got this: ");
      Serial.print(toMachine); Serial.println(">");
      toMachine=""; //clears variable for new input
    }  
    else {     
      toMachine += c; //makes the string readString
    }
  }

}

---------------------Gives me this---------------------------------



serial delimit test 1.0
Got this: .Ë÷2÷¶vCá161227 212844 (GMT+0h)
Ready>

Sketch two:

#include <SoftwareSerial.h>
SoftwareSerial portOne(2, 3); // RX, TX
String toMachine;
String fromMachine;
void setup()  
{
  // Open serial communications
  Serial.begin(9600);
  portOne.begin(57600);
}



void loop() // run over and over
{

  while (Serial.available()==0);
  toMachine = Serial.readString(); 
   
  Serial.print("Me:");
  //portOne.print('\r');
  Serial.println(toMachine);
  portOne.print(toMachine);
  portOne.print('\r');
  while (portOne.available()==0) {   
  }
  fromMachine=portOne.readString();
  Serial.println("Machine:");
  Serial.println(fromMachine);
  
}

---------------------Gives me this---------------------------------

Me:show time
Machine:
.Û÷2÷[vCá161227 212934 (GMT+0h)
Ready>

Sketch three:

// Example 2 - Receive with an end-marker

#include <SoftwareSerial.h>
SoftwareSerial portOne(2, 3); // RX, TX
const byte numChars = 255;
char receivedChars[numChars];   // an array to store the received data

boolean newData = false;

void setup() {
    Serial.begin(9600);
    portOne.begin(57600);
    Serial.println("<Arduino is ready>");
    portOne.print("show time");
    portOne.print('\r');
}

void loop() {
    recvWithEndMarker();
    showNewData();
}

void recvWithEndMarker() {
    static byte ndx = 0;
    char endMarker = '>';
    char rc;
    
    while (portOne.available() > 0 && newData == false) {
        rc = portOne.read();

        if (rc != endMarker) {
            receivedChars[ndx] = rc;
            ndx++;
            if (ndx >= numChars) {
                ndx = numChars - 1;
            }
        }
        else {
            receivedChars[ndx] = '\0'; // terminate the string
            ndx = 0;
            newData = true;
        }
    }
}

void showNewData() {
    if (newData == true) {
        Serial.print("Got this: ");
        Serial.print(receivedChars);
        newData = false;
        Serial.println(">");
    }
}

---------------------Gives me this---------------------------------

<Arduino is ready>
Got this: .Û÷"÷[vCá161227 213014 (GMT+0h)
Ready>

Software Serial and higher baudrates might be the problem.

aslak: Note that I'm actually connecting to a RS232 device and giving it the command to show me the time.

Do you have something like a MAX232 chip to convert between RS232 voltage levels and the TTL voltage levels used by Arduino Serial? RS232 voltage levels could damage an Arduino.

...R

@Robin2 Yes - I’m using a linksprite UNO <-> RS232 shield.

Try SoftwareSerial at 9600 baud and when that works you can experiment with higher speeds.

If you really need higher speeds use a Mega (with 3 spare HardwareSerial ports) or a Leonardo or Micro which has one spare port.

...R

@sterretje and @Robin2, I'll give lower speeds a shot.

Forgot to mention: all sketches DO return valid data but also junk is included.

Valid data

161227 213014 (GMT+0h)
Ready>

This could be caused by SoftwareSerial. It cannot transmit and receive at the same time. This means that sending a character can corrupt the received character. You should also know that SoftwareSerial disables interrupts for long periods of time. When your sketch gets more complicated, this can interfere with other parts of your sketch or with libraries.

Instead, try AltSoftSerial. It is much, much better than SoftwareSerial, but you can only use it on pins 8 & 9. It can transmit and receive at the same time.

If you could use 38400 baud instead of 57600, NeoSWSerial is almost as good. It works on any two pins, it can transmit and receive at the same time, but it only implements baud rates 9600, 19200 and 38400.

Although it’s not causing the problem now, using the String class could cause problems later, when your sketch is more complex. Look at the “Serial Input Basics” section of the Useful Links page. It shows you how to use character arrays (aka C strings) to accumulate a line.

Cheers,
/dev

...and Bob's your uncle! Now it's working like it should and perfectly!

Thank you @/dev , I see that the library is written by you - nice!