RS485 - SoftSerial works, hardware doesn't

Hello community.
As in the topic: I use Arduino to communicate with vibration sensor via RS485. I've made UART<>RS485 converter, using MAX485, as below.

of course GND of Arduino, converter and sensor are connected.

When I use SoftwareSerial, everything works great. When I send communication frame to sensor (fonction startpomiaru()) I receive answer from sensor:
:FF00000120
Which is ok. I have tested it on both Arduino Duemilanove and Mega R3 - it works. Code below

int pinControl = 9;
 char kontrol;
 char znak;
 
SoftwareSerial mySerial(10, 11); // RX, TX
  
void startpomiaru(){ // start measure 
  digitalWrite(pinControl,HIGH);
  delay(1);
  mySerial.write(58); //:
  mySerial.print("0101000111");
  mySerial.write(13); //cr
  mySerial.write(10); //lf
  delay(1);
  digitalWrite(pinControl,LOW);
}
  
 
void setup() {  
  pinMode(pinControl, OUTPUT);  
  pinMode(12, OUTPUT);
  mySerial.begin(38400);
  Serial.begin(115200);
  Serial.println("elo elo");
  digitalWrite(pinControl, LOW);
  digitalWrite(12, LOW); // power supply for sensor
}

void loop() {
 
if(Serial.available()){ //read from keyboard
  kontrol = Serial.read();
  //Serial.println("");

  switch(kontrol){
    case 's': // start measure
      startpomiaru();
      break;   
  }
    
}

while(mySerial.available()>0){
    znak = mySerial.read();
    Serial.write(znak);

  }

}

Problem becomes, when I try to use hardware serial - instead Software Serial. Code for this is just the same, only "mySerial" replaced with Serial1.

 int pinControl = 20;
 char kontrol;
 char znak;


//SoftwareSerial mySerial(10, 11); // RX, TX
  
void startpomiaru(){ // start measure 
  digitalWrite(pinControl,HIGH);
  delay(1);
  Serial1.write(58);
  Serial1.print("0101000111");
  Serial1.write(13);
  Serial1.write(10);
  delay(1);
  digitalWrite(pinControl,LOW);
}
  
 
void setup() {  
  pinMode(pinControl, OUTPUT);  
  pinMode(17, OUTPUT);
  Serial.begin(115200);
  Serial.println("elo elo");
  digitalWrite(pinControl, LOW);
  digitalWrite(17, LOW); // power supply for sensor
  Serial1.begin(38400);
}

  

void loop() {
 
if(Serial.available()){ //odczytanie danych z klawerki
  kontrol = Serial.read();
  //Serial.println("");

  switch(kontrol){
    case 's': // start measure
      startpomiaru();
      break;   
  }
    
}

while(Serial1.available()){
    znak = Serial1.read();
    Serial.write(znak);

  }

}

I receive something like this:ÜÃÏÏÏ

I have tried for all serial ports (1,2,3) with the same result.
Moreover i have to use hardware serial, because SoftSerial can't keep up with the pace, when sensor sends big amount of measurement data - it receives only the size of buffer.

I have tested all serial ports in my mega - they all works
Thanks in advance for any ideas.

I suspect the problem is that your are turning off the transmit enable signal before you know if all the characters have been transmitted. Keep in mind that hardware serial transmit is interrupt driven meaning that your print statements are sent very fast to the software buffers but that doesn't mean they have been all read from the buffer and actually sent out the hardware uart. If your sending message length is always nearly the same I suspect you can just adjust the delay() statement until it is long enough to make sure your complete message has been sent out, but not to long as to prevent receiving a response from the sensor if in fact it will be responding to your sent message. Does that make sense?

//SoftwareSerial mySerial(10, 11); // RX, TX
  
void startpomiaru(){ // start measure 
  digitalWrite(pinControl,HIGH);
  delay(1);
  Serial1.write(58);
  Serial1.print("0101000111");
  Serial1.write(13);
  Serial1.write(10);
  delay(1);                             // increase this value until complete message is sent out.
  digitalWrite(pinControl,LOW);
}

retrolefty:
Does that make sense?

Yes, very. And what is more important - it works!

Thank you so much for help :slight_smile:

Another potentially more stable solution would be to use the Serial.flush() method. According to the docs this will sleep the Arduino until all Serial data is finished being sent.

Please let the dead rest in peace.