[Solved]Reading data from an RS232 port

Hello, I have a commercial air quality monitor that output’s data every 6 seconds. I have verified that the product is indeed sending out data by connecting it to the serial port on my computer.

I verified that the RS232 shield is working by loading the following example code from the Serial.Read() reference page:

int incomingByte = 0;	// for incoming serial data

void setup() {
	Serial.begin(9600);	// opens serial port, sets data rate to 9600 bps

void loop() {

	// send data only when you receive data:
	if (Serial.available() > 0) {
		// read the incoming byte:
		incomingByte = Serial.read();

		// say what you got:
		Serial.print("I received: ");
		Serial.println(incomingByte, DEC);

With the arduino powered by an external battery pack, I was able to communicate reliably with the arduino through the RS232 port. However, when my air quality monitor is plugged in, there is no output to the terminal. (of course I powered the arduino through the USB so that I could monitor it)

When I connect the air quality monitor to my Arduino through a Cutedigi RS232 shield, it never reads any data. It seems like the data never goes into the serial buffer, since my code never passes the Serial.available() >0 if statement.

Any ideas why this would be the case? For reference, I have attached my project sketch that I am using below.

Function: Read data from an RS232 port and write to an SD card
Based on example code found on the internet

 #include <SD.h> 
const int chipSelect = 10;
void setup()
  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
  Serial.println("card initialized.");

int counter = 0;
int particles = 0;

void loop()
  if (Serial.available() >0) {
    particles = Serial.read();
    Serial.print("Arduino Received:");
    Serial.println(particles, BYTE); // print as a raw byte value
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    // print to the serial port too:
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");

A link to the device would be nice.

You need to have a MAX232 (or similar, RS232-to-TTL converter) between the air quality monitor [out] and the arduino [in].

This is the product:

I am using a Max232 circuit to decode the RS232 signal. This chip is embedded in the RS232 Shield provided by Cutedigi (http://www.cutedigi.com/product_info.php?products_id=4329).


Serial.println(particles, BYTE);
That results the ascii of "particles"

prints the numeric of "particles"
which, I believe, is the same result as
Serial.println(particles, DEC);

[Maybe PaulS will rip me a new one.]

What's the data look like that you're getting with the AQM connected to the PC?

The AQM outputs data every 6 seconds in this form:


(two numbers separated by a comma, and a carriage return and line feed.)

Right now, when I have the AQM connected to the microcontroller, Serial.Available() is always 0, so the arduino never even reads anything. How come this is happening? When connected to the PC, the data appears in the terminal just fine.


How about getting rid of the SD-related stuff and working solely the serial issue first?

Is that Delay(300) a monkey-wrenching the deal?

Tried doing it without the SD card stuff, didn't help at all. For your reference, this is the sketch I used:

int incomingByte = 0;    // variable to hold the analog value

void setup() {
  // open the serial port at 9600 bps:

void loop() {
  int test = Serial.available();
  if (Serial.available() > 0) {
            // read the incoming byte:
            incomingByte = Serial.read();

  // print it out
  Serial.print("Arduino Received: ");
  Serial.println(incomingByte, BYTE); // print as a raw byte value

  // delay 10 milliseconds before the next reading:

The result was just a line of 0's scrolling in the serial terminal window.

I'm not sure what you meant by monkey-wrenching, but the delay was there just to slow down the loop.

Just run by us the exact wiring you used here? You plugged the device into where?

The DB9 connector from the AQM device is plugged into the cutedigi rs232 shield. This shield is mounted on top of the Arduino microcontroller. I also have an SD card datalogging shield, but I have removed this for the tests that I am running.

The cutedigi rs232 shield uses pins 0 and 1 for the serial rx and tx communications.

Have you got a data sheet for the gadget? I can't find it. Maybe Rx and Tx are swapped over.

I found a schematic on the product website. They didn't have anything else unfortunately.


Although, if the pins were swapped, how would I be able to communicate with the arduino using my computer? I can get that to work fine, but when I connect the air quality device to the arduino, the data doesn't seem to ever make it into the serial buffer (calling Serial.available() gives 0). If I disconnect the air quality device from my arduino, and plug it into my computer's serial port, the data shows up in my terminal, just as it was designed.

That isn't the device. That's the interface board. All I am saying is that if the air quality device has the Tx and Rx pins swapped then you would get those symptoms. Try getting a null-modem pin-swapping gadget.

It may be worth your time to get a logic analyzer like this one:

For $US 149 you would soon see what baud rate it was using, and which pin is which.

Ah sorry, I misinterpreted your question.

9600 baud
8 bits
no parity
1 stop bit
no flow control

These are the specifications that are in the product manual. It works just fine actually. When I plug the device into the serial port of my computer I can read the data from a terminal window. That is precisely what has me so confused. I know the RS232 interface arduino shield works, and I know that the device works fine with my computer, so why doesn't it play nicely with the arduino?

What pin on the DB9 connector does it transmit on, and what pin does it receive on? That is the fundamental point.

You know, I'll give the manufacturer a call on Monday to find out exactly which ones they use.

I assumed that it would be the standard pin config (2 for RX 3 for TX) since it worked fine when I plugged it into my computer, but it sounds like you're suggesting its possible that the pins are mismatched and I could still read the data through a PC serial connection.

Thanks for your responses

You might want to ask for their data sheet.

I might be talking rubbish here (I’ve only had an arduino for a short time), but doesn’t Serial.begin() open the internal UART? Would you not need an alternative method to use an external serial device? Otherwise, I can’t see how you could Serial.print() back through the USB interface, and Serial.read() to the external port at the same time.

As I say, I’m new to arduino, so please correct me if I’m wrong, as I might want to do something similar at some point.

I was looking at the pic and schematic of your 232 board.
It has RXD and TXD LEDs. What’s happening with those?

Okay guys I think I have found the issue. Nick Gammon hit the nail on the head.

The RS232 Shield and the device are both DCE devices, and the computer is a DTE device. I was using a gender changer to connect the sheild and the device, but this doesn't work. The RX/TX pairs need to be swapped for the two devices to talk to each other (otherwise the RX of one device is connected to the RX of the other device and they're both waiting around for something to happen).

So, if anyone ever runs into problems like this, try getting a null modem cable.

Thanks for all of your help guys.