Advertisement
ektoras2012

Untitled

Jun 5th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.38 KB | None | 0 0
  1. /**
  2.  *  Modbus master example 2:
  3.  *  The purpose of this example is to query several sets of data
  4.  *  from an external Modbus slave device.
  5.  *  The link media can be USB or RS232.
  6.  *
  7.  *  Recommended Modbus slave:
  8.  *  diagslave http://www.modbusdriver.com/diagslave.html
  9.  *
  10.  *  In a Linux box, run
  11.  *  "./diagslave /dev/ttyUSB0 -b 19200 -d 8 -s 1 -p none -m rtu -a 1"
  12.  *   This is:
  13.  *    serial port /dev/ttyUSB0 at 19200 baud 8N1
  14.  *    RTU mode and address @1
  15.  */
  16.  
  17.  
  18. #include <ModbusRtu.h>
  19. #include <SoftwareSerial.h>
  20. #define ID   0
  21. //#define Led 3
  22.  
  23.  
  24. uint16_t au16data[32]; //!< data array for modbus network sharing
  25.  
  26.  
  27. uint8_t u8state; //!< machine state
  28. uint8_t u8query; //!< pointer to message query
  29.  
  30.  
  31.  
  32.  
  33. /**
  34.  *  Modbus object declaration
  35.  *  u8id : node id = 0 for master, = 1..247 for slave
  36.  *  u8serno : serial port (use 0 for Serial)
  37.  *  u8txenpin : 0 for RS-232 and USB-FTDI
  38.  *               or any pin number > 1 for RS-485
  39.  */
  40. Modbus master(0,0,2); // this is master and RS-232 or USB-FTDI
  41.  
  42.  
  43. /**
  44.  * This is an structe which contains a query to an slave device
  45.  */
  46. modbus_t telegram[2];
  47.  
  48.  
  49. unsigned long u32wait;
  50.  
  51.  
  52. SoftwareSerial mySerial(0, 1);
  53.  
  54.  
  55. void setup() {
  56.  
  57.  
  58. ioset_up();
  59.   // telegram 0: write single coil
  60.   telegram[0].u8id = 1; // slave address
  61.   telegram[0].u8fct = 5; // function code (this one is single coil)
  62.   telegram[0].u16RegAdd = 0; // start address in slave
  63.   telegram[0].u16CoilsNo = 2; // number of elements (coils or registers) to read
  64.   telegram[0].au16reg = au16data[0]; // pointer to a memory array in the Arduino
  65.  
  66.   //telegram 1: write a single register
  67.   telegram[1].u8id = 1; // slave address
  68.   telegram[1].u8fct = 6; // function code (this one is write a single register)
  69.   telegram[1].u16RegAdd = 1; // start address in slave
  70.   telegram[1].u16CoilsNo = 4; // number of elements (coils or registers) to read
  71.   telegram[1].au16reg = au16data[0]; // pointer to a memory array in the Arduino
  72.  
  73.   // telegram 2: write single coil
  74.   telegram[2].u8id = 1; // slave address
  75.   telegram[2].u8fct = 5; // function code (this one is single coil)
  76.   telegram[2].u16RegAdd = 1; // start address in slave
  77.   telegram[2].u16CoilsNo = 2; // number of elements (coils or registers) to read
  78.   telegram[2].au16reg = au16data[0]; // pointer to a memory array in the Arduino
  79.  
  80.   Serial.begin( 9600);
  81.   master.begin( 9600 ); // baud-rate at 9600
  82.   master.begin( &mySerial, 9600 );
  83.   master.setTimeOut( 3000 ); // if there is no answer in 3000 ms, roll over
  84.   u32wait = millis() + 1000;
  85.   u8state = u8query = 0;
  86.  
  87.  
  88.   pinMode(6, OUTPUT);
  89.   pinMode(7, OUTPUT);
  90.   pinMode(8, OUTPUT);
  91.   pinMode(9, OUTPUT);
  92.  
  93.  
  94. }
  95. void loop() {
  96.   io_poll();
  97.  
  98.  
  99.   switch( u8state ) {
  100.   case 0:
  101.     if (millis() > u32wait) u8state++; // wait state
  102.     break;
  103.   case 1:
  104.     master.query( telegram[u8query] ); // send query (only once)
  105.     u8state++;
  106.   u8query++;
  107.   if (u8query > 2) u8query = 0;
  108.     break;
  109.   case 2:
  110.     master.poll(); // check incoming messages
  111.     if (master.getState() == COM_IDLE) {
  112.       u8state = 0;
  113.       u32wait = millis() + 1000;
  114.  
  115.    Serial.print(au16data[0]);
  116.  
  117.  
  118.     }
  119.     break;
  120.  
  121.  
  122.   // set digital outputs -> au16data[0]
  123.   digitalWrite( 6, bitRead( au16data[0], 0 ));
  124.   digitalWrite( 7, bitRead( au16data[0], 1 ));
  125.   digitalWrite( 8, bitRead( au16data[0], 2 ));
  126.   digitalWrite( 9, bitRead( au16data[0], 3 ));
  127.  
  128.   }
  129.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement