Estoy tratando de implementar modbus a un arduino uno. Utilizo un MAX485 conectado asi
http://dereenigne.org/wp-content/uploads/modbus.png. Y varios ejemplos basados en el ejemplo de jpzometa
Estoy probando con programas como el "Modscan" o "Modbus Poll" pero no consigo comunicar con el arduino, o no me da nada o me devuelve un "0". Pero no tengo claro si esto es realmente una respuesta del arduino o es un error de comunicaciones.
Me surgen varias dudas:
- Alimento el MAX485 a traves del Arduino no creo que esto sea un problema por el bajo consumo. Podria causar algún problema?
- Puedo poner algunos leds para ver si comunica el arduino? Deberian encenderse los leds TX-RX de placa? Por que no lo hacen.
Alguna idea?
Pongo el codigo:
#include <SimpleModbusSlave.h>
#define ledPin 13 // onboard led
#define buttonPin 7 // push button
/* This example code has 9 holding registers. 6 analogue inputs, 1 button, 1 digital output
and 1 register to indicate errors encountered since started.
Function 5 (write single coil) is not implemented so I'm using a whole register
and function 16 to set the onboard Led on the Atmega328P.
The modbus_update() method updates the holdingRegs register array and checks communication.
*/
// Using the enum instruction allows for an easy method for adding and
// removing registers. Doing it this way saves you #defining the size
// of your slaves register array each time you want to add more registers
// and at a glimpse informs you of your slaves register layout.
//////////////// registers of your slave ///////////////////
enum
{
// just add or remove registers and your good to go...
// The first register starts at address 0
ADC0,
ADC1,
ADC2,
ADC3,
ADC4,
ADC5,
LED_STATE,
BUTTON_STATE,
TOTAL_ERRORS,
// leave this one
TOTAL_REGS_SIZE // total number of registers for function 3, 4 and 16 share the same register array
};
unsigned int holdingRegs[TOTAL_REGS_SIZE]; // function 3, 4 and 16 register array
////////////////////////////////////////////////////////////
void setup()
{
// parameters(long baudrate, byte ID, byte transmit enable pin, unsigned int holding registers size)
// the transmit enable pin is used in half duplex communication to activate a MAX485 or similar
// to deactivate this mode use any value < 2 because 0 & 1 is reserved for Rx & Tx
modbus_configure(9600, 1, 2, TOTAL_REGS_SIZE);
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop()
{
// modbus_update() is the only method used in loop(). It returns the total error
// count since the slave started. You don't have to use it but it's useful
// for fault finding by the modbus master.
holdingRegs[TOTAL_ERRORS] = modbus_update(holdingRegs);
for (byte i = 0; i < 6; i++)
{
holdingRegs[i] = analogRead(i);
delayMicroseconds(500);
}
byte buttonState = digitalRead(buttonPin); // read button states
// assign the buttonState value to the holding register
holdingRegs[BUTTON_STATE] = buttonState;
// read the LED_STATE register value and set the onboard LED high or low with function 16
byte ledState = holdingRegs[LED_STATE];
if (ledState) // set led
digitalWrite(ledPin, HIGH);
if (ledState == 0 || buttonState) // reset led
{
digitalWrite(ledPin, LOW);
holdingRegs[LED_STATE] = 0;
}
}