代码拉取完成,页面将自动刷新
/*
Modbus-Arduino Example - Master Modbus IP Client (ESP8266/ESP32)
Read Holding Register from Server device
(c)2018 Alexander Emelianov (a.m.emelianov@gmail.com)
https://github.com/emelianov/modbus-esp8266
*/
#ifdef ESP8266
#include <ESP8266WiFi.h>
#else
#include <WiFi.h>
#endif
#include <ModbusIP_ESP8266.h>
const int REG = 528; // Modbus Hreg Offset
IPAddress remote(192, 168, 30, 13); // Address of Modbus Slave device
const int LOOP_COUNT = 10;
ModbusIP mb; //ModbusIP object
void setup() {
Serial.begin(115200);
WiFi.begin("SSID", "PASSWORD");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
mb.client();
}
uint16_t res = 0;
uint8_t show = LOOP_COUNT;
void loop() {
if (mb.isConnected(remote)) { // Check if connection to Modbus Slave is established
mb.readHreg(remote, REG, &res); // Initiate Read Coil from Modbus Slave
} else {
mb.connect(remote); // Try to connect if no connection
}
mb.task(); // Common local Modbus task
delay(100); // Pulling interval
if (!show--) { // Display Slave register value one time per second (with default settings)
Serial.println(res);
show = LOOP_COUNT;
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。