Arduino to RS232 interface issue

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
eujin888
 
Posts: 5
Joined: Sat Feb 06, 2016 12:48 am

Arduino to RS232 interface issue

Post by eujin888 »

I am trying to interface an arduino mega via a level converter to a RS232 device. To activate the RS232 device, I found I have to send string of hex codes. When I send the codes direct from the PC to the RS232 device, I see data being read back. But when I send the codes from the arduino/level converter, the RS232 device sends nothing to the arduino serial monitor.

Codes sent to RS232 device:
0x55,0xAA,0x01,0x00,0x00,0x00,0x00,0x01,0x00

I expect the RS232 device to respond:
55 AA 01 00 00 00 25 00 01 00 01 00 5E 00 5E 00 00 00 00 00 00 00 22 00 22 00 22 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 02 49

The arduino program I used:

Code: Select all


#include <SoftwareSerial.h>

int i = 0;
int x = 1 ;
byte GetData[] = {0x55,0xAA,0x01,0x00,0x00,0x00,0x00,0x01,0x00};

void setup() {
 Serial.begin(9600);
}
   
void loop()
{

 if(x>0){
   for (int i=0; i<sizeof(GetData); i++){
     Serial.write(GetData[i]);
   }
        x=x-1;
     }
 if (Serial.available()) {
   Serial.println("data");
   while(Serial.available()>0) {
     byte inByte = Serial.read();
     //Serial.print(inByte);
     delay(10);
   }}
} 
The arduino serial monitor is blank.

Please help. Is there some bugs in my arduino program, or I have wrong configuration.

Thank you.

User avatar
Franklin97355
 
Posts: 23910
Joined: Mon Apr 21, 2008 2:33 pm

Re: Arduino to RS232 interface issue

Post by Franklin97355 »

What are you using between the Arduino and the RS232 device? You need a TTL to RS232 converter since the signal is not just shifted in level but also polarity at times.

User avatar
eujin888
 
Posts: 5
Joined: Sat Feb 06, 2016 12:48 am

Re: Arduino to RS232 interface issue

Post by eujin888 »

franklin97355,

I am using a rs232 to ttl converter (http://76.my/Malaysia/rs232-to-ttl-conv ... [email protected]) between the arduino mega and the rs232 device(an esmart max power point tracker) in a solar project. I want to send a signal to the rs232 device to activate it then read the rs232 output.

TTL to RS232 converter info:
1) Convert RS232 signal (serial COM port) to TTL signal.
2) Operating voltage: 3.3 to 5Vdc. Operating current: 13mA.
3) Power LED indicator.
4) RS232 DB9 female connector.
5) Pinout:
VCC = 3.3 to 5V
RXD = Transmit data (Out)
TXD = Receive data (In)
GND = Ground

User avatar
Franklin97355
 
Posts: 23910
Joined: Mon Apr 21, 2008 2:33 pm

Re: Arduino to RS232 interface issue

Post by Franklin97355 »

Could you post some pictures of your connections and the pinout for the converter so we can have a look?

User avatar
eujin888
 
Posts: 5
Joined: Sat Feb 06, 2016 12:48 am

Re: Arduino to RS232 interface issue

Post by eujin888 »

I use a ttl to rs232 converter between arduino and rs232 device(an e-smart MPPT).

I attach a photo of the set-up.

Arduino Mega pin 10 to TTL/RS232 converter TXD; pin11 to TTL/RS232 converter RXD.
TTL/RS232 connector output to e-smart MPPT RS232 port.


Kindly advise/instruct. Thank you.
Attachments
IMG_6675.JPG
IMG_6675.JPG (442.4 KiB) Viewed 1596 times

User avatar
Franklin97355
 
Posts: 23910
Joined: Mon Apr 21, 2008 2:33 pm

Re: Arduino to RS232 interface issue

Post by Franklin97355 »

You are using pins that are not the TX and RX pins on the Mega but you don't use software serial in your program. Since the Mega has three serial ports why not use one of those? Post your complete and current code please.

User avatar
eujin888
 
Posts: 5
Joined: Sat Feb 06, 2016 12:48 am

Re: Arduino to RS232 interface issue

