Guest User

Modbus RTU USB powerstrip

a guest
Jul 2nd, 2013
306
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* -----------------------------------------------------------------------------------
  2. A sketch for the modbus RTU USB power strip based on the arduino-modbus-slave library by Jason Vreeland released on 3/7/2010 under GNU license
  3. Rev: 30 July 2013, by Ayush Sagar for GSoc 2013
  4. -----------------------------------------------------------------------------------*/
  5.  
  6. #include <modbus.h>
  7. #include <modbusDevice.h>
  8. #include <modbusRegBank.h>
  9. #include <modbusSlave.h>
  10.  
  11. //Setup register bank
  12. //All of the data accumulated will be stored here
  13. modbusDevice regBank;
  14. //Create the modbus slave protocol handler
  15. modbusSlave slave;
  16.  
  17. void setup()
  18. {  
  19.  
  20. //Assign the modbus slave ID.  
  21.   regBank.setId(1);
  22.  
  23. /*
  24. modbus registers follow the following format
  25. 00001-09999  Coils (Digital Outputs), A master device can read and write to these registers
  26. 10001-19999  Discrete Inputs (Digital Inputs), A master device can only read the values from these registers
  27. 30001-39999  Input Registers (Analog Inputs), A master device can only read the values from these registers
  28. 40001-49999  Holding Registers (Analog Outputs), A master device can read and write to these registers
  29.  
  30. Analog values are 16 bit unsigned words stored with a range of 0-32767
  31. Digital values are stored as bytes, a zero value is OFF and any nonzer value is ON
  32.  
  33. */
  34.  
  35. //Add Digital Output registers (Coils) 00001-00016 to the register bank
  36.   regBank.add(1);
  37.   regBank.add(2);
  38.   regBank.add(3);
  39.   regBank.add(4);
  40.   regBank.add(5);
  41.   regBank.add(6);
  42.   regBank.add(7);
  43.   regBank.add(8);
  44.   regBank.add(9);
  45.   regBank.add(10);
  46.   regBank.add(11);
  47.   regBank.add(12);
  48. /*
  49.  
  50. //Not adding other registers because they are not needed
  51.  
  52. Assign the modbus device object to the protocol handler
  53. This is where the protocol handler will look to read and write
  54. register data.  Currently, a modbus slave protocol handler may
  55. only have one device assigned to it.
  56. */
  57.   slave._device = &regBank;  
  58.  
  59. // Initialize the serial port for coms at 9600 baud  
  60.   slave.setBaud(9600);  
  61.  
  62. // Initialize Aruino IO pins
  63.   pinMode(2, OUTPUT);
  64.   pinMode(3, OUTPUT);
  65.   pinMode(4, OUTPUT);
  66.   pinMode(5, OUTPUT);
  67.   pinMode(6, OUTPUT);
  68.   pinMode(7, OUTPUT);
  69.   pinMode(8, OUTPUT);
  70.   pinMode(9, OUTPUT);
  71.   pinMode(10, OUTPUT);
  72.   pinMode(11, OUTPUT);
  73.   pinMode(12, OUTPUT);
  74.   pinMode(13, OUTPUT);
  75. }
  76. void loop()
  77. {
  78.   digitalWrite(2,regBank.get(1));
  79.   digitalWrite(3,regBank.get(2));
  80.   digitalWrite(4,regBank.get(3));
  81.   digitalWrite(5,regBank.get(4));
  82.   digitalWrite(6,regBank.get(5));
  83.   digitalWrite(7,regBank.get(6));
  84.   digitalWrite(8,regBank.get(7));
  85.   digitalWrite(9,regBank.get(8));
  86.   digitalWrite(10,regBank.get(9));
  87.   digitalWrite(11,regBank.get(10));
  88.   digitalWrite(12,regBank.get(11));
  89.   digitalWrite(13,regBank.get(12));
  90.   slave.run();
  91. }
  92. /*-----------------------------------------------------------------------------------*/
RAW Paste Data