RS485 data Communication to Autonics CT6s-1p4t

#include <ModbusMaster.h>
#include <SoftwareSerial.h>

float NG1 = 0;

uint32_t lastSentTime = 2000;

SoftwareSerial SerialMod(15,4);
ModbusMaster node;
#define modbusaddr 1

void preTransmission()
{
// digitalWrite(MAX45_RE_NEG, 1);
// digitalWrite(MAX45_DE,1);
// delay(10);
}

void postTransmission()
{
// digitalWrite(MAX45_RE_NEG, 0);
// digitalWrite(MAX45_DE,0);
// delay(10);
}

void setup() {
Serial.begin(115200);
SerialMod.begin(9600);

delay(2000);
Serial.println("Ready");

node.begin(modbusaddr, SerialMod);
node.preTransmission(preTransmission);
node.postTransmission(postTransmission);
}

float reform_uint16_2_float32(uint16_t u1, uint16_t u2)
{
uint32_t num = ((uint32_t)u1 & 0xFFFF) << 16 | ((uint32_t)u2 & 0xFFFF);
float numf;
memcpy(&numf, &num, 4);
return numf;
}

float getRTU(uint16_t m_startAddress){
uint8_t m_length =2;
uint16_t result;
float x;

node.preTransmission(preTransmission);
node.postTransmission(postTransmission);
result = node.readInputRegisters(0x03EB, 2);
if (result == node.ku8MBSuccess) {
return reform_uint16_2_float32(node.getResponseBuffer(0),node.getResponseBuffer(1));
}
}

void loop() {
NG1 = getRTU(0x03EB);
Serial.println("Counter : " + String(NG1,0));
delay(1000);
Serial.println("==================================");
}

I have set the address data on Autonics in TheRTU RS485 modbus folder

What is your problem code or hardware?

Moved to Programming Questions, please also include code tags around your code and state your problem.

Thank you.

And don't forget to tell us which Arduino you are using, which RS485 module you are using, how you've wired it up (a hand drawn sketch is fine) and provide a link to the user manual for the Modbus device you are trying to talk to.

1 Like

it seems that you know that transmitter of the rs-485 interface chip needs to be enabled before transmission and needs to be disabled afterwards. Serial.flush() can be used to wait for transmission to complete to disable the transmitter.

preTransmission() needs to enable the transmitter and postTransmission() should call Serial.flush() and disable the transmitter. no need for delays


I have a problem reading RTU RS485 Nodbus data , sorry data is interger

#include <ModbusMaster.h>
#include <SoftwareSerial.h>

int NG1=0;
int NG2=0;
int NG3=0;
int Good=0;
//float PF=0;
//float F=0;

SoftwareSerial SerialMod(15,4);
ModbusMaster node;
#define modbusaddr 01
const unsigned long baud = 9600;
const unsigned int bufSize = 255;
const unsigned int numInputRegisters = 2;
const byte rxPin = 22;
const byte txPin = 23;
const byte potPins[numInputRegisters] = {16, 17};
uint32_t lastSentTime = 2500;
byte buf[bufSize];
SoftwareSerial master(rxPin, txPin);


void preTransmission()
{
  //delay(1);
}

void postTransmission()
{
  //delay(1);
}

void setup() {
  Serial.begin(115200); 
  Serial1.begin(9600, SERIAL_8N1); 
  delay(2000);
  Serial.println("Ready");

  node.begin(modbusaddr, SerialMod);
  node.preTransmission(preTransmission);
  node.postTransmission(postTransmission);   
}

float reform_uint16_2_float32(uint16_t u1, uint16_t u2)
{  
  uint32_t num = ((uint32_t)u1 & 0xFFFF) << 16 | ((uint32_t)u2 & 0xFFFF);
    float numf;
    memcpy(&numf, &num, 4);
    return numf;
}

float getRTU(uint16_t m_startAddress){
  uint8_t m_length =2;
  uint16_t result;
  float x;

  node.preTransmission(preTransmission);
  node.postTransmission(postTransmission);
  result = node.readInputRegisters(m_startAddress, m_length);
  if (result == node.ku8MBSuccess) {
    Serial.print("V: ");    // address 0-1 
    ((byte*)&x)[3]= node.getResponseBuffer(0x00)>> 8;
    ((byte*)&x)[2]= node.getResponseBuffer(0x00)& 0xff;
    ((byte*)&x)[1]= node.getResponseBuffer(0x01)>> 8;
    ((byte*)&x)[0]= node.getResponseBuffer(0x01)& 0xff;
    Serial.println(x,2);
     return reform_uint16_2_float32(node.getResponseBuffer(0),node.getResponseBuffer(1));
  }
}  

void loop() {
  NG1 = getRTU(0x0000);
  Serial.println("Counter1 : " + String(NG1,2));
  delay(1000);
  NG2 = getRTU(0x0006);
  Serial.println("Counter2 : " + String(NG2,2));
  delay(1000);
  NG3 = getRTU(0x000C);
  Serial.println("Counter3 : " + String(NG3,2));
  delay(1000);
  Good = getRTU(0x0156);
  Serial.println("OK : " + String(Good,2));
  delay(1000);
//  PF = getRTU(0x001E);
//  Serial.println("Power Factor : " + String(PF,2));
//  delay(1000);
//  F = getRTU(0x0046);
//  Serial.println("Frequency : " + String(F,2));
//  delay(1000);
  Serial.println("==================================");
}
1 Like

See my previous question above. In fact re-read my post #5 and provide answers if you want anything more that guesses from other forum members.

I see the code above is also using Serial1. So your Arduino supports more than 1 hardware serial port?

Guessing now, but is your Arduino a MEGA2560 by any chance?

You have 2 software serial ports defined as well. Do you need these? Hardware serial ports are always a better choice.

Unless you have an auto switching RS485 module, these 2 functions need code in them to tell your RS485 module to switch to transmit mode and back to receive mode.

i am using ESP32 to read Integer data via RS485 connection to autonics CT6S-1T46

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.