Arduino connect hmi and instrument by RS-232

Hello
Fisrt,I aplogize about my bad English.
Now I am working to make a automatic system by arduino .
I am already finish to connect HMI(DELTA 107-CV) and the analyze instrument by TTL to RS232 module(max3232),also I can successfully control external hardware to achieve automation by the relay module.
But there is two big promblem .
First,when I connect HMI, my arduino will send some garbled(see in serial port) to the HMI,how can I stop it.(I'm wondering if hmi needs to send something back to the arduino).
Second,I can send command to the instrument ,but I can not get the result send back to arduino.

Thank you all

THIS IS My CODE

#include <SoftwareReset.h>
#include <ModbusRtu.h>
#include <SoftwareSerial.h>///RS-232
/////////////////////////////////////////////////////////////////////////////////////HMI SETTING///////////////////////////////////////////////////////////
Modbus master(2,8,0);
modbus_t telegram;
uint16_t RW[16] ={
  11,22,33,44,55,66,77,88,0,0,0,0,0,0,1,-1
};
uint8_t u8state;       //!< machine state
uint8_t u8query;       //!< pointer to message query
unsigned long u32wait;
///////////////////////////////////////////////////////////////////////////////////INSTRUMENT SETTING///////////////////////////////////////////////////////////////////////////////////////////////
SoftwareSerial mySerial(6,7); ////////instrument pin
const byte STX = 0x02;   //////////////instrument command           
const byte ETX = 0x03;   //////////////instrument command            
Modbus slave(0, mySerial);
int data;
int i; 
int sum = 0;
int w;
void setup()
{
////////////////////////////////////////////////////////////////RS-232///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
master.begin(4800);/////////////////////////////// HMI baud
slave.begin(4800);
Serial.begin(4800,SERIAL_8N1);///////////////////////////// ARDUINO baud
mySerial.begin(4800);////////////////////////// instrument baud
master.setTimeOut( 300000 ); // if there is no answer in 5000 ms, roll over
u32wait = millis() + 1000;
u8state = u8query = 0;
////////////////////////////////////////////////////////////instrument reset command//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
mySerial.write(STX); 
mySerial.write('R');
mySerial.write(ETX);
////////////////////////////////////////////////////////external hardware///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
pinMode(8,OUTPUT);///////////Drain pump
digitalWrite(8,0);
pinMode(9,OUTPUT);///////////Methanol pump
digitalWrite(9,0);
pinMode(10,OUTPUT);//////////SY-03B injection pump
digitalWrite(10,0);
pinMode(13,OUTPUT);//////////Three-way solenoid valve
digitalWrite(11,0);
pinMode(12,OUTPUT);//////////stirring motor
digitalWrite(12,0);
}
void loop() 
{
master.poll(RW,16);
delay(10);
if(mySerial.available()){
Serial.print(mySerial.read())
}
 switch ( u8state ) {
    case 0:
      if (millis() > u32wait) u8state++; // wait state
      break;
    case 1:
      telegram.u8id = 2; // slave address 
      telegram.u8fct = 3; // function code (this one is registers read)
      telegram.u16RegAdd = 0; // start address in slave
      telegram.u16CoilsNo = 6; // number of elements (coils or registers) to read
      telegram.au16reg = RW; // pointer to a memory array in the Arduino
      Serial.print("Query");
      Serial.println(master.query( telegram )); // send query (only once)

      u8state++;
      break;
    case 2:
    
      Serial.print("POLL:");
      Serial.println(master.poll());
    
      if (master.getState() == COM_IDLE) {
        u8state = 0;
        u32wait = millis() + 2000;
      }
      break;
  }
///////////////////////////////////////////////////process //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
while(RW[4]==0){
digitalWrite(8,1);////////////////////Drain pump open
delay(5000); 
digitalWrite(8,0);////////////////////Drain pump close
digitalWrite(13,1);///////////////////Three-way solenoid valve open
digitalWrite(10,1);///////////////////SY-03B injection pump open
delay(15000); 
digitalWrite(10,0);///////////////////SY-03B injection pump close
digitalWrite(13,0);///////////////////Three-way solenoid valve close
digitalWrite(12,1);///////////////////stirring motor open
delay(5000); 
digitalWrite(12,0);///////////////////stirring motor clsoe
digitalWrite(8,1);////////////////////Drain pump open
delay(5000); 
digitalWrite(8,0);////////////////////Drain pump close
digitalWrite(9,1);////////////////////Methanol pump open
delay(5000); 
digitalWrite(9,0);////////////////////Methanol pump close
digitalWrite(12,1);///////////////////stirring motor open
delay(5000); 
digitalWrite(12,0);///////////////////stirring motor clsoe
digitalWrite(8,1);////////////////////Drain pump open
delay(5000); 
digitalWrite(8,0);////////////////////Drain pump close
digitalWrite(9,1);////////////////////Methanol pump open
delay(5000); 
digitalWrite(9,0);////////////////////Methanol pump close
mySerial.write(STX); /////////////////instrument command for Prepare
mySerial.write('B');
mySerial.write(ETX);
digitalWrite(12,1);///////////////////stirring motor open
delay(40000);
mySerial.write(STX);//////////////////instrument command fot inject sample
mySerial.write('A');
mySerial.write(ETX);
digitalWrite(13,1);///////////////////Three-way solenoid valve open
digitalWrite(10,1);///////////////////SY-03B injection pump open
delay(15000);
digitalWrite(10,0);///////////////////SY-03B injection pump close
digitalWrite(13,0);///////////////////Three-way solenoid valve close
delay(2000);
mySerial.write(STX);//////////////////instrument command for analyze
mySerial.write('A');
mySerial.write(ETX);
delay(15000);
softwareReset::simple();
digitalWrite(8,1);////////////////////Drain pump open
delay(5000); 
digitalWrite(8,0);////////////////////Drain pump close

}
////////////////////////////////////////////////////////////////////manual open Drain pump //////////////////////////////////////////////////////////////////////////////////////////////////////////////
if(RW[5]==0){
digitalWrite(8,1);
}
////////////////////////////////////////////////////////////////////manual open Methanol pump////////////////////////////////////////////////////////////////////////////////////////////////////////////
if(RW[7]==0){
digitalWrite(9,1);
}
////////////////////////////////////////////////////////////////////manual open SY-03B injection pump///////////////////////////////////////////////////////////////////////////////////////////////////////////
if(RW[8]==0){
digitalWrite(10,1);
}
////////////////////////////////////////////////////////////////////manual open Three-way solenoid valve///////////////////////////////////////////////////////////////////////////////////////////////////////////
if(RW[9]==0){
digitalWrite(13,1);
}
////////////////////////////////////////////////////////////////////manual open stirring motor///////////////////////////////////////////////////////////////////////////////////////////////////////////
if(RW[6]==0){
digitalWrite(12,1);
}
//////////////////////////////////////////////////////////reset /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
while(RW[2]==0){
sum+=i;
i++;
if(i>3){
delay(10);
softwareReset::simple();
}
delay(10);
}
}

Your topic was MOVED to its current forum category as it is more suitable than the original

THANK YOU

Did you confirm HMI baud rate is 4800? Are you testing with the final (complete) sketch? You should solve this problem with a simpler test code, to eliminate complexity and isolate the issue.

You've made the sketch hard to read and typo error prone by hard coding the pin numbers. You should name them with variables, e.g.

const int CLOSE = 0;
const int OPEN = 1;
const int drainPin = 8;
const int methanolPin = 9;
...
digitalWrite(drainPin, CLOSE);
digitalWrite(methanolPin, OPEN);

Or, "pump" instead of "pin" if you prefer.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.