Ai
1 Star 0 Fork 0

NightBreeze/modbus-esp8266

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
client.ino 1.38 KB
一键复制 编辑 原始数据 按行查看 历史
Alexander Emelianov 提交于 2022-03-08 20:11 +08:00 . _ to - in examples
/*
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;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/d__xin/modbus-esp8266.git
git@gitee.com:d__xin/modbus-esp8266.git
d__xin
modbus-esp8266
modbus-esp8266
master

搜索帮助