HELP: Recording RS232 Output to Arduino Uno

I'm very new to Arduino so apologies if this is a naive problem.

My goal is to read and record RS232 output from a balance. I have the Arduino (with an SD shield) and an RS232 to TTL converter (this model: http://www.nkcelectronics.com/rs232-to-ttl-converter-board-33v232335.html) hooked up as in the attached diagram (TX -> Pin 0, and RX -> Pin 1). I have the following sketch (attached) which should simply write the RS232 output to the SD card.

However, when I check the serial monitor the following appears:

Initializing SD card...card initialized.
Arduino Gotcha:0
0
Arduino Gotcha:0
0
Arduino Gotcha:104
104
Arduino Gotcha:0
0
Arduino Gotcha:16
16
Arduino Gotcha:1
1
Arduino Gotcha:3
3
Arduino Gotcha:12
12
Arduino Gotcha:0
0
Arduino Gotcha:0
0
Arduino Gotcha:0
0

It appears the Arduino is getting a signal from the RS232, but it's gibberish. I've tested the RS232 output by hooking it up to my computer - the output is fine.

I've combed through the forums and google but am completely stumped. Does anyone have any suggestions?

Thanks,
-Greg

sketch_apr17a.ino (1.28 KB)

And baudrate, start and stop bits and parity all match?

If your computer has RS232 port, then hook up your setup to that port and open that port, then type in something and see if arduino receives it correctly. This is to check you have a working converter and correct receive code. You may be receiving one character at a time and somehow print it in integer value instead of character. Try this and also check baudrate etc. too.

I double checked the baud rate (9600), Data bit (8), Parity (None) and Stop bit (1) - they match between the Arduino and output from the balance.

I've hooked my set-up to my PC but am unsure how to "type in something" into the port (I tried googling to no avail) - any pointers? For what it's worth, when I plug/unplug the set-up to the PC I get a signal ("Arduino Gotcha: 0"). If I leave the set-up as is, then it's a continuous stream of zeroes. Here is an example of what I mean:

0
0
0
0
0
Arduino Gotcha:0 //I unplugged the set-up here
0
0
0
Arduino Gotcha:0 //I plugged the set-up back in here, etc...
0
Arduino Gotcha:0
0
Arduino Gotcha:0
0
Arduino Gotcha:0
0
Arduino Gotcha:0
0
0
0
0

I should also clarify: the output from the balance is ASCII format. The message I receive successfully on the PC is "W: +45.81" (or whatever the weight is). Can the Arduino deal with ASCII format?

Try not to use pin 0 and 1 for RS232 communication since Arduino UNO also use it as a serial monitor and uploading sketch. It will cause conflict and data may be corrupted.

Check SoftwareSerial http://www.arduino.cc/en/Reference/softwareSerial

Try to use SoftwareSerial with pins other than 0 and 1, e.g. pin 10 and 11, and debug it using serial monitor.

If you insist using pin 0 and 1 for RS232 comm, don't connect USB to computer, use barrel jack or pin Vin to power Arduino UNO.

I've read up on Software Serial and updated the code as follows (to the best of my ability) in addition to moving to pins 10/11:

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

int counter = 0;
int particles = 0;

void loop()
{
 delay(1000);
 if (mySerial.available() >0) {
   particles = mySerial.read();
   mySerial.println("Arduino Gotcha:");
   mySerial.println(particles, DEC); // print as a raw byte value
   delay(1000);
   mySerial.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:
   mySerial.println(particles);
 }  
 // if the file isn't open, pop up an error:
 else {
   mySerial.println("error opening datalog.txt");
 } 
}

It compiles okay and after I upload it nothing appears in the serial monitor. It should print what it receives on Digital Port 10, correct? I'm admittedly a complete novice at coding. Any advice?

You can use a serial port program to open the serial port and type something up. I use this serial port program:

Your computer probably has serial ports 1 and 2.
For your code, you don't want the .flush() line. Also remove all the delay().
mySerial.begin(9600); Change to Serial.begin(9600);
Change all mySerial.print to Serial.print. What you are doing is to send the printout to your balance. I don't know if it expects any message. Send to PC instead using Serial

gwentwor:
I've read up on Software Serial and updated the code as follows (to the best of my ability) in addition to moving to pins 10/11:

#include <SD.h>

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

int counter = 0;
int particles = 0;

void loop()
{
delay(1000);
if (mySerial.available() >0) {
  particles = mySerial.read();
  mySerial.println("Arduino Gotcha:");
  mySerial.println(particles, DEC); // print as a raw byte value
  delay(1000);
  mySerial.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:
  mySerial.println(particles);

// if the file isn't open, pop up an error:
else {
  mySerial.println("error opening datalog.txt");
}
}




It compiles okay and after I upload it nothing appears in the serial monitor. It should print what it receives on Digital Port 10, correct? I'm admittedly a complete novice at coding. Any advice?

I am guessing that your SD is on a shield so you don't know what pins it uses. 10 and 11 are two of the SPI pins and SD uses SPI. I know because my SD is on a module, I have to stick the jumpers in and I'm looking at an UNO with SD, 10 to 13 + GND are filled. I've gone with 2 and 3 for soft serial with SD.

Also there's a debug technique we use when we're not sure what the code is doing. We print short messages to serial monitor from different places in the code to show us what path the code took and what critical values we want to know. It can work very well, check yourself this way as well as your code.

When you have SD, you can log run data to that. If there's a problem, you might have data on how it got there.

Liudr: thanks! I've changed the code as you suggested. I've got my PC triggering a response on the Arduino. When I send a message via the Terminal program it triggers the "Arduino Gotcha: n" in the Arduino terminal window. However, the message is not received properly - it either spits out a "0" or (sometimes) a single, double, or triple digit number in the terminal window. I am unsure why the message is not received properly - any ideas?

GoForSmoke: Thanks for the info about the SD shield (pins 10-13), I did not know that. Following your suggestion, I've modified the .ide for soft serial use on pins 2/3. Please note, this code is a modification on what I described for liudr since he suggested not using soft serial and (I think) using the default pins 0/1 (although xkimi pointed out, this can cause issues with the SD shield):

#include <SD.h>
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2,3);
const int chipSelect = 10;
void setup()
{
 Serial.begin(9600);
 mySerial.begin(9600);
 Serial.println("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); 
 pinMode(2, INPUT);
 pinMode(3, 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(1000);
 if (mySerial.available() >0) {
   particles = mySerial.read();
   Serial.println("Arduino Gotcha:");
   Serial.println(particles, DEC); // print as a raw byte value
   delay(1000);
   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");
 } 
}

However, when I upload this and watch serial monitor the input (either the balance or PC terminal) fail to communicate with the Arduino (i.e. the "Arduino Gotcha: n" does not appear). I've tried playing around with the code to no avail. Am I missing anything obvious?

Thanks,
-Greg

How about reducing factors? Make a sketch that only reads the software serial and prints out to serial monitor?

That could show that the wiring and hardware are working or make an easier to find solution if not.

remove flush, remove delay, then replace Serial.println(particles,DEC); with Serial.write(particles); Serial.println();