Lavanya rajan
Well-known member
Hello again,
I'm encountering an issue with another device from the same series. This particular device features analog input and output, each with four channels. The analog output is of the Read/Write type, operating with a function code of 03. It's designed to output either voltage (ranging from +/- 0.06 to +/- 10V) or current (4-20mA).
My objective is to output a voltage of 2V using the Write Holding Register function. I've implemented the corresponding code, which I'm providing below. However, upon measuring the voltage at the output terminals, it doesn't match the intended 2V.
Could anyone provide assistance in resolving this discrepancy?
serial output
device settings
I'm encountering an issue with another device from the same series. This particular device features analog input and output, each with four channels. The analog output is of the Read/Write type, operating with a function code of 03. It's designed to output either voltage (ranging from +/- 0.06 to +/- 10V) or current (4-20mA).
My objective is to output a voltage of 2V using the Write Holding Register function. I've implemented the corresponding code, which I'm providing below. However, upon measuring the voltage at the output terminals, it doesn't match the intended 2V.
Could anyone provide assistance in resolving this discrepancy?
Code:
#include <SPI.h>
#include <Ethernet.h>
#include <ArduinoRS485.h>
#include <ArduinoModbus.h>
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(172, 18, 7, 161);
EthernetClient ethClient;
ModbusTCPClient modbusTCPClient(ethClient);
IPAddress server(172, 18, 7, 159);
int MB_HR[4]; // Array to hold int values for holding registers
void setup() {
Serial.begin(9600);
Ethernet.init(29);
while (!Serial) {
;
}
Ethernet.begin(mac, ip);
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1);
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
}
void loop() {
if (!modbusTCPClient.connected()) {
Serial.println("Attempting to connect to Modbus TCP server");
if (!modbusTCPClient.begin(server, 502)) {
Serial.println("Modbus TCP Client failed to connect!");
} else {
Serial.println("Modbus TCP Client connected");
}
} else {
Serial.println("Attempting to read A-I/O status from server");
updateholdingregisters();
}
}
void updateholdingregisters() {
Serial.println("Analog Output status");
for (int i = 0; i < 4; i++) {
int valueToWrite = 1000;
MB_HR[i] = valueToWrite;
boolean writeSuccess = modbusTCPClient.holdingRegisterWrite(0x01, i, MB_HR[i]);
if (writeSuccess) {
Serial.print("Wrote to AO ");
Serial.print(i);
Serial.println(": Write successful");
} else {
Serial.print("Failed to write to AO ");
Serial.println(i);
}
int readValue = modbusTCPClient.holdingRegisterRead(0x01, i);
if (readValue != -1) {
Serial.print("Read from AO ");
Serial.print(i);
Serial.print(": ");
Serial.println(readValue);
} else {
Serial.print("Failed to read from AO ");
Serial.println(i);
}
}
delay(1000); // Adjust delay as needed
}
serial output
Code:
Attempting to connect to Modbus TCP server
Modbus TCP Client connected
Attempting to read A-I/O status from server
Analog Output status
Wrote to AO 0: Write successful
Read from AO 0: 2000
Wrote to AO 1: Write successful
Read from AO 1: 2000
Wrote to AO 2: Write successful
Read from AO 2: 2000
Wrote to AO 3: Write successful
Read from AO 3: 2000
device settings