Arduino to RS-232 Device

Hi all,

I want to send the following from Arduino to an RS-232 device via software serial. “*01E0000#”

Upon recieving this code the device must normally reply back with a status," Ready".

I used the following code. The device already has an inbulit MAX232 chip and the signal levels are matched. I connected the Tx of device to Rx of Arduino and vice-versa and the ground pins together.

But the device does not respond to the code. Can anyone please help? Thank you in advance.

#include <SoftwareSerial.h>//library for communicating to a RS-232 device

SoftwareSerial mySerial(15,14);// use SoftwareSerial with Rx on Pin 15 and Tx and Pin 14

void setup()
{
  mySerial.begin(9600);// To communicate with RS-232 Device via the MAX232 chip 
  
  Serial.begin(9600);// To communicate with the computer

}

void loop() 

{ 
   mySerial.write("*01E0000#");//sets ventil 1 
 
   delay(10);
 
  mySerial.write("*0110000#");//opens ventil 1 
 
  if (Serial.available())//if the RS-232 has data to return
  {
  Serial.write(mySerial.read());
  }
}

You send the thing some data. You twiddle your thumbs for a while. Then, you send the thing some more data. Then, if the device has responded, you read one byte. Then, you repeat sending, waiting, sending, and reading one byte.

I don't understand that logic.

Does the device have any LEDs that indicate that it is getting data? That it is sending a response?

May be the code is a bit mis-leading. The device already has an in built programmed Micro-controller and is not visible from the outside and hence no LED's . After it recieves the code it must respond with a CLICK sound and a "Ready" reply to the Arduino. Like you have mentioned,if it responds, I read one byte and I know that it "does" work.. But the problem is that it does not respond....

Should

mySerial.write("*01E0000#");//sets ventil 1

be

mySerial.print("*01E0000#");//sets ventil 1

and should

if (Serial.available())//if the RS-232 has data to return

be

if (mySerial.available())//if the RS-232 has data to return

Thank you for your comments. I do get that it should be mySerial.available. But even when I try it with mySerial.print it does not respond. :(

The sending is the easy part.

Receiving data needs some care. Have a look at the examples in serial input basics

...R

Thank you for your post link. It seems really informative and will benefit from it for receiving data to Arduino. But I am still stuck at Phase 1. I am sure that the device does not read the "*01E0000#" I am sending. :(

The device has a C program writen on it's Micro-controller already. When I connect the RS-232 device directly to my laptop and send the same command (WITH IT'S GUI INTERFACE) it works and responds with a CLICK sound and with a string "Ready".

When I send the command using a mySerial.write or mySerial.print it does not respond. That is where I am not sure if I have to convert the "*01E0000#" to HEX or DEC or something else. I am sending it via an Rx Tx using the SoftareSerial.

Thank you...

srivathsaann: When I send the command using a mySerial.write or mySerial.print it does not respond. That is where I am not sure if I have to convert the "*01E0000#" to HEX or DEC or something else. I am sending it via an Rx Tx using the SoftareSerial.

Maybe you need to terminate the line with carriage return/ linefeed characters? Try using .println instead of .print or just change the string to "*01E0000#\r\n"

srivathsaann: The device has a C program writen on it's Micro-controller already. When I connect the RS-232 device directly to my laptop and send the same command (WITH IT'S GUI INTERFACE) it works and responds with a CLICK sound and with a string "Ready".

Without suitable receiving code on your Arduino you cannot receive the "Ready" message.

Can you write a simple Arduino program to show the exact bytes that your device sends - so that you can be sure you are sending the correct data to the PC ?

...R

Hi,

I tried with println too, but it does not work yet because there is no serial connection detected. I suppose there is some error in the declaration of the serial pins...

if (mySerial.available())//if the RS-232 has data to return
  {
Serial.write("hi");  
Serial.write(mySerial.read());
  }

