Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Test modbus Server Library https://github.com/emelianov/modbus-esp8266
- */
- #include <SPI.h>
- #include <Ethernet.h>
- #include <ModbusEthernet.h>
- #define MAC 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x69
- #define IP 192, 168, 6,69
- ModbusEthernet modbus; // Modbus TCP instance
- void setup() {
- byte mac[] = {MAC};
- IPAddress ip(IP); // The IP address will be dependent on your local network:
- Serial.begin(9600); // Open serial communications and wait for port to open
- while (!Serial) {} // wait for serial port to connect. Needed for Leonardo only
- Ethernet.begin(mac, ip); // start the Ethernet connection
- delay(1200); // Arceli W5500 time to initialice
- Serial.print (F(" Modbus Server TCP on "));
- Serial.println (ip);
- modbus.server(); // TCP modbus server
- createModbusMemory( &modbus, 8 ,8); //Crée 8 entrées et 8 sorties
- }
- //Create the modbus memory structure
- //https://github.com/emelianov/modbus-esp8266/tree/master/examples/TCP-ESP#API
- void createModbusMemory(ModbusEthernet *modbus , int in , int out){
- (*modbus).addHreg(0, 0, in);//010300000002 Resp. 01030400010001 or 01030400000000
- (*modbus).addCoil(0, 0, out);//010100000008 https://www.simplymodbus.ca/FC15.htm
- }
- void ReadModbusMemory (ModbusEthernet *modbus,int in,int out){
- for (int i=in ;i<out+8;i++){//Read in
- Serial.print ((*modbus).Coil(i)) ;//bool Coil(uint16_t offset);
- }
- Serial.println(F(""));
- for (int i=in ; i<in+8 ; i++){//Read in
- Serial.print ( (*modbus).Hreg(i)) ;// Hreg(uint16_t offset)
- }
- Serial.println(F("\n"));
- }
- void loop() {
- modbus.task(); // Server Modbus TCP queries
- ReadModbusMemory (&modbus,0,0);
- delay(50);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement