Добавлены библиотеки
This commit is contained in:
+35
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* Modbus slave example 3:
|
||||
* The purpose of this example is to link a data array
|
||||
* from the Arduino to an external device through RS485.
|
||||
*
|
||||
* Recommended Modbus Master: QModbus
|
||||
* http://qmodbus.sourceforge.net/
|
||||
*/
|
||||
|
||||
#include <ModbusRtu.h>
|
||||
|
||||
// assign the Arduino pin that must be connected to RE-DE RS485 transceiver
|
||||
#define TXEN 4
|
||||
|
||||
// data array for modbus network sharing
|
||||
uint16_t au16data[16] = {
|
||||
3, 1415, 9265, 4, 2, 7182, 28182, 8, 0, 0, 0, 0, 0, 0, 1, -1 };
|
||||
|
||||
/**
|
||||
* Modbus object declaration
|
||||
* u8id : node id = 0 for master, = 1..247 for slave
|
||||
* port : serial port
|
||||
* u8txenpin : 0 for RS-232 and USB-FTDI
|
||||
* or any pin number > 1 for RS-485
|
||||
*/
|
||||
Modbus slave(1,Serial,TXEN); // this is slave @1 and RS-485
|
||||
|
||||
void setup() {
|
||||
Serial.begin( 19200 ); // baud-rate at 19200
|
||||
slave.start();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
slave.poll( au16data, 16 );
|
||||
}
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
/**
|
||||
* Modbus master example 2:
|
||||
* The purpose of this example is to query several sets of data
|
||||
* from an external Modbus slave device.
|
||||
* The link media can be USB or RS232.
|
||||
*
|
||||
* Recommended Modbus slave:
|
||||
* diagslave http://www.modbusdriver.com/diagslave.html
|
||||
*
|
||||
* In a Linux box, run
|
||||
* "./diagslave /dev/ttyUSB0 -b 19200 -d 8 -s 1 -p none -m rtu -a 1"
|
||||
* This is:
|
||||
* serial port /dev/ttyUSB0 at 19200 baud 8N1
|
||||
* RTU mode and address @1
|
||||
*/
|
||||
|
||||
#include <ModbusRtu.h>
|
||||
|
||||
uint16_t au16data[16]; //!< data array for modbus network sharing
|
||||
uint8_t u8state; //!< machine state
|
||||
uint8_t u8query; //!< pointer to message query
|
||||
|
||||
/**
|
||||
* Modbus object declaration
|
||||
* u8id : node id = 0 for master, = 1..247 for slave
|
||||
* port : serial port
|
||||
* u8txenpin : 0 for RS-232 and USB-FTDI
|
||||
* or any pin number > 1 for RS-485
|
||||
*/
|
||||
Modbus master(0,Serial,0); // this is master and RS-232 or USB-FTDI
|
||||
|
||||
/**
|
||||
* This is an structe which contains a query to an slave device
|
||||
*/
|
||||
modbus_t telegram[2];
|
||||
|
||||
unsigned long u32wait;
|
||||
|
||||
void setup() {
|
||||
// telegram 0: read registers
|
||||
telegram[0].u8id = 1; // slave address
|
||||
telegram[0].u8fct = 3; // function code (this one is registers read)
|
||||
telegram[0].u16RegAdd = 0; // start address in slave
|
||||
telegram[0].u16CoilsNo = 4; // number of elements (coils or registers) to read
|
||||
telegram[0].au16reg = au16data; // pointer to a memory array in the Arduino
|
||||
|
||||
// telegram 1: write a single register
|
||||
telegram[1].u8id = 1; // slave address
|
||||
telegram[1].u8fct = 6; // function code (this one is write a single register)
|
||||
telegram[1].u16RegAdd = 4; // start address in slave
|
||||
telegram[1].u16CoilsNo = 1; // number of elements (coils or registers) to read
|
||||
telegram[1].au16reg = au16data+4; // pointer to a memory array in the Arduino
|
||||
|
||||
Serial.begin( 19200 ); // baud-rate at 19200
|
||||
master.start();
|
||||
master.setTimeOut( 5000 ); // if there is no answer in 5000 ms, roll over
|
||||
u32wait = millis() + 1000;
|
||||
u8state = u8query = 0;
|
||||
}
|
||||
|
||||
void loop() {
|
||||
switch( u8state ) {
|
||||
case 0:
|
||||
if (millis() > u32wait) u8state++; // wait state
|
||||
break;
|
||||
case 1:
|
||||
master.query( telegram[u8query] ); // send query (only once)
|
||||
u8state++;
|
||||
u8query++;
|
||||
if (u8query > 2) u8query = 0;
|
||||
break;
|
||||
case 2:
|
||||
master.poll(); // check incoming messages
|
||||
if (master.getState() == COM_IDLE) {
|
||||
u8state = 0;
|
||||
u32wait = millis() + 1000;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
au16data[4] = analogRead( 0 );
|
||||
|
||||
}
|
||||
|
||||
+129
@@ -0,0 +1,129 @@
|
||||
/**
|
||||
* Modbus slave example 2:
|
||||
* The purpose of this example is to link the Arduino digital and analog
|
||||
* pins to an external device.
|
||||
*
|
||||
* Recommended Modbus Master: QModbus
|
||||
* http://qmodbus.sourceforge.net/
|
||||
*
|
||||
* Editado al español por LuxARTS
|
||||
*/
|
||||
|
||||
//Incluye la librería del protocolo Modbus
|
||||
#include <ModbusRtu.h>
|
||||
#define ID 1
|
||||
|
||||
//Crear instancia
|
||||
Modbus slave(ID, Serial, 0); //ID del nodo. 0 para el master, 1-247 para esclavo
|
||||
//Puerto serie (0 = TX: 1 - RX: 0)
|
||||
//Protocolo serie. 0 para RS-232 + USB (default), cualquier pin mayor a 1 para RS-485
|
||||
boolean led;
|
||||
int8_t state = 0;
|
||||
unsigned long tempus;
|
||||
|
||||
uint16_t au16data[9]; //La tabla de registros que se desea compartir por la red
|
||||
|
||||
/*********************************************************
|
||||
Configuración del programa
|
||||
*********************************************************/
|
||||
void setup() {
|
||||
io_setup(); //configura las entradas y salidas
|
||||
|
||||
Serial.begin(19200); //Abre la comunicación como esclavo
|
||||
slave.start();
|
||||
tempus = millis() + 100; //Guarda el tiempo actual + 100ms
|
||||
digitalWrite(13, HIGH ); //Prende el led del pin 13 (el de la placa)
|
||||
}
|
||||
|
||||
/*********************************************************
|
||||
Inicio del programa
|
||||
*********************************************************/
|
||||
void loop() {
|
||||
//Comprueba el buffer de entrada
|
||||
state = slave.poll( au16data, 9 ); //Parámetros: Tabla de registros para el intercambio de info
|
||||
// Tamaño de la tabla de registros
|
||||
//Devuelve 0 si no hay pedido de datos
|
||||
//Devuelve 1 al 4 si hubo error de comunicación
|
||||
//Devuelve mas de 4 si se procesó correctamente el pedido
|
||||
|
||||
if (state > 4) { //Si es mayor a 4 = el pedido fué correcto
|
||||
tempus = millis() + 50; //Tiempo actual + 50ms
|
||||
digitalWrite(13, HIGH);//Prende el led
|
||||
}
|
||||
if (millis() > tempus) digitalWrite(13, LOW );//Apaga el led 50ms después
|
||||
|
||||
//Actualiza los pines de Arduino con la tabla de Modbus
|
||||
io_poll();
|
||||
}
|
||||
|
||||
/**
|
||||
* pin maping:
|
||||
* 2 - digital input
|
||||
* 3 - digital input
|
||||
* 4 - digital input
|
||||
* 5 - digital input
|
||||
* 6 - digital output
|
||||
* 7 - digital output
|
||||
* 8 - digital output
|
||||
* 9 - digital output
|
||||
* 10 - analog output
|
||||
* 11 - analog output
|
||||
* 14 - analog input
|
||||
* 15 - analog input
|
||||
*
|
||||
* pin 13 reservado para ver el estado de la comunicación
|
||||
*/
|
||||
void io_setup() {
|
||||
pinMode(2, INPUT);
|
||||
pinMode(3, INPUT);
|
||||
pinMode(4, INPUT);
|
||||
pinMode(5, INPUT);
|
||||
pinMode(6, OUTPUT);
|
||||
pinMode(7, OUTPUT);
|
||||
pinMode(8, OUTPUT);
|
||||
pinMode(9, OUTPUT);
|
||||
pinMode(10, OUTPUT);
|
||||
pinMode(11, OUTPUT);
|
||||
pinMode(13, OUTPUT);
|
||||
|
||||
digitalWrite(6, LOW );
|
||||
digitalWrite(7, LOW );
|
||||
digitalWrite(8, LOW );
|
||||
digitalWrite(9, LOW );
|
||||
digitalWrite(13, HIGH ); //Led del pin 13 de la placa
|
||||
analogWrite(10, 0 ); //PWM 0%
|
||||
analogWrite(11, 0 ); //PWM 0%
|
||||
}
|
||||
|
||||
/*********************************************************
|
||||
Enlaza la tabla de registros con los pines
|
||||
*********************************************************/
|
||||
void io_poll() {
|
||||
// digital inputs -> au16data[0]
|
||||
// Lee las entradas digitales y las guarda en bits de la primera variable del vector
|
||||
// (es lo mismo que hacer una máscara)
|
||||
bitWrite( au16data[0], 0, digitalRead( 2 )); //Lee el pin 2 de Arduino y lo guarda en el bit 0 de la variable au16data[0]
|
||||
bitWrite( au16data[0], 1, digitalRead( 3 ));
|
||||
bitWrite( au16data[0], 2, digitalRead( 4 ));
|
||||
bitWrite( au16data[0], 3, digitalRead( 5 ));
|
||||
|
||||
// digital outputs -> au16data[1]
|
||||
// Lee los bits de la segunda variable y los pone en las salidas digitales
|
||||
digitalWrite( 6, bitRead( au16data[1], 0 )); //Lee el bit 0 de la variable au16data[1] y lo pone en el pin 6 de Arduino
|
||||
digitalWrite( 7, bitRead( au16data[1], 1 ));
|
||||
digitalWrite( 8, bitRead( au16data[1], 2 ));
|
||||
digitalWrite( 9, bitRead( au16data[1], 3 ));
|
||||
|
||||
// Cambia el valor del PWM
|
||||
analogWrite( 10, au16data[2] ); //El valor de au16data[2] se escribe en la salida de PWM del pin 10 de Arduino. (siendo 0=0% y 255=100%)
|
||||
analogWrite( 11, au16data[3] );
|
||||
|
||||
// Lee las entradas analógicas (ADC)
|
||||
au16data[4] = analogRead( 0 ); //El valor analógico leido en el pin A0 se guarda en au16data[4]. (siendo 0=0v y 1023=5v)
|
||||
au16data[5] = analogRead( 1 );
|
||||
|
||||
// Diagnóstico de la comunicación (para debug)
|
||||
au16data[6] = slave.getInCnt(); //Devuelve cuantos mensajes se recibieron
|
||||
au16data[7] = slave.getOutCnt(); //Devuelve cuantos mensajes se transmitieron
|
||||
au16data[8] = slave.getErrCnt(); //Devuelve cuantos errores hubieron
|
||||
}
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
/**
|
||||
* Modbus master example 1:
|
||||
* The purpose of this example is to query an array of data
|
||||
* from an external Modbus slave device.
|
||||
* The link media can be USB or RS232.
|
||||
*
|
||||
* Recommended Modbus slave:
|
||||
* diagslave http://www.modbusdriver.com/diagslave.html
|
||||
*
|
||||
* In a Linux box, run
|
||||
* "./diagslave /dev/ttyUSB0 -b 19200 -d 8 -s 1 -p none -m rtu -a 1"
|
||||
* This is:
|
||||
* serial port /dev/ttyUSB0 at 19200 baud 8N1
|
||||
* RTU mode and address @1
|
||||
*/
|
||||
|
||||
#include <ModbusRtu.h>
|
||||
|
||||
// data array for modbus network sharing
|
||||
uint16_t au16data[16];
|
||||
uint8_t u8state;
|
||||
|
||||
/**
|
||||
* Modbus object declaration
|
||||
* u8id : node id = 0 for master, = 1..247 for slave
|
||||
* port : serial port
|
||||
* u8txenpin : 0 for RS-232 and USB-FTDI
|
||||
* or any pin number > 1 for RS-485
|
||||
*/
|
||||
Modbus master(0,Serial,0); // this is master and RS-232 or USB-FTDI
|
||||
|
||||
/**
|
||||
* This is an structe which contains a query to an slave device
|
||||
*/
|
||||
modbus_t telegram;
|
||||
|
||||
unsigned long u32wait;
|
||||
|
||||
void setup() {
|
||||
Serial.begin( 19200 ); // baud-rate at 19200
|
||||
master.start();
|
||||
master.setTimeOut( 2000 ); // if there is no answer in 2000 ms, roll over
|
||||
u32wait = millis() + 1000;
|
||||
u8state = 0;
|
||||
}
|
||||
|
||||
void loop() {
|
||||
switch( u8state ) {
|
||||
case 0:
|
||||
if (millis() > u32wait) u8state++; // wait state
|
||||
break;
|
||||
case 1:
|
||||
telegram.u8id = 1; // slave address
|
||||
telegram.u8fct = 3; // function code (this one is registers read)
|
||||
telegram.u16RegAdd = 1; // start address in slave
|
||||
telegram.u16CoilsNo = 4; // number of elements (coils or registers) to read
|
||||
telegram.au16reg = au16data; // pointer to a memory array in the Arduino
|
||||
|
||||
master.query( telegram ); // send query (only once)
|
||||
u8state++;
|
||||
break;
|
||||
case 2:
|
||||
master.poll(); // check incoming messages
|
||||
if (master.getState() == COM_IDLE) {
|
||||
u8state = 0;
|
||||
u32wait = millis() + 100;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
* Modbus slave example 1:
|
||||
* The purpose of this example is to link a data array
|
||||
* from the Arduino to an external device.
|
||||
*
|
||||
* Recommended Modbus Master: QModbus
|
||||
* http://qmodbus.sourceforge.net/
|
||||
*/
|
||||
|
||||
#include <ModbusRtu.h>
|
||||
|
||||
// data array for modbus network sharing
|
||||
uint16_t au16data[16] = {
|
||||
3, 1415, 9265, 4, 2, 7182, 28182, 8, 0, 0, 0, 0, 0, 0, 1, -1 };
|
||||
|
||||
/**
|
||||
* Modbus object declaration
|
||||
* u8id : node id = 0 for master, = 1..247 for slave
|
||||
* port : serial port
|
||||
* u8txenpin : 0 for RS-232 and USB-FTDI
|
||||
* or any pin number > 1 for RS-485
|
||||
*/
|
||||
Modbus slave(1,Serial,0); // this is slave @1 and RS-232 or USB-FTDI
|
||||
|
||||
void setup() {
|
||||
Serial.begin( 19200 ); // baud-rate at 19200
|
||||
slave.start();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
slave.poll( au16data, 16 );
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
* Modbus slave example 2:
|
||||
* The purpose of this example is to link a data array
|
||||
* from the Arduino to an external device.
|
||||
*
|
||||
* Recommended Modbus Master: modpoll
|
||||
* http://www.modbusdriver.com/modpoll.html
|
||||
*/
|
||||
|
||||
#include <ModbusRtu.h>
|
||||
|
||||
// data array for modbus network sharing
|
||||
uint16_t au16data[16] = {
|
||||
3, 1415, 9265, 4, 2, 7182, 28182, 8, 0, 0, 0, 0, 0, 0, 1, -1 };
|
||||
|
||||
/**
|
||||
* Modbus object declaration
|
||||
* u8id : node id = 0 for master, = 1..247 for slave
|
||||
* port : serial port
|
||||
* u8txenpin : 0 for RS-232 and USB-FTDI
|
||||
* or any pin number > 1 for RS-485
|
||||
*/
|
||||
Modbus slave(1,Serial,0); // this is slave @1 and RS-232 or USB-FTDI
|
||||
|
||||
void setup() {
|
||||
Serial.begin( 19200, SERIAL_8E1 ); // 19200 baud, 8-bits, even, 1-bit stop
|
||||
slave.start();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
slave.poll( au16data, 16 );
|
||||
}
|
||||
+101
@@ -0,0 +1,101 @@
|
||||
/**
|
||||
* Modbus master example 2:
|
||||
* The purpose of this example is to query an array of data
|
||||
* from an external Modbus slave device.
|
||||
* This example is similar to "simple_master", but this example
|
||||
* allows you to use software serial instead of hardware serial
|
||||
* in case that you want to use D1 & D2 for other purposes.
|
||||
* The link media can be USB or RS232.
|
||||
|
||||
The circuit:
|
||||
* software serial rx(D3) connect to tx pin of another device
|
||||
* software serial tx(D4) connect to rx pin of another device
|
||||
|
||||
* In this example, we will use two important methods so that we can use
|
||||
* software serial.
|
||||
*
|
||||
* 1. Modbus::Modbus(uint8_t u8id)
|
||||
* This is a constructor for a Master/Slave through USB/RS232C via software serial
|
||||
* This constructor only specifies u8id (node address) and should be only
|
||||
* used if you want to use software serial instead of hardware serial.
|
||||
* This method is called if you create a ModBus object with only on parameter "u8id"
|
||||
* u8id is the node address of the arduino that will be programmed on,
|
||||
* 0 for master and 1..247 for slave
|
||||
* for example: Modbus master(0);
|
||||
* If you use this constructor you have to begin ModBus object by
|
||||
* using "void Modbus::begin(SoftwareSerial *softPort, long u32speed)".
|
||||
*
|
||||
* 2. void Modbus::begin(SoftwareSerial *sPort, long u32speed)
|
||||
* Initialize class object.
|
||||
* This is the method you have to use if you construct the ModBus object by using
|
||||
* Modbus::Modbus(uint8_t u8id) in order to use software serial and to avoid problems.
|
||||
* You have to create a SoftwareSerial object on your own, as shown in the example.
|
||||
* sPort is a pointer to your SoftwareSerial object, u32speed is the baud rate, in
|
||||
* standard increments (300..115200)
|
||||
|
||||
created long time ago
|
||||
by smarmengol
|
||||
modified 29 July 2016
|
||||
by Helium6072
|
||||
|
||||
This example code is in the public domain.
|
||||
*/
|
||||
|
||||
#include <ModbusRtu.h>
|
||||
#include <SoftwareSerial.h>
|
||||
|
||||
// data array for modbus network sharing
|
||||
uint16_t au16data[16];
|
||||
uint8_t u8state;
|
||||
|
||||
SoftwareSerial mySerial(3, 5);//Create a SoftwareSerial object so that we can use software serial. Search "software serial" on Arduino.cc to find out more details.
|
||||
|
||||
/**
|
||||
* Modbus object declaration
|
||||
* u8id : node id = 0 for master, = 1..247 for slave
|
||||
* port : serial port
|
||||
* u8txenpin : 0 for RS-232 and USB-FTDI
|
||||
* or any pin number > 1 for RS-485
|
||||
*/
|
||||
Modbus master(0, mySerial); // this is master and RS-232 or USB-FTDI via software serial
|
||||
|
||||
/**
|
||||
* This is an structe which contains a query to an slave device
|
||||
*/
|
||||
modbus_t telegram;
|
||||
|
||||
unsigned long u32wait;
|
||||
|
||||
void setup() {
|
||||
mySerial.begin(9600);//use the hardware serial if you want to connect to your computer via usb cable, etc.
|
||||
master.start(); // start the ModBus object.
|
||||
master.setTimeOut( 2000 ); // if there is no answer in 2000 ms, roll over
|
||||
u32wait = millis() + 1000;
|
||||
u8state = 0;
|
||||
}
|
||||
|
||||
void loop() {
|
||||
switch( u8state ) {
|
||||
case 0:
|
||||
if (millis() > u32wait) u8state++; // wait state
|
||||
break;
|
||||
case 1:
|
||||
telegram.u8id = 104; // slave address
|
||||
telegram.u8fct = 4; // function code (this one is registers read)
|
||||
telegram.u16RegAdd = 3; // start address in slave
|
||||
telegram.u16CoilsNo = 1; // number of elements (coils or registers) to read
|
||||
telegram.au16reg = au16data; // pointer to a memory array in the Arduino
|
||||
|
||||
master.query( telegram ); // send query (only once)
|
||||
u8state++;
|
||||
break;
|
||||
case 2:
|
||||
master.poll(); // check incoming messages
|
||||
if (master.getState() == COM_IDLE) {
|
||||
u8state = 0;
|
||||
u32wait = millis() + 2000;
|
||||
Serial.println(au16data[0]);//Or do something else!
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user