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.begin(9600);
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:
return;
}
Serial.println("card initialized.");
}
int counter = 0;
int particles = 0;
void loop()
{
delay(300);
if (Serial.available() >0) {
particles = Serial.read();
Serial.print("Arduino Received:");
Serial.println(particles, BYTE); // print as a raw byte value
delay(10);
Serial.flush();
}
// 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) {
dataFile.println(particles);
dataFile.close();
// print to the serial port too:
Serial.println(particles);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
}