Hi Team,
Please explain this code little bit as i asked query against the comments, i need to understand this in well manners.
secondly, this code is working and i tested with HMI. i need to convert these bits to words. Please guide for this.
#include <ModbusRtu.h>
#define ID 1
Modbus slave(ID, 0, 0); // First digit indicates ID number but what is other (ID, 0,0) ?
int8_t state = 0; // what does it indicates
unsigned long tempus;
uint16_t au16data[5]; // what does it mean by array 5, is that mean i can use only 5 input outputs?
void setup() {
configuracionIO();
slave.begin(9600);
tempus = millis() + 100; // what is this purpose.
digitalWrite(13, HIGH );
}
void loop() {
// poll messages
// blink led pin on each valid message
state = slave.poll( au16data, 5 ); what os this poll message? what does it mean by 5?
// what is this state 4 mean
if (state > 4) {
tempus = millis() + 50;
digitalWrite(13, HIGH);
}
if (millis() > tempus)
digitalWrite(13, LOW );
MapeoIOmodbus();
}
void configuracionIO() {
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, INPUT);
pinMode(7, INPUT);
pinMode(8, INPUT);
pinMode(9, INPUT);
pinMode(10, INPUT);
pinMode(11, INPUT);
pinMode(12, INPUT);
// pinMode(13, INPUT);
digitalWrite(2, LOW );
digitalWrite(3, LOW );
digitalWrite(4, LOW );
digitalWrite(5, LOW );
// analogWrite(10, 0 ); //PWM 0%
//analogWrite(11, 0 ); //PWM 0%
}
/*********************************************************
Enlaza la tabla de registros con los pines
Bind register table to pins
*********************************************************/
void MapeoIOmodbus() {
// digital outputs -> au16data[0]
// Lee los bits de la variable cero y los pone en las salidas digitales
//Reads the bits of the zero variable and puts them on the digital outputs
digitalWrite( 2, bitRead( au16data[0], 0 )); //Lee el bit 0 de la variable au16data[0] y lo pone en el pin 2 de Arduino
digitalWrite( 3, bitRead( au16data[0], 1 )); //Read bit 0 from variable au16data[0] and put it on pin 2 of Arduino
digitalWrite( 4, bitRead( au16data[0], 2 ));
digitalWrite( 5, bitRead( au16data[0], 3 ));
// digital inputs -> au16data[1]
// 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[1], 0, digitalRead( 6 )); //Lee el pin 6 del Arduino y lo guarda en el bit 0 de la variable au16data[0]
bitWrite( au16data[1], 1, digitalRead( 7 )); // Read pin 6 from the Arduino and store it in bit 0 of the variable au16data[0]
bitWrite( au16data[1], 2, digitalRead( 8 ));
bitWrite( au16data[1], 3, digitalRead( 9 ));
bitWrite( au16data[1], 4, digitalRead( 10 )); //Lee el pin 6 del Arduino y lo guarda en el bit 0 de la variable au16data[0]
bitWrite( au16data[1], 5, digitalRead( 11 )); // Read pin 6 from the Arduino and store it in bit 0 of the variable au16data[0]
bitWrite( au16data[1], 6, digitalRead( 12));
//bitWrite( au16data[1], 7, digitalRead( 13));
// 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 entradaa analógica (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)
}