Arduino serial communication. How to read the device response via rs232?

Hello forum,

this is my first threat on this helpful forum..

I want to send dataprotocols from my Arduino via the serial interface rs232 to the mass flow controller, get response and print it on the serial monitor .

this is an excerpt from the instruction manual

Purpose:

  1. send the protocol as a HEX (array) commands to MFC (Bronkhorst)
  2. receive the response as a String
  3. print the response on serial monitor.

Problem

I converted the protocol to hexa and sent it to MFC, but i don't get the expected response shown on the serial monitor.

#include <SoftwareSerial.h>

SoftwareSerial m_Port(15,14); // (Rx, Tx) on arduino Mega2560

// the whole protocol is :06030401210121\r\n 
String bronkhrostProtocol = ":06030401210121"; // first part of the protocol | in hexadecimal: 3A303630333031303132313345383

int string_length;
int digitalVlaue;
String R;
String maReponse;

void setup() {
  Serial.begin(9600);
  m_Port.begin(38400);
  string_length = bronkhrostProtocol.length();
  while(!Serial) {}
}

void loop() {
Serial.println('\n');  
Serial.print("StringLen is: ");                   // just a scope to show length of the String 
Serial.println(string_length);
Serial.println('\n');

//======= Converting to hexadecimal========
//=======================================================

char char_Array [string_length];
for(int i = 0; i< string_length; i++){
 char_Array[i] = bronkhrostProtocol.charAt(i);   // store the string elements to char array
}
Serial.print("for loop ");                       // just a scope

for(int j=0; j<string_length; j++){
  
  Serial.print(char_Array[j]);                   // scope to show the array elements
  }
  Serial.println('\n');

String hex_array2 = "0D0A";                      // second part of the protocol is allready converted to hexa
char char_Array2[hex_array2.length()];

for(int f = 0; f<hex_array2.length(); f++){      //store the hex elements in array2
  char_Array2[f] = hex_array2.charAt(f);
  }
  
  for(int m = 0; m<16; m++){
   m_Port.print(char_Array[m], HEX);            // send the first part of the protocol to Mass Flow Controller in hexadecimal
  }
  
 for(int l = 0; l<5; l++){
  Serial.print(char_Array2[l], HEX);
  m_Port.print(char_Array2[l]);                // send the second part of the protocol to Mass Flow Controller in hexadecimal
  }
  Serial.println('\n');

  //========= Answer form MFC===================
  //===============expected answer is :06030201213E80\r\n
  //====================================================
R = m_Port.readStringUntil('\n'); // should read the response until '\n'
//maReponse = R.substring(0,10);
Serial.println("Answer is ");
Serial.print(R);
  
  while (true){
  }
  
}

on the serial monitor i got this

Is there any possibility to send the hexa data other than using m_Port.print(char_Array[m], HEX);?

Any thoughts on this issue shall be appreciated.
Thank you!

Did you mean
String hex_array2 = "\r\n"; ?

Why did you make the code so complicated?

yes.. I am still a beginner in coding with arduino. thanks

Let's make it easier then.

Which Arduino board do you have.
Is that a Arduino Mega 2560 ? then please don't use the SoftwareSerial, but use Serial1.

this is a text string (an array of characters):

char outtext[] = ":06030401210121\r\n"

this is how you send it every second

void loop()
{
  Serial1.write( outtext);
  delay( 1000);
}

You could read the response after half a second:

void loop()
{
  Serial1.write( outtext);
  delay( 500);
 
  while( Serial1.available() > 0)
  {
    Serial.write( (byte) Serial1.read());
  }

  delay( 500);
}

I followed the KISS rule.

If you don't get any response, how do you know that your wiring is okay ? How do you know that your message gets to the MFC ?
Can you test the device with a computer ? Do you have a scope or logic analyzer ?

Please tell us what kind of device it is. We want to read the documentation ourselves.

[ADDED] Found the documentation of your screendump. It is 917027-Manual-RS232-interface.pdf from bronkhorst.com.

thanks for your answer!

It is the Arduino Mega2560 and the device is the Bronkhorst MASS-STREAM (D-6321-DR).. The wiring should be correct, because i have alternately wired tx with rx between RS232 and Arduino board..
Here is also the instruction manual of the mass flow controller
917027-Manual-RS232-interface.pdf (1,8 MB)

How do you know that your MFC has node 3 ?
You could try to send a message to all nodes. I think the old fashioned sprintf function is the best to build the string.

char buffer[20];
int length = 6;
int node = 3;
int command = 4;
sprintf( buffer, ":%02d%02d%02d0x210121\r\n", length, node, command);

But before you do that, use my example to make the most simple sketch that is possible and show us that super simple sketch.

This won't work, according to the device manual. It states to use ASCII Hex representation for all data bytes. The transmission is terminated by a single CR or '\r' although I imagine that '\n' might be ignored.

From the manual:

All bytes (except the initial and termination character) are converted from 1 binary byte to 2 hexadecimal bytes in ASCII representation. Example: binary data byte 0x2A --> hexadecimal ASCII characters 0x32, 0x41.

I suggest %X formatting (but do not know what might be intended by the "210121" bit).

sprintf( buffer, ":%02X%02X%02X210121\r", length, node, command);

There is

  • existing RS232-HOST ASCII protocol
  • ProPar enhanced binary protocol
  • Communication messages between ProPar interfaces

They three different things. Now I'm confused :confused:

@m_gh21, your topic has been moved to a more suitabable location on the forum.

Pins 14 and 15 on the Mega are hardware serial ports (Serial3); although it might not solve your problem, I suggest that you get rid of SoftwareSerial and use Serial3 everywhere instead of m_Port.