Send and Receive using same RS485 Shield

Hi,

In my project, I have connected the arduino and RS485 in the following way: Arduino Uno>>RS485Shield>>USB to RS485 Converter>>PC.
I would like to know if we can program arduino in such a way that the RS485 will send and receive using the same shield at different times.

Regards,
JAM

RS485 is bidirectional so why not.
But first you must tell us which protocol are you running on RS485. Is it Modbus?

I am not using any protocol. I am just using the RS485 serial communication program for sending and receiving separately. 
Code for Sender:
[code][int EN = 2;

void setup() /****** SETUP: RUNS ONCE ******/
{
  pinMode(EN, OUTPUT );
  Serial.begin (19200);
}//--(end setup )---

void loop()    /****** LOOP: RUNS CONSTANTLY ******/
{
  // Send Data
  digitalWrite(EN, HIGH ); // enable send
  Serial.print ( 'A' );
  delay(1000);
  Serial.print ( 'B' );
  delay (1000);
}]

Code for Receiver:

[int ledPin=13;
int EN = 2;
int Val;

void setup()/****** SETUP: RUNS ONCE ******/
{
  pinMode(ledPin, OUTPUT );
  pinMode(EN, OUTPUT );
  Serial.begin (19200);
}//--(end setup )---

void loop()  /****** LOOP: RUNS CONSTANTLY ******/
{
  // receive Data
  digitalWrite (EN, LOW ); // enable receive
  Val = Serial.read ();
  if (-1 != Val) 
  {
    if ( 'A' == Val) 
    {
      digitalWrite (ledPin, HIGH );
      delay (500);
      digitalWrite (ledPin, LOW );
      delay (500);
    }
  }]

I am trying to enable sending and receiving on the same code by modifying in this way:
if(EN=HIGH)
{Sender code)}
else{Receiver Code}.

But It doesnt work this way i think.[/code]

First answer us a question. Is your device going to be a slave or a master? Or none of them?

If it is a slave, EN must be low all the time until you send from arduino to pc, then you change EN high, send message, and change low again. This way you put your arduino to listening mode unless you talk.

I recommend using Modbus there is a library for Arduino.

None of them actually..

If I intend use Modbus protocol, I could set the arduino as the master and the PC as the slave. Is this possible? Do I have to set up another application on the PC to act as slave?

Yes, you would need a Modbus slave emulator running on pc. You can also do the opposite. Pc as a master and arduino as a slave. There are lots of modbus master and slave emulators for pc.

Before introducing Modbus you should read about modbus protocol or you will have a hard time understanding it.

Thank you for that information.I am going through the modbus library. I have got the PC>>RS485converter>>Rs485Shield>>Arduino UNO connection working in both ways now.

#include <RS485.h>
#include <SoftwareSerial.h>
#include <Bridge.h>

char Message[maxMsgLen + 1] ;
int ENABLEPIN = 4;
int LED_PIN = 13;
int text;

void setup()
{
  pinMode(ENABLEPIN,INPUT); 
  digitalWrite (ENABLEPIN,LOW);
  pinMode(LED_PIN,OUTPUT); 
  Serial.begin(9600);
   Bridge.begin();
  Serial.println("Tx/RX");
  //digitalWrite (RX_PIN,LOW); 

  RS485_Begin(28800);
}

void loop()
{

  int STATE=  digitalRead(ENABLEPIN);
  delay(1000);

  if(STATE == 1)
  {
    Serial.println("Transmitter");
    int analog0 = analogRead(A0);
    float temperature =  analog0 * 0.48828125;
    if (RS485_SendMessage(Message, fWrite, ENABLE_PIN)) // Send data to PC
    {
      Serial.print("Sending-");
      Serial.print("Temperature:");
      Serial.print(temperature);
      Serial.print("*C");
      Serial.println();
      digitalWrite(LED_PIN, LOW);
      delay(1000);
    }
  }
  else if (STATE == 0)
  {  
    Serial.println("Receiver");
   
    text= Serial.read();

    if (-1 != text)
    {
      for(int i=0;i<3;i++)

      {
        if ('H' == text ){
          digitalWrite(LED_PIN, HIGH);
          Serial.println("led HIGH");
          delay(1000);
          // break;
        }
        else if ('L' == text )
        {
          digitalWrite(LED_PIN, LOW);
          Serial.println("led LOW ");
          delay(1000);
          //break;
        }
      }
    }
  }
}
// }

In the future,I would modifying the code to transfer data through Ethernet. I am now trying to use Ardinuo Yun instead of UNO but I do not get the same output. Is there any way I could modify this to be used on Yun?

Hi Jam311,

at first sorry for my bad english. Which Shield do you use? This one? https://www.sparkfun.com/products/12965

If you did, could you tell me how to use the enable pin? You chose D4 for it. But this Pin isn´t connected with /RE and DE if you look to the Shematic (https://www.sparkfun.com/products/12965) of the Shield. And Rx is on Pin D0 and Tx on D1 by standard.

Thank you!