Problem communicating between arduino and RPi via RS485 bus.

shreyagrawal_28:
Hi. I am trying to communicate between Arduino and RPi via RS485 bus. But when I try to read data from arduino, nothing is received on the RPi side, it just keeps printing blank spaces.
I have tried with low baudrate as suggested in some posts.
I am using arduino pro mini (Atmega 328).

Here is the arduino code.

void setup () {

pinMode(3, OUTPUT);                 //RS485 pin
 delay(50);
 digitalWrite(3, LOW);               //Enabling in receive mode
 Serial.begin(4800);    
 delay(100);
}

void loop() {
if (Serial.available())
{
digitalWrite(3, HIGH);               //Enabling transmit
delay(5);
Serial.println('hello');
delay(20);
digitalWrite(3, LOW);                //Disabling transmit
delay(5);
}
}

Instead of your fixed delays on the write use:

unsigned long deadtime=0;
void loop(){
if(Serial.availabe()){
  deadTime = millis();  // require 50ms of inactivity on the bus before switching to TX
  char ch=Serial.read(); // do something with the input
  //
  }
else {
  if(millis()-deadtime>50){ // serial bus has been inactive for over 50 ms
    digitalWrite(3, HIGH);               //Enabling transmit 
    Serial.println('hello'); 
    Serial.flush();  // make sure all data has actually exited the TX pin before switching to RX
    digitalWrite(3, LOW);                //Disabling transmit back into RX mode
    deadTime = millis(); // restart dead counter
    }
}

It also look like your Arduino code was waiting for a byte from Serial() before it would send?

Chuck.