RS232 with arduino uno

Hi everyone hope u are fine.
i connect my arduino uno with an rs232 to ttl converter than with my energy meter, and i want to know the response of a frame(FE FE FE FE 68 99 99 99 99 99 99 68 23 0A 60 00 34 12 78 56 BC 9A F0 DE 2B 16) so i do this code but the result is just 000000000
considering that this frame has a frame response in the datasheet of this energy meter

#include<SoftwareSerial.h>

//SoftwareSerial pins 
#define pinTX 2
#define pinRX 4

byte data[47];

SoftwareSerial RS232(pinRX, pinTX);
static byte trame1[]     = {0xFE,0xFE,0xFE,0xFE,0x68,0x99,0x99,0x99,0x99,0x99,0x99,0x68,0x23,0x0A,0x60,0x00,0x34,0x12,0x78,0x56,0xBC,0x9A,0xF0,0xDE,0x2B,0x16};
// FE FE FE FE 68 99 99 99 99 99 99 68 23 0A 60 00 34 12 78 56 BC 9A F0 DE 2B 16     


void setup() {
    RS232.begin(9600);
    Serial.begin(9600);
    Serial.println("Starting");
}
 
void loop() {
  
  for(int i=0;i<26;i++){
    RS232.write(trame1[i]);
  } 

  while (RS232.available()){
      RS232.readBytes(data, 18);
     }
    delay(1000);

   for (int i=0;i<47;i++){
    Serial.print(data[i]) ;
      }
  Serial.print("\n") ;
      delay(5000);  
      
}

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

do you have an oscilloscope to look at the signals on the RS232 serial lines?
check the Rx and Tx signal levels - should be around -12volts when idle rising to +12V when signals are present - if you don't have a oscilloscope a voltmeter would give you some idea if levels are correct
you should see your Tx data and if there is any response

Post a link to the User Manual of your energy meter.

i think you didn t inderstood me,
when i send this frame via an rs232 cable with my laptop i can get the response with docklight like (FE FE FE FE 68 99 99 99 99 99 99 68 23 02 60 80 6B 16) but now i want to get the response via arduino so i did this connection

with the mentioned code but i didnt get any response

while (RS232.available()){
    RS232.readBytes(data, 18);
}

As soon as one char is available, you try to read 18, it seems.

You have to wait on each character.

Try printing each as it comes in after waiting for it to be available.

a7

still any response

the meter interface is RS232 - how do you interface the Arduino to the RS232?
post a circuit?

Post the code you used to try that. The entire code that compiles, uploads and fails.

And any observations or output that it produced.

a7

#include<SoftwareSerial.h>

//SoftwareSerial pins 
#define pinTX 2
#define pinRX 4

byte data[47];

SoftwareSerial RS232(pinRX, pinTX);
static byte trame1[]     = {0xFE,0xFE,0xFE,0xFE,0x68,0x99,0x99,0x99,0x99,0x99,0x99,0x68,0x23,0x0A,0x60,0x00,0x34,0x12,0x78,0x56,0xBC,0x9A,0xF0,0xDE,0x2B,0x16};
// FE FE FE FE 68 99 99 99 99 99 99 68 23 0A 60 00 34 12 78 56 BC 9A F0 DE 2B 16     


void setup() {
    RS232.begin(9600);
    Serial.begin(9600);
    Serial.println("Starting");
}
 
void loop() {
  
  for(int i=0;i<26;i++){
    RS232.write(trame1[i]);
  } 

  while (RS232.available()){
      RS232.readBytes(data, 18);
     }
    delay(1000);

   for (int i=0;i<47;i++){
    Serial.print(data[i]) ;
      }
  Serial.print("\n") ;
      delay(5000);  
      
}

output is:

starting:
0000000000000000000
0000000000000000000
0000000000000000000
.......

Usually, I do in the following way with an understanding that follows and the trick works.

byte n = Serial.available();
if( n != 0)
{
     Serial.readBtes(myData, 18);
}

It checks that there is atleast one data byte in the Serial Buffer. After that, the control enters into the execution of readBytes() which is a blocking code. The code line keeps reading characters from the Serial Buffer as they arrive and makes an exit when 18 charcaters/data bytes are read or timeout (default 1-sec) occurs.

Therefore, the following codes of the OP are equivalent to my code and should work also:

while (RS232.available())
{
   RS232.readBytes(data, 18);
}

Firstly as pointed out you are not checking that 18 characters are available before reading them - not going to work reliably (though readBytes() uses timed-out reads so it may appear to work sometimes.

Secondly readBytes() returns the actual number of bytes read and you are ignoring the result - always check it.

Thirdly you risk loop reading repeatedly reading without using the results. There is no reason to use a while loop here.

I think what you actually need is more like:

  if (RS232.available () >= 18)  // guard the read by ensuring it will complete
  {
    int bytes_read = RS232.readBytes (data, 18) ;
    if (bytes_read == 18)   // make doubly sure
    {
       ... do something with the data ...
    }
  }

post #6 recommended you wait for each character, e.g. something along the lines of

  for(int i=0;i<18;i++) {
    while(!Serial.available()) ;  //wait for a byte
    data[i]=RS232.read();         // read byte
  }

@marwan-baza
Are you using the following RS232 Converter Module? If yes, check it first with a loop back connection.
rs232ModuleUno

yes i m using this RS232 Converter Module

Can loop back testing work with software serial ports?

Asking, srsly do not know but it was a suspect in a recent problem. we did not run that part to ground, so.

Added: OK, googled it and see respectable opinions suggesting it is not possible.

a7

1. In my mind, the setup of Post-14 worked theoretically with the following codes; but, practically it had not worked.

#include<SoftwareSerial.h>
SoftwareSerial SUART(2, 3);  //SRX = 2, STX = 3
char myData[20] = "";

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

void loop()
{
  SUART.print("BUBT");
  while(SUART.available())
  {
    SUART.readBytes(myData, 5);
    Serial.println(myData);
  } 
  delay(2000);
}

2. Now, I am suggesting the following working setup to check the functionalities of RS232 Converter Modules.

it may be simpler to move to an Arduino Mega (if available) which has hardware serial ports
I try to avoid doing serious work using SoftwareSerial or similar bitbash code - recently had to use bitbash I2C when hardware designer misconfigured pins on a PIC24 !

Excellent suggestion. The following setup with MEGA works; but, I am not happy with output message in the Serial Monitor as it shows an unsyncronized message.

char myData[10] = "";

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

void loop()
{
  Serial3.print("Arduino");
  byte n = Serial3.available();
  if(n != 0)
  {
    Serial3.readBytes(myData, 10);
    Serial.println(myData);
    memset(myData, 0, 10);
  }
  delay(1000);
}