Hello,
I am using Arduino Opta and Delta PLC DVP14SS2. With Modbus Poll I can send command to set coil successfully On or Off. After each command I get a response from PLC. The problem is with Opta. I can send command to set coil successfully On or Off but I can`t read the response. RS485.available() is [-1].
#include <Arduino.h>
#define DEBUG true //set to true for debug output, false for no debug output
#define DEBUG_SERIAL if(DEBUG)Serial
#include <ArduinoRS485.h>
#include "CRC16.h"
void send_request_on_hex();
void send_request_off_hex();
void send_value_one();
void send_value_two();
CRC16 crc(CRC16_MODBUS_POLYNOME,
CRC16_MODBUS_INITIAL,
CRC16_MODBUS_XOR_OUT,
CRC16_MODBUS_REV_IN,
CRC16_MODBUS_REV_OUT);
int crc_low;
int crc_high;
constexpr auto baudrate{ 9600 };
// Calculate preDelay and postDelay in microseconds for stable RS-485 transmission
constexpr auto bitduration{ 1.f / baudrate };
constexpr auto wordlen{ 9.6f }; // OR 10.0f depending on the channel configuration
constexpr auto preDelayBR{ bitduration * wordlen * 3.5f * 1e6 };
constexpr auto postDelayBR{ bitduration * wordlen * 3.5f * 1e6 };
const int Req_on[] = {2, 5, 8, 0, 255, 0, 0x8E, 0x69};
const int Req_off[] = {2, 5, 8, 0, 0, 0, 0xCF, 0x99};
void setup() {
delay(10000);
DEBUG_SERIAL.begin(baudrate);
RS485.begin(baudrate, SERIAL_8E1);
RS485.setDelays(preDelayBR, postDelayBR);
}
void loop() {
send_request_on_hex();
delay(2000);
send_value_one();
delay(2000);
send_request_off_hex();
delay(2000);
send_value_two();
delay(2000);
}
void send_request_on_hex()
{
byte i;
DEBUG_SERIAL.println();
DEBUG_SERIAL.println("Sending ON command");
DEBUG_SERIAL.println();
delay(1500);
RS485.beginTransmission();
for (i = 0; i < 8; i++)
{
DEBUG_SERIAL.print("- Sending: ");
DEBUG_SERIAL.println(Req_on[i]);
RS485.write(Req_on[i]);
}
RS485.endTransmission();
RS485.receive();
if (RS485.available() > 0)
{
DEBUG_SERIAL.println("Receiving...");
DEBUG_SERIAL.println(RS485.read(), HEX);
DEBUG_SERIAL.println(RS485.read(), HEX);
DEBUG_SERIAL.println(RS485.read(), HEX);
DEBUG_SERIAL.println(RS485.read(), HEX);
DEBUG_SERIAL.println(RS485.read(), HEX);
DEBUG_SERIAL.println(RS485.read(), HEX);
DEBUG_SERIAL.println(RS485.read(), HEX);
DEBUG_SERIAL.println(RS485.read(), HEX);
}
else{
DEBUG_SERIAL.println("No data");
}
RS485.noReceive();
}