Hola si disculpame, el codigo en general son banderas que se activan con if dependiendo de las entradas, lo actualiza una funcion.
Tengo mas pruebas con codigo completo.
El primer codigo no funciona. cuando excede los intentos de comunicacion sin apretar el boton, y uno apreta el boton de la freq (pin8) la frecuencia se actualiza. si apretas el pin 9 no sucede nada.
#include "SimpleModbusMaster.h"
/*
The example will use packet1 to read a register from address 0 (the adc ch0 value)
from the arduino slave (id=1). It will then use this value to adjust the brightness
of an led on pin 9 using PWM.
It will then use packet2 to write a register (its own adc ch0 value) to address 1
on the arduino slave (id=1) adjusting the brightness of an led on pin 9 using PWM.
*/
//////////////////// Port information ///////////////////
#define baud 9600
#define timeout 1000
#define polling 900 // the scan rate
#define retry_count 5
// used to toggle the receive/transmit pin on the driver
#define TxEnablePin 10
// The total amount of available memory on the master to store data
#define TOTAL_NO_OF_REGISTERS 5
// This is the easiest way to create new packets
// Add as many as you want. TOTAL_NO_OF_PACKETS
// is automatically updated.
enum
{
PACKET1,
PACKET2,
TOTAL_NO_OF_PACKETS // leave this last entry
};
// Create an array of Packets to be configured
Packet packets[TOTAL_NO_OF_PACKETS];
// Masters register array
unsigned int regs[TOTAL_NO_OF_REGISTERS];
void setup()
{
// Initialize each packet
modbus_construct(&packets[PACKET1], 2, PRESET_SINGLE_REGISTER, 269, 0, 0);
modbus_construct(&packets[PACKET2], 2, PRESET_SINGLE_REGISTER, 8192, 0, 1);
// Initialize the Modbus Finite State Machine
modbus_configure(&Serial, baud, SERIAL_8N2, timeout, polling, retry_count, TxEnablePin, packets, TOTAL_NO_OF_PACKETS, regs);
pinMode(9,INPUT_PULLUP);
pinMode(8,INPUT_PULLUP);
pinMode(10, OUTPUT);
}
void loop()
{
modbus_update();
if (digitalRead(8)==0) {
packets->connection=1;
regs[0]=2000; //PACKET1, direccion del variador 269, establecer frecuencia a 20.00Hz
}
if(digitalRead(9)==0){
packets->connection=1;
regs[1]=1; //PACKET2, direccion del variador 8192, RUN
}
}
El siguiente codigo si funciona independientemente del tiempo, pero en este intento arduino esta siempre escribiendo el regs[0], por lo que entiendo que no llega nunca a no haber conexion y no cae a cero la variable connection de la libreria.. no se si me explico bien..
#include "SimpleModbusMaster.h"
/*
The example will use packet1 to read a register from address 0 (the adc ch0 value)
from the arduino slave (id=1). It will then use this value to adjust the brightness
of an led on pin 9 using PWM.
It will then use packet2 to write a register (its own adc ch0 value) to address 1
on the arduino slave (id=1) adjusting the brightness of an led on pin 9 using PWM.
*/
//////////////////// Port information ///////////////////
#define baud 9600
#define timeout 1000
#define polling 900 // the scan rate
#define retry_count 5
// used to toggle the receive/transmit pin on the driver
#define TxEnablePin 10
// The total amount of available memory on the master to store data
#define TOTAL_NO_OF_REGISTERS 5
// This is the easiest way to create new packets
// Add as many as you want. TOTAL_NO_OF_PACKETS
// is automatically updated.
enum
{
PACKET1,
PACKET2,
TOTAL_NO_OF_PACKETS // leave this last entry
};
// Create an array of Packets to be configured
Packet packets[TOTAL_NO_OF_PACKETS];
// Masters register array
unsigned int regs[TOTAL_NO_OF_REGISTERS];
void setup()
{
// Initialize each packet
modbus_construct(&packets[PACKET1], 2, PRESET_SINGLE_REGISTER, 269, 0, 0);
modbus_construct(&packets[PACKET2], 2, PRESET_SINGLE_REGISTER, 8192, 0, 1);
// Initialize the Modbus Finite State Machine
modbus_configure(&Serial, baud, SERIAL_8N2, timeout, polling, retry_count, TxEnablePin, packets, TOTAL_NO_OF_PACKETS, regs);
pinMode(9,INPUT_PULLUP);
pinMode(10, OUTPUT);
}
void loop()
{
modbus_update();
regs[0]=2000; //PACKET1, direccion del variador 269, establecer frecuencia a 20.00Hz
if(digitalRead(9)==0){
regs[1]=1;//PACKET2, direccion del variador 8192, RUN
}
if (digitalRead(9)==1){
regs[1]=4; //PACKET2, direccion del variador 8192, valor 4, STOP
}
}
La definicion de la matriz no la tengo del todo clara, estoy investigando en la libreria pero empieza en : "// Masters register array
unsigned int regs[TOTAL_NO_OF_REGISTERS];"
gracias!