I tried this code above just to check if the RS-232 device establishes a serial connection. But it does not.. :(

Is there a problem with mySerial? or it's declaration? The connections seem pretty straightforward too.

But it is upsetting that it does not work...

Regarding the no. of bytes... I am not sure how to predict it... The longest the device replies will be as following string: The ventil is in use...

srivathsaann: I tried this code above just to check if the RS-232 device establishes a serial connection. But it does not.. :(

Is there a problem with mySerial? or it's declaration? The connections seem pretty straightforward too.

Try using software serial on a couple of standard digital pins instead of using analogue pins in digital mode. For some reason I feel this particular problem of software serial not working on analogue pins has happened before on here.

Hi all,

I am still trying this but with no success. :frowning: The problem is still the same that the Arduino does not establish a serial communication with the Rs-232 device.

I tried all possibilities with different digital pins as Tx Rx and also Serial 1, Serial 2 and Serial 3 (as I have an Arduino Mega). But in vain. There is no serial port identified. I thought may be I must use the Altsoftware serial library and tried the following.

Still it does not recognize the RS-232 device. I am supplying this Rs-232 device with a 24 V Regulated Power Supply.

After running the following code , the serial monitor does not print “hi”. However it does when I unplug the Tx and Rx wires, or turn OFF the power supply to the device…

I am totally confused and lost… Can anyone please help me?

Thank you in advance.

#include <AltSoftSerial.h>

// AltSoftSerial always uses these pins:
//
// Board          Transmit  Receive   PWM Unusable
// -----          --------  -------   ------------
// Teensy 3.0 & 3.1  21        20         22
// Teensy 2.0         9        10       (none)
// Teensy++ 2.0      25         4       26, 27
// Arduino Uno        9         8         10
// Arduino Leonardo   5        13       (none)
// Arduino Mega      46        48       44, 45
// Wiring-S           5         6          4
// Sanguino          13        14         12

AltSoftSerial altSerial;

void setup() {
  Serial.begin(9600);
  while (!Serial) ; // wait for Arduino Serial Monitor to open
  Serial.println("AltSoftSerial Test Begin");
  altSerial.begin(9600);
  //altSerial.println("Hello World");
}

void loop() {
  char c;

  if (Serial.available()) {
    c = Serial.read();
    altSerial.print(c);
  }
  if (altSerial.available())
  {
 Serial.println("hi");
    c = altSerial.read();
    Serial.print(c);
  }
}

If you can't talk to the device using HardwareSerial, it is unreasonable to assume that software serial will make magic happen.

Hi Paul,

the other device which I want to connect via arduino expects a port. I guess the rx and tx as hardwareSerial does not act as a port or does it?? If I have a port connected to my device, for example the computer (via usb and db9 converter) then the device is giving a response as ready. The device is kinda black box to me as I don't have the entire code for the program written in the micro-controller.

Thanks.

DB9 describes a kind of connector. What do you mean by "db9 converter"? I'm guessing that it includes a MAX232 chip to convert the TTL levels of a USB port to RS232 levels that serial ports (which is what a DB9 connector connects to) use.

Two of the 9 pins are TX and RX. Another is ground. The other 6 have their purposes, but can generally be ignored.

Hi,

the device has an inbuilt MAX232 which converts TTL to RS232. From the device I have Rx, Tx, Ground, and power supply for the device. The device will take care of TTL levels. The Rx,Tx and Ground which I get from the device is connected to a db9 connector but to convert it to USB format I use db9 to USB converter. It works fine when I do it in such a manner.

When I have Rx,Tx and Ground from device to the arduino HardwareSerial port and try to read it and display in serial monitor it doesn't work.

srivathsaann: Hi,

the device has an inbuilt MAX232 which converts TTL to RS232. From the device I have Rx, Tx, Ground, and power supply for the device. The device will take care of TTL levels. The Rx,Tx and Ground which I get from the device is connected to a db9 connector but to convert it to USB format I use db9 to USB converter. It works fine when I do it in such a manner.

When I have Rx,Tx and Ground from device to the arduino HardwareSerial port and try to read it and display in serial monitor it doesn't work.

As the device you trying to talk to has RS232 to TLL level converter I assume the thing is putting out RS232 level serial. Please tell me your not plugging this directly into the Arduino pins but your running it through another RS232 to TLL level converter to make it safe/suitable for the Arduino.

Please tell me your not plugging this directly into the Arduino pins but your running it through another RS232 to TLL level converter to make it safe/suitable for the Arduino

OMG.Yes, I am connecting it directly to Arduino. I thought one RS232 to TTL was sufficient. Should I have one more to level it?

Oh may be I am understanding it right now. Thanks to you. Please tell me if this understanding is right.

TTL should be on the side of micro-controller or Atmega. And if we need RS232 out then it should be converted and taken via a db9 kinda pin...(right??)

Yes: http://www.nkcelectronics.com/rs232-to-ttl-converter-board-33v232335.html or http://www.nkcelectronics.com/RS232-to-TTL-converter-board-DTE-with-Male-DB9-33V-to-5V_p_369.html for example

Thanks for the links :slight_smile: Hope I can get further. I will try with this converter or make one by myself. I have one MAX232.