Hola! Tengo una duda sobre el funcionamiento de éste módulo, ya que viene con un conector db9 y con un bloque para las salidas A/B. Cuando se usa la función send de la librería RS485, los datos se envían a ambos conectores (tanto al db9 como al bloque con salidas A/B)? Es que he estado mirando vuestro tutorial, pero sólo usais el bloque A/B. No usais el conector db9.
Estoy tratando de comunicar dos Arduino a través del módulo RS485 y dos cables macho-hembra con conectores db9 (usando un conversor macho-macho para unir ambos cables).
He programado el emisor para que envíe un caracter "A" y el receptor para que lo lea y lo imprima por el monitor serie pero no funciona.
Éste es el código del transmisor:
Code:
#include <Wire.h>
// Cooking API libraries
#include <arduinoUtils.h>
// Include always this library when you are using the RS-485 functions
#include <arduinoRS485.h>
//Include the SPI library
#include <SPI.h>
// Create an instance
RS485 myDevice = RS485();
void setup() {
// Using the SOCKET1
Utils.setONSocket1();
Utils.setMUXSocket1();
// Power on the USB for viewing data in the serial monitor
Serial.begin(115200);
delay(100);
// Initializes the module and assigns the SPI
if ( myDevice.begin() == 0) {
Serial.println("RS-485 module started successfully");
} else {
Serial.println("RS-485 did not initialize correctly");
}
delay(100);
// Configure the baud rate of the module
myDevice.baudRateConfig(9600);
// Configure the parity bit as disabled
myDevice.parityBit(DISABLE);
// Use one stop bit configuration
myDevice.stopBitConfig(1);
// Print hello message
Serial.println("Hello this is RS-485 communication send data example.");
}
void loop() {
myDevice.send("A");
Serial.println("enviado");
delay(1000);
}
Y el receptor:
Code:
#include <Wire.h>
// Cooking API libraries
#include <arduinoUtils.h>
// Include always this library when you are using the RS-485 functions
#include <arduinoRS485.h>
// Include the SPI library
#include <SPI.h>
// Create an instance
RS485 myDevice = RS485();
void setup() {
// Using the SOCKET1
Utils.setONSocket1();
Utils.setMUXSocket1();
// Initializes the serial for viewing data in the serial monitor
Serial.begin(115200);
delay(100);
// Initializes the module and assigns the SPI
// Only allowed in SOCKET1
if ( myDevice.begin() == 0) {
Serial.println("RS-485 module started successfully");
} else {
Serial.println("RS-485 did not initialize correctly");
}
delay(100);
// Configure the baud rate of the module
myDevice.baudRateConfig(9600);
// Configure the parity bit as disabled
myDevice.parityBit(DISABLE);
// Use one stop bit configuration
myDevice.stopBitConfig(1);
// Print hello message
Serial.println("This is RS-485 communication receive data example.");
}
void loop() {
// If data in response buffer
if (myDevice.available())
{
while (myDevice.available()) {
// Read one byte from the buffer
char data = myDevice.read();
// Print data received in the serial monitor
Serial.println("leido");
Serial.print(data);
}
}
delay(1000);
}
Básicamente el problema es que sólo se cumple una vez la condición if(myDevice.available()) al principio del programa. Cuando debería de cumplirse siempre, ya que el emisor está programado para enviar continuamente cada segundo (y lo hace, ya que lo he monitorizado).
Perdón por el post tan largo y gracias de antemano.
Un saludo!