1 Star 0 Fork 0

NightBreeze/modbus-esp8266

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Transactional.ino 1.78 KB
一键复制 编辑 原始数据 按行查看 历史
Alexander Emelianov 提交于 2020-10-11 03:04 +08:00 . Cleanup code style and fix bugs
/*
Modbus-Arduino Example - Modbus IP Client (ESP8266/ESP32)
Write multiple coils to Slave device
(c)2019 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 = 100; // Modbus Coils Offset
const int COUNT = 5; // Count of Coils
IPAddress remote(192, 168, 20, 102); // Address of Modbus Slave device
ModbusIP mb; // ModbusIP object
void setup() {
Serial.begin(115200);
WiFi.begin();
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();
}
bool cb(Modbus::ResultCode event, uint16_t transactionId, void* data) { // Modbus Transaction callback
if (event != Modbus::EX_SUCCESS) // If transaction got an error
Serial.printf("Modbus result: %02X\n", event); // Display Modbus error code
if (event == Modbus::EX_TIMEOUT) { // If Transaction timeout took place
mb.disconnect(remote); // Close connection to slave and
mb.dropTransactions(); // Cancel all waiting transactions
}
return true;
}
bool res[COUNT] = {false, true, false, true, true};
void loop() {
if (!mb.isConnected(remote)) { // Check if connection to Modbus Slave is established
mb.connect(remote); // Try to connect if no connection
Serial.print(".");
}
if (!mb.writeCoil(remote, REG, res, COUNT, cb)) // Try to Write array of COUNT of Coils to Modbus Slave
Serial.print("#");
mb.task(); // Modbus task
delay(50); // Pushing interval
}
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

搜索帮助