SoftwareSerial garbage on second port

I am an Arduino newbie and working on my first real project (after doing a number of the exercises). The idea is to create a method of watering plants when a sensor reports that the moisture level is low (I am known for killing my herbs). Once working I will offer it to the family so want to keep the costs down. The system will be controlled by an application running on a serial connected PC, written in Visual Basic.net, which I am quite familiar with. The sketch code is:

/* Arduino Automatic Watering System

*/
#include <SoftwareSerial.h>

const int rxPin = 2;                    // pin used to receive data from PC
const int txPin = 3;                    // pin used to send data to the PC
SoftwareSerial serial_pc(rxPin, txPin); // new serial port on pins 2 and 3

int number = 0;

void setup() {
  Serial.begin(9600);                   // built in serial port
  Serial.println("On board port ready.");
  serial_pc.begin(9600);                // initialise PC serial port
  Serial.println("Externsl port ready.");
}

void loop() {
  //Check for operator input
  funcAbort();

  serial_pc.println(number);
  delay(10);

  //Serial.print("*");
  Serial.println(number);

  //Check for operator input
  funcAbort();

  delay(5000);
  number ++;
}

// Functions

/*  Read input from the operator via the Serial interface
     RAT Replace with input from the network controller
*/
void funcAbort() {
  int incomingByte;
  char incomingChar;

  while (Serial.available() > 0) {
    //Read operator input byte by byte
    // maybe use Serial.readBytes to get the whole string?
    incomingByte = Serial.read();
    incomingChar = char(incomingByte);
    if (incomingChar == 'a') { // SIngle quotes for single characters
      Serial.println();
      Serial.println("Aborted");
      delay(100);
      //Exit with status zero (no errors)
      exit(0);
    }
    else
    {
      Serial.print(incomingChar);
    }
  }
}

The circuit is simple as well, one connection from GND to the RS232 ground pin, Arduino pin 2 to the RS232 XMT pin and one from pin 3 on the Arduino to the RS232 RCV pin.
As I am new, I wrote a simple serial interface in VB.net and an Arduino sketch to test the link, and was dismayed when I saw the garbage I received on the PC when I ran the monitor. The serial monitor on the Arduino correctly showed a number counting up:
Arduino monitor
My monitor showed garbage instead of the expected numbers:


I assumed I has made a mistake in my monitor software, so I ran the sketch against a PuTTY serial monitor and got more garbage, which indicates to me the problem was at the Arduino end. After much Googling and searching the Forum I am at a loss to explain the problem The baud rates all match, though I experimented with different rates on the monitors to see if that had any affect (none).
So, can anybody see what I am doing wrong?

I am assuming you are not using the PC USB to emulate a serial data connection, you are using a real one?
Paul

Also, I see you are using a 16 bit integer to store the incoming BYTE. Then you print it as a byte, which likely gives you the high order byte of the 16 bit integer.

Wow, that was quick, thank you.
Yes, the monitor software and PuTTY are running on a real PC - the connection is through an RS232 breakout box to a USB Serial converter to the PC. Is this likely to cause a problem?
Well spotted with the int to byte problem, I will change that and see if it has any effect.

Hello
Check rx- and tx-wires to be connected correct.

RX and TX wires are correct - although I assume there would be nothing showing on the monitors if I got that wrong. In answer to Paul's earlier response, changing from int to byte in functAbort had no noticeable effect on the monitor - i did not expect it to as that is handled by SoftwareSerial. but as I am new to this, I changed it anyway.

Check your RS232 CTS and RTS lines.
https://tldp.org/HOWTO/Serial-HOWTO-19.html
and the No handshake null modem diagram on
https://www.digi.com/support/knowledge-base/scsi-terminal-server-sts-and-etherlite-signal-and

Thanks for the info and links, but I looked into handshaking and could see no method for SoftwareSerial to handle the subject, However, handshaking errors would lose characters rather than scrambling them as is happening here.

Problem solved :smiley: well, bypassed rather than solved, but in my book, anything that works is a solution! In my original configuration, I was using a serial breakout box to connect to the computer - not a satisfactory situation in the first place (can you buy a PC nowadays with a serial port?). I found a USB to serial convertor that could plug in to the Arduino, wired that up and lo and behold, all worked just as I wanted and expected it to, without any modifications!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.