Tutorial: Collecting data via RS-232 output from a device/gauge/instrument

Introduction
This is a useful procedure to use/learn if you have a device (/gauge/instrument), such as a force gauge, a barcode reader, a GPS device, whatever, from which you want to collect data to, e.g., your Arduino.

These notes are based on my knowledge/research over the last few days (and thanks importantly to forum members here who answered my questions!); not everything might be 100% correct but things in this case are pretty simple.

Here is a tutorial on the basic idea of setting things up.
Note 1: In this tutorial, I’ll call the external thing the “device”, and your microcontroller the “Arduino” (because I tested with the Uno).
Note 2: Some specifics in the setup will vary based on your particular device, as I explain below.
Note 3: This tutorial is written at a beginner comprehension level.

Steps
(1) Ideally, to start, you need to have something that has a female 9-pin output from your device. If necessary (in the case of your device having a standard 25 pin connector), buy a converter cable that gives you what is called a DB9 plug at the end.

(2) The DB9 plug has 9 pins (VERY important: the order of these pins might be different/mirrored based on which way you look at the plug, which cable/DB9 plug you bought, and which device you have… a number of things, so best is to check datasheet… And/Or if you’re using a converter cable in between, use a multimeter to find which pins go to which).
Again, TYPICAL (not always) digram (source: http://www.arcelect.com/rs232.htm):

(3) THREE of these pins are important in most cases (in some cases, other pins such as DTR might be important to be used as trigger interrupts).
The pins are: Ground, RX (receive TO device), TX (transmit FROM device)

(4) Now, there is a difference between RS-232 and TTL. The latter, TTL, is the voltage HIGHs and LOWs used in typical digital communication, e.g., in the case of Arduino, and has HIGH near 5 Volts and LOW near 0 Volts. On the other hand, RS-232 is a protocol that uses voltage pulses of a different magnitude (e.g., plus 12 volts might be used for high, and minus 12 volts for low). Also RS-232 has inverted signals with respect to standard digital communication.

(5) So you need to use something in between your device and your Arduino. There is a popular, inexpensive chip that does the protocol conversion for you: a MAX232 chip. (That’s Maxim’s version; other chip models with 232 at the end are also likely to work).
VERY easy to setup. You need FIVE capacitors of 1 microfarad, and set things up as appropriate; for this, please refer to this diagram from the datasheet of MAX232 (http://datasheets.maxim-ic.com/en/ds/MAX220-MAX249.pdf)

(6) At this point, for example, you can wire up the cables from your device and from your Arduino to the MAX232 chip as follows:
pin 11 of MAX232 – the TX of your Arduino’s serial
pin 12 of MAX232 – the RX of your Arduino’s serial
pin 13 of MAX232 – the TX of your device’s RS-232
pin 14 of MAX232 – the RX of your device’s RS-232
Make sure you also wire up GND of the MAX232 to your Arduino and to your device’s RS-232 GND pin.
And of course, wire up 5V output of your Arduino to where it says “5V” on the above circuit diagram.

(7) Note that, by Arduino’s serial, I of course mean the RX and TX on pins 0 and 1 (they are also labeled). However, you might want to use these pins for communication with your computer (which is the default setup when you don’t wire anything to pins 0 and 1). In that case, you can create Serial communication on any other pins of your Arduino with the help of the popular NewSoftSerial library; very simple to use. Download and read about it here: http://arduiniana.org/libraries/newsoftserial/
My code provided below uses NewSoftSerial library.

[8] Now you’re all set. Some devices send (TX) data out continuously by default, some devices take (RX) data, and some devices send data only upon receiving something (TX after RX).
In all cases, your device’s manual/datasheet should explain the specifications involved.
Specifications to watch for:
–Baud rate: Change your Serial.begin’s baudrate accordingly.
–Start/stop bits: NewSoftSerial library’s serial print and read commands automatically take care of these bits, so you’re good.
–Commands: If your device only sends data upon receiving something, that means you need to first send it a command character, often an ASCII character, and often followed by a carriage return.
Again, when you do a print command with NewSoftSerial, you’ll be set, but for carriage return, you do need to use “.print(13,BYTE)”… here, 13 corresponds to an ASCII carriage return.

Code
Here is my present code (remember to change baud rate and change/delete the character commands sent OUT from Arduino as appropriate):

#include <NewSoftSerial.h>

// use NewSoftSerial with RX on pin 2, TX on pin 3
NewSoftSerial mySerial (2, 3);

// a variable for keeping track of time
unsigned long start = 0;

void setup() {
// 'mySerial' is for communication with the RS-232 device via the MAX232 chip
  mySerial.begin(19200);

// 'Serial' is for communication with computer of course
  Serial.begin(19200);
}

void loop() {
// Send out a character command 'D' (followed by carriage return) to
// the device, which in my device's case tells it to reply with data
    mySerial.print('D'); mySerial.print(13,BYTE);
  
// wait up to 300 ms to see if device replies with data
    start = millis();
    while (!mySerial.available() && (millis()-start) < 300) {}

// fetch data via mySerial, then print it out on PC's serial monitor
    while (mySerial.available() ) {
        Serial.print((char)mySerial.read());
    }

// do all of this every 300 ms
  delay(300);
}

rs232.GIF

max232.gif

It is not working. I have Arduino 1.8.5.. So the NewSoftSerial.h library is changed to SoftwareSerial.h but its not working. Random numbers from 0 to 255 on Serial monitor only