Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * 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
- * u8serno : serial port (use 0 for Serial)
- * u8txenpin : 0 for RS-232 and USB-FTDI
- * or any pin number > 1 for RS-485
- */
- Modbus master(0,0,9); // this is master and RS-232 or USB-FTDI, PIN 9 PARA INDICAR QUE TRASMITO
- /**
- * This is an structe which contains a query to an slave device
- */
- modbus_t telegram;
- unsigned long u32wait;
- void setup() {
- master.begin( 9600 ); // baud-rate at 19200
- 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 = 5; // function code (this one is registers read)
- telegram.u16RegAdd = 0; // start address in slave
- telegram.u16CoilsNo = 2; // number of elements (coils or registers) to read
- //telegram.au16reg = au16data; // pointer to a memory array in the Arduino
- // PARA EL VARIADOR OMRON MX1: MODBUS RTU 01 05 00 00 FF 00 -> START
- // 01 -> SLAVE (VFD OMRON)
- // 05 -> WRITE COIL
- // 00 00 -> ADDRESS COIL
- // FF 00 -> VALUES TO WRITE IN COIL FOR RUN
- telegram.au16reg[0] = 0;
- telegram.au16reg[1] = 0;
- telegram.au16reg[2] = 0;
- telegram.au16reg[3] = 0;
- telegram.au16reg[4] = 0;
- telegram.au16reg[5] = 0;
- telegram.au16reg[6] = 0;
- telegram.au16reg[7] = 0;
- telegram.au16reg[8] = 1;
- telegram.au16reg[9] = 1;
- telegram.au16reg[10] = 1;
- telegram.au16reg[11] = 1;
- telegram.au16reg[12] = 1;
- telegram.au16reg[13] = 1;
- telegram.au16reg[14] = 1;
- telegram.au16reg[15] = 1;
- master.query( telegram ); // send query (only once)
- delay(3500);
- telegram.u8id = 1; // slave address
- telegram.u8fct = 6; // 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
- //telegram.au16reg = 0000000111110100;
- // PARA EL VARIADOR OMRON MX1: MODBUS RTU 01 06 00 01 01 F4 -> CHANGE FQ
- // 01 -> SLAVE (VFD OMRON)
- // 06 -> WRITE IN REGISTE
- // 00 01 -> REGISTER ADDRESS
- // 01 F4 -> VALUE TO WRITE IN THE REGISTER TO CHANGE THE MOTOR FREQUENCY
- telegram.au16reg[0] = 0;
- telegram.au16reg[1] = 0;
- telegram.au16reg[2] = 0;
- telegram.au16reg[3] = 0;
- telegram.au16reg[4] = 0;
- telegram.au16reg[5] = 0;
- telegram.au16reg[6] = 0;
- telegram.au16reg[7] = 1;
- telegram.au16reg[8] = 1;
- telegram.au16reg[9] = 1;
- telegram.au16reg[10] = 1;
- telegram.au16reg[11] = 1;
- telegram.au16reg[12] = 0;
- telegram.au16reg[13] = 1;
- telegram.au16reg[14] = 0;
- telegram.au16reg[15] = 0;
- master.query( telegram ); // send query (only once)
- delay(3500);
- telegram.u8id = 1; // slave address
- telegram.u8fct = 5; // function code (this one is registers read)
- telegram.u16RegAdd = 1; // start address in slave
- telegram.u16CoilsNo = 2; // number of elements (coils or registers) to read
- //telegram.au16reg = au16data; // pointer to a memory array in the Arduino
- // PARA EL VARIADOR OMRON MX1: MODBUS RTU 01 05 00 00 00 00 -> STOP
- // 01 -> ESCLAVO (VARIADOR OMRON)
- // 05 -> ESCRIBIR EN UNA BOBINA
- // 00 00 -> DIRECCION DE LA BOBINA
- // 00 00 -> VALOR A ESCRIBIR EN LA BOBINA PARA STOP
- telegram.au16reg[0] = 0;
- telegram.au16reg[1] = 0;
- telegram.au16reg[2] = 0;
- telegram.au16reg[3] = 0;
- telegram.au16reg[4] = 0;
- telegram.au16reg[5] = 0;
- telegram.au16reg[6] = 0;
- telegram.au16reg[7] = 0;
- telegram.au16reg[8] = 0;
- telegram.au16reg[9] = 0;
- telegram.au16reg[10] = 0;
- telegram.au16reg[11] = 0;
- telegram.au16reg[12] = 0;
- telegram.au16reg[13] = 0;
- telegram.au16reg[14] = 0;
- telegram.au16reg[15] = 0;
- master.query( telegram ); // send query (only once)
- delay(3500);
- u8state++;
- break;
- case 2:
- master.poll(); // check incoming messages
- if (master.getState() == COM_IDLE) {
- u8state = 0;
- u32wait = millis() + 100;
- }
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement