Advertisement
AntonioVillanueva

Test libreria de Alexander Emelianov

Apr 19th, 2022 (edited)
1,166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. /*
  3.   Test modbus Server Library https://github.com/emelianov/modbus-esp8266
  4.  
  5. */
  6.  
  7. #include <SPI.h>
  8. #include <Ethernet.h>
  9. #include <ModbusEthernet.h>
  10.  
  11. #define MAC  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x69
  12. #define IP 192, 168, 6,69
  13.  
  14. ModbusEthernet modbus;              // Modbus TCP instance
  15.  
  16. void setup() {
  17.  
  18.   byte mac[] = {MAC};
  19.   IPAddress ip(IP); // The IP address will be dependent on your local network:  
  20.  
  21.   Serial.begin(9600);     // Open serial communications and wait for port to open
  22.   while (!Serial) {}        // wait for serial port to connect. Needed for Leonardo only
  23.  
  24.   Ethernet.begin(mac, ip);  // start the Ethernet connection
  25.   delay(1200);              // Arceli W5500 time to initialice
  26.  
  27.   Serial.print (F(" Modbus Server TCP on "));  
  28.   Serial.println (ip);
  29.  
  30.   modbus.server();              // TCP modbus server
  31.   createModbusMemory( &modbus, 8 ,8); //Crée 8 entrées et 8 sorties
  32.  
  33. }
  34.  
  35. //Create the modbus memory structure
  36. //https://github.com/emelianov/modbus-esp8266/tree/master/examples/TCP-ESP#API
  37. void createModbusMemory(ModbusEthernet *modbus , int in , int out){
  38.   (*modbus).addHreg(0, 0, in);//010300000002 Resp. 01030400010001 or 01030400000000
  39.   (*modbus).addCoil(0, 0, out);//010100000008 https://www.simplymodbus.ca/FC15.htm
  40.  
  41. }
  42.  
  43. void ReadModbusMemory (ModbusEthernet *modbus,int in,int out){
  44.   for (int i=in ;i<out+8;i++){//Read in
  45.     Serial.print ((*modbus).Coil(i)) ;//bool Coil(uint16_t offset);    
  46.   }
  47.  
  48.   Serial.println(F(""));
  49.   for (int i=in ; i<in+8 ; i++){//Read in
  50.     Serial.print ( (*modbus).Hreg(i)) ;// Hreg(uint16_t offset)    
  51.   }
  52.   Serial.println(F("\n"));  
  53.  
  54.  
  55. }
  56.  
  57. void loop() {
  58.   modbus.task();                // Server Modbus TCP queries
  59.   ReadModbusMemory (&modbus,0,0);
  60.   delay(50);
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement