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 .
send the protocol as a HEX (array) commands to MFC (Bronkhorst)
receive the response as a String
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){
}
}
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.
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).
@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.