Post by eujin888 »

This is the complete and current code.


Code: Select all

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX

byte GetData[] = {0x55,0xAA,0x01,0x00,0x00,0x00,0x00,0x01,0x00};

int i = 0;
int x = 1 ; 
void setup() {

 Serial.begin(9600);
 mySerial.begin(9600);

}

void loop()
{

 //char buffer[4];
 
 if(i>30000) {
// Serial.println("Sending...");
 if(x>0){
   for (int i=0; i<sizeof(GetData); i++){
   
    //sprintf(buffer,"%00c",GetData[i]);
     
     Serial.write(GetData[i]);
     mySerial.write(GetData[i]);
    
      
     }
   
 }
   //Serial.println("");
    x= x  - 1 ; 
    i=0;
 }
 if (mySerial.available()) {
   Serial.println("data");
   int inByte = mySerial.read();
   Serial.print(inByte);
 }
 if (Serial.available()) {
  
   int inByte = Serial.read();
   mySerial.write(inByte);
   Serial.write(inByte);
   
 }
 i++;
}

User avatar
luciusjo
 
Posts: 35
Joined: Sun Jul 12, 2015 8:22 pm

Re: Arduino to RS232 interface issue

Post by luciusjo »

I recently used a similar product for work, TTL to RS232. What seemed to help me debug the most was to use two serial ports (2 on the mega or 1 and a softSerial); using one to send/read the 232 signal, and the other for the arduino IDE serial monitor to print information and or the response from the 232 port.

User avatar
eujin888
 
Posts: 5
Joined: Sat Feb 06, 2016 12:48 am

Re: Arduino to RS232 interface issue

Post by eujin888 »

luciusjo wrote:I recently used a similar product for work, TTL to RS232. What seemed to help me debug the most was to use two serial ports (2 on the mega or 1 and a softSerial); using one to send/read the 232 signal, and the other for the arduino IDE serial monitor to print information and or the response from the 232 port.
Hi luciusjo, can you please share the coding?

Thank you.

User avatar
luciusjo
 
Posts: 35
Joined: Sun Jul 12, 2015 8:22 pm

Re: Arduino to RS232 interface issue

Post by luciusjo »

Here is the base of my code. this code looks at the response to see if the string is empty, and also looks at the previous response to see if the recieved data from the port changed since the last response. You may need to add some delay's into the code to slow the execution time to give some time between poll and read cycles.

Code: Select all

#include <SoftwareSerial.h>

SoftwareSerial SecondSerial(2,3);   // Rx, Tx corresponding to arduino digital pins

int intTime = 2000;         // 2 second delay after device power-up
String response = "0";      // Response from SecondSerial serial port; set to blank string initally incase data not present
String PrevResponse = "0";  // Previous response to save

void setup() 
{
  SecondSerial.begin(9600);
  Serial.begin(9600);
  Serial.println("....Starting....");
  Serial.println();
  delay (intTime);
  
}

    // --------------------   'loop'   --------------------
void loop() 
{
  Poll();
  if(response != "")
  {
    if(response == PrevResponse && response != "0")
    // Check to see if data is old, and or present
    {
      Serial.println("Data not new");
      Serial.println("\tResponse\t" + response);
      Serial.println("\tPrevious Response\t" + PrevResponse);
    }
    else
    // prints data from SecondSerial is data is new and not a blank string, and then flushes the port
    {
      Serial.println("Response Acquired!");
      Serial.print("\tResponse data:\t");
      Serial.println(response);
        Serial.println("\tResponse\t" + response);
        Serial.println("\tPrevious Response\t" + PrevResponse);
      SecondSerial.flush();
    }
  }   

    PrevResponse = response;
    Serial.println("----------- End ------------");
}
    // -------------------- End 'loop' --------------------

void Poll()
{
  SecondSerial.print("$B173\r"); // This is where you send your hex values
  Serial.println();
  Serial.println("SecondSerial Serial port Polled...");
  Serial.println();
  response = SecondSerial.readStringUntil('\r');
    // Reads data from SecondSerial until carriage return is seen (\r)
}
    // -------------------- End 'Poll' --------------------

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”