I'm doing a RS485 serial communication via a MAX3080.
Arduino is the master and the slave is a pic.
The software does the following:
1)Arduino sends a code to the PIC
2) PIC receives the code and sends a data to arduino
3) Arduino show in monitor serial received data
The problem:
The command if (RS485.available ()> 0) indicates whether data received and I always said no data received and shows nothing on screen. Is that the code has arrived safely at that pic when you get the pic code switch on an LED that has received.
/*
Software Serial
- Rx Pin 10 Arduino
- Tx Pin 11 Arduino
- Control de transmision y recepcion Pin 3 Arduino
*/
#include <SoftwareSerial.h> //Librerias necesarias para crear una comunicacion serial en otros pines
/* DEFINICIONES DE PIN EN ARDUINO */
#define RX 10 // Pin de Recepcion Puerto serie
#define TX 11 // Pin de Transmision Puerto serie
#define CTR 3 // Control de direccion
#define LEDRX 4
#define LEDTX 5
#define LEDCTR 13
#define RS485TX HIGH
#define RS485RX LOW
SoftwareSerial RS485(RX, TX); // Declaracion Puerto serie
/* INICIALIZACIONES */
void setup()
{
Serial.begin(9600); // Comunicacion serie al monitor
RS485.begin(9600); // set the data rate
pinMode(LEDRX, OUTPUT); // Led RX puesto en salida
pinMode(LEDTX, OUTPUT); // Led TX puesto en salida
pinMode(LEDCTR, OUTPUT); // Led TX puesto en salida
pinMode(CTR, OUTPUT); // Control de direccion como salida
Serial.println("Esperando respuesta"); // Mensaje al monitor serie
}// Finalizacion de inicializaciones
void loop() // Programa en ejecucion
{
leds(), // Leds Inicialmente Apagados
transm(); // Transmite al esclavo
receiv(); // Recibe del esclavo
}//--(end main loop )---
/*-----( Declare User-written Functions )-----*/
void leds()
{
digitalWrite(LEDRX, LOW); // Led RX Apagado
digitalWrite(LEDTX, LOW); // Led TX Apagado
digitalWrite(LEDCTR, LOW); // Led CTR Apagado
Serial.println();
}
void transm ()
{
digitalWrite(CTR, RS485TX);// Modo Transmitiendo
digitalWrite(LEDTX, HIGH); // Enciendo y apago led
delay(500);
digitalWrite(LEDTX, LOW);
delay(500);
if (digitalRead(CTR == HIGH))
{
digitalWrite(LEDCTR, HIGH);
delay(500);
Serial.println("Transmitiendo dato ..."); // Comprobar que el pin CTR funciona bien y escriba por pantalla un mensaje
}
RS485.write("$OW8&"); //Codigo que empiezo la comunicacion con el pic
Serial.println("Envio $OW8&.... "); // Muestro por pantalla lo que envio a traves del puerto serie
Serial.println();
}
void receiv ()
{
digitalWrite(CTR, RS485RX);// Modo recibiendo datos
RS485.listen(); // Escuchando el puerto RS485
if (RS485.available()>0)
{
if (digitalRead(CTR == LOW))
{
digitalWrite(LEDCTR, LOW);
delay(500);
Serial.println("Recibiendo dato ..."); //Comprobar que el pin CTR funciona bien y escriba por pantalla un mensaje
}
digitalWrite(LEDRX, HIGH); //Enciendo y apago led
delay(500);
digitalWrite(LEDRX, LOW);
delay(500);
Serial.write(RS485.read()); // Envio al monitor serial lo que recibo del PIC
Serial.println();
}
}
//*********( THE END )***********