使用 RS 485 MODBUS 和 ARDuino mega 从 Elite 100 电能表读取数据
我正在尝试使用 Arduino Mega 从能量计(型号:ELITE 100,品牌:SECURE)读取 4 个寄存器。我使用 RS 485 转 TTL 模块让 arduino 与电能表进行通信。上传代码后,在串行监视器中我看到arduino无法与电能表通信,并显示十六进制格式的响应代码是“E2”。我无法理解真正的问题是什么。请帮我。
电能表 MODBUS 注册链接:https://drive.google.com/drive/folders/1kTQg0wvcX-iG7jkeZHwIUkK8e-9VVVW_?usp=sharing
一些设置值为: 奇偶校验位:无,波特率:9600,停止位:1
代码输出为:失败,响应代码:E2
#include<ModbusMaster.h>
/* DI = UNO / NANO TX PIN / arduino mega pin 1 TX pin
RO = UNO/ NANO RX PIN / arduino mega pin 0 RX Pin
*/
#define MAX485_DE 3
#define MAX485_RE_NEG 2
ModbusMaster node;
void preTransmission() {
digitalWrite(MAX485_RE_NEG, 1);
digitalWrite(MAX485_DE, 1);
}
void postTransmission()
{
digitalWrite(MAX485_RE_NEG, 0);
digitalWrite(MAX485_DE, 0);
}
void setup() {
pinMode(MAX485_RE_NEG, OUTPUT);
pinMode(MAX485_DE, OUTPUT);
// Init in receive mode
digitalWrite(MAX485_RE_NEG, 0);
digitalWrite(MAX485_DE, 0);
Serial.begin(9600, SERIAL_8N1);
//slave ID 1
node.begin(1, Serial);
Serial.println("Starting Modbus Transaction:");
node.preTransmission(preTransmission);
node.postTransmission(postTransmission);
}
uint16_t newData = 0;
float floatData;
double dataOne;
void loop() { // loop starts from here.
static uint32_t i;
uint8_t j, result;
uint16_t data[10];
i++;
result = node.readHoldingRegisters(40172, 2); // read holding registars
Serial.println(" ");
/*
if (result == node.ku8MBSuccess) {
Serial.print("\nSuccess, Received data 0: ");
for (j = 0; j < 2; j++) { // THIS FUNCTION READ SINGLE REGISTAR
data[j] = node.getResponseBuffer(j);
floatData, dataOne = node.getResponseBuffer(j);
Serial.print("\nHex data:");
Serial.print(data[j], HEX);
Serial.print(" ");
Serial.print("\nDec data:");
Serial.print(data[j], DEC);
Serial.print("\nfloat data:");
Serial.print(floatData);
*/
if (result == node.ku8MBSuccess) {
Serial.print("\nSuccess, Received data 0: ");
for (j = 0; j < 2; j++) {
data[j] = node.getResponseBuffer(j);
}
unsigned long temp = (unsigned long)data[0] + (unsigned long)data[1] * 65536;
floatData = *((float*)&temp);
Serial.print(floatData);
}
//}
//}
else {
Serial.print("Failed, Response Code: ");
Serial.print(result, HEX);
Serial.println(" ");
delay(5000); // 5000
}
delay(1000);
}
I'm trying to read 4 Registers from a Energy Meter (Model no: ELITE 100, Make : SECURE), using Arduino Mega. I'm using RS 485 to TTL module for arduino to communicate with the Energy meter. After uploading the code,in serial monitor I saw that arduino fails to communicate with the energy meter, and show the response code in hex format is "E2". I unable to understand what is the actual problem. Please help me.
Energy Meter MODBUS Register link: https://drive.google.com/drive/folders/1kTQg0wvcX-iG7jkeZHwIUkK8e-9VVVW_?usp=sharing
Some settings values are:
Parity Bit: None, Baud Rate : 9600 , Stop Bit : 1
CODE Output is: Failed, Response Code: E2
#include<ModbusMaster.h>
/* DI = UNO / NANO TX PIN / arduino mega pin 1 TX pin
RO = UNO/ NANO RX PIN / arduino mega pin 0 RX Pin
*/
#define MAX485_DE 3
#define MAX485_RE_NEG 2
ModbusMaster node;
void preTransmission() {
digitalWrite(MAX485_RE_NEG, 1);
digitalWrite(MAX485_DE, 1);
}
void postTransmission()
{
digitalWrite(MAX485_RE_NEG, 0);
digitalWrite(MAX485_DE, 0);
}
void setup() {
pinMode(MAX485_RE_NEG, OUTPUT);
pinMode(MAX485_DE, OUTPUT);
// Init in receive mode
digitalWrite(MAX485_RE_NEG, 0);
digitalWrite(MAX485_DE, 0);
Serial.begin(9600, SERIAL_8N1);
//slave ID 1
node.begin(1, Serial);
Serial.println("Starting Modbus Transaction:");
node.preTransmission(preTransmission);
node.postTransmission(postTransmission);
}
uint16_t newData = 0;
float floatData;
double dataOne;
void loop() { // loop starts from here.
static uint32_t i;
uint8_t j, result;
uint16_t data[10];
i++;
result = node.readHoldingRegisters(40172, 2); // read holding registars
Serial.println(" ");
/*
if (result == node.ku8MBSuccess) {
Serial.print("\nSuccess, Received data 0: ");
for (j = 0; j < 2; j++) { // THIS FUNCTION READ SINGLE REGISTAR
data[j] = node.getResponseBuffer(j);
floatData, dataOne = node.getResponseBuffer(j);
Serial.print("\nHex data:");
Serial.print(data[j], HEX);
Serial.print(" ");
Serial.print("\nDec data:");
Serial.print(data[j], DEC);
Serial.print("\nfloat data:");
Serial.print(floatData);
*/
if (result == node.ku8MBSuccess) {
Serial.print("\nSuccess, Received data 0: ");
for (j = 0; j < 2; j++) {
data[j] = node.getResponseBuffer(j);
}
unsigned long temp = (unsigned long)data[0] + (unsigned long)data[1] * 65536;
floatData = *((float*)&temp);
Serial.print(floatData);
}
//}
//}
else {
Serial.print("Failed, Response Code: ");
Serial.print(result, HEX);
Serial.println(" ");
delay(5000); // 5000
}
delay(1000);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想获取链接中提到的保持寄存器的值: https://drive.google.com/drive/folders/1kTQg0wvcX-iG7jkeZHwIUkK8e-9VVVW_?usp=sharing
读取 V1:R 相到中性点电压在初始地址中减 1 表示
40100 - 1 = 40099 并仅将 99 传递给代码 Ex。 node.readHoldingRegisters(99, 2)
希望它能起作用,也感谢 Brits
If you want to get the value of holding register as mentioned in Link: https://drive.google.com/drive/folders/1kTQg0wvcX-iG7jkeZHwIUkK8e-9VVVW_?usp=sharing
to read V1: R Phase to Neutral Voltage subtract 1 in initial address means
40100 - 1 = 40099 and pass only 99 to the code Ex. node.readHoldingRegisters(99, 2)
Hope it work and also thanks to Brits