Error code while trying to receive data with RS485/Modbus RTU to arduino - from arduino to CanBus

Hi.I would appreciate it if you could help me solve my problem.
I'm trying to receive mass flow data from a hydrogen coriolis.(mass flow address :30004 or 0x7534).Im currently using rs485 wth arduino.
This is the code ı'm using:

#include <ModbusMaster.h>

/*!
  Rx/Tx is hooked up to the hardware serial port at 'Serial'.
  The Data Enable and Receiver Enable pins are hooked up as follows:
*/
#define MAX485_DE      8
#define MAX485_RE_NEG  7

// initiating modbus prject
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);
  
  digitalWrite(MAX485_RE_NEG, 0);
  digitalWrite(MAX485_DE, 0);

  // Modbus communication runs at 9600 baud
  Serial.begin(9600);

  // Modbus slave ID 1
  node.begin(1, Serial);
  
  node.preTransmission(preTransmission);
  node.postTransmission(postTransmission);
}

bool state = true;

void loop()
{
  uint8_t result;
  uint16_t data[32];
  
  
  result = node.writeSingleCoil(0x0002, state);
  state = !state;
      

  // Read 16 registers starting at 30000)
  result = node.readHoldingRegisters(0x7530,16);
      

  if (result == node.ku8MBSuccess)
  {

    Serial.print("mass flow: ");
    Serial.println(node.getResponseBuffer(0x04));  //flow mass address 30004
    Serial.print("temp: ");
    Serial.println(node.getResponseBuffer(0x06));  //temperature address 30006
    
  }
    Serial.println("\n ------------  \n ");

  delay(1000);
}


/*

  RS485_HalfDuplex.pde - example using ModbusMaster library to communicate
  with EPSolar LS2024B controller using a half-duplex RS485 transceiver.

  This example is tested against an EPSolar LS2024B solar charge controller.
  See here for protocol specs:
  http://www.solar-elektro.cz/data/dokumenty/1733_modbus_protocol.pdf

  Library:: ModbusMaster
  Author:: Marius Kintel <marius at kintel dot net>

  Copyright:: 2009-2016 Doc Walker

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

*/


Thank you guys in advance.

Welcome to the forum

How is the Arduino connected to the device ?

Thank you.


left side which says a-b pins is connected to device(coriolis).

Use a software serial port on your UNO - see library manager - to do the modbus RS485 Comms.

It seems that you have the RS485 pins on the device connected directly to the pins on the Arduino. If that is the case then it will not work because the two are not compatible

You need a voltage/polarity converter between the two

You mean a-b pins or the power pins ?Can you give me a more detailed information.


This is the error ı receive right now.
The odd thing is when ı directly connect device to pc through usb to rs485 converter ı can read data on modbus poll so ı'm assuming there's something wrong with the code.

I've already done that but no response so far.Thanks for the answer though.

I cannot tell from your 'photo, which is why a schematic would be more helpful, what exactly is the long board that is connected to the Uno ? What is connected to the two wires on the left of the 'photo ?

Please provide a link to the long board ?

the one connected to uno is max485,and the left of the photo which has two wires connected to device (A to A - B to B)

30004 is input register, not holding register. 4th register, so 0x03 in hex.
Also, like already mentioned here, you need to use softwareserial with Uno. And not hardwareserial pins.

You said that ı need a voltage/polarity converter but the device is connected to power directly withut arduino so its not poweredby arduino.Do ı still need the voltage converter?


This is the error ı'm currently encountering right now.

Here is the Final code ı'm using:

#include <ModbusMaster.h>

#define MAX485_DE      8
#define MAX485_RE_NEG  7
#define MAX485_DI      6
#define MAX485_RO      5

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);
  
  digitalWrite(MAX485_RE_NEG, 0);
  digitalWrite(MAX485_DE, 0);

  Serial.begin(9600);

  node.begin(1, Serial);
  
  node.preTransmission(preTransmission);
  node.postTransmission(postTransmission);
}

void loop()
{
  uint8_t result;
  
  // Adjust to read input registers starting from address 30000
  result = node.readInputRegisters(0x7530, 8);
  
  if (result == node.ku8MBSuccess)
  {
    Serial.print("mass flow: ");
    Serial.println(node.getResponseBuffer(0x04));  // mass flow address 30004
    Serial.print("temp: ");
    Serial.println(node.getResponseBuffer(0x06));  // temperature address 30006
  } 
  else 
  {
    Serial.print("Modbus Error: ");
    Serial.println(result);
  }

  Serial.println("\n ------------  \n ");

  delay(1000);
}

Also ı tried to change it from 0x04 to 0x03 as you said Kmin but nothing's changed.

You still use hardwareserial.

My bad.I've changed it.

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

// Define software serial RX and TX pins
#define RX_PIN 2
#define TX_PIN 3

#define MAX485_DE      8
#define MAX485_RE_NEG  7

// Create a SoftwareSerial instance
SoftwareSerial mySerial(RX_PIN, TX_PIN);

ModbusMaster node;

void preTransmission()
{
  digitalWrite(MAX485_RE_NEG, 1); // DE = 1 to enable driver (transmit mode)
  digitalWrite(MAX485_DE, 1);
}

void postTransmission()
{
  digitalWrite(MAX485_RE_NEG, 0); // RE = 0 to enable receiver (receive mode)
  digitalWrite(MAX485_DE, 0);
}

void setup()
{
  pinMode(MAX485_RE_NEG, OUTPUT);
  pinMode(MAX485_DE, OUTPUT);
  
  digitalWrite(MAX485_RE_NEG, 0);
  digitalWrite(MAX485_DE, 0);

  mySerial.begin(9600); // Initialize software serial port

  node.begin(1, mySerial); // Use software serial for Modbus communication
  
  node.preTransmission(preTransmission);
  node.postTransmission(postTransmission);
}

void loop()
{
  uint8_t result;
  
  // Adjust to read input registers starting from address 30000
  result = node.readInputRegisters(0x7530, 16);
  
  if (result == node.ku8MBSuccess)
  {
    mySerial.print("mass flow: ");
    mySerial.println(node.getResponseBuffer(0x04));  // mass flow address 30004
    mySerial.print("temp: ");
    mySerial.println(node.getResponseBuffer(0x06));  // temperature address 30006
  } 
  else 
  {
    mySerial.print("Modbus Error: ");
    mySerial.println(result);
  }

  mySerial.println("\n ------------  \n ");

  delay(1000);
}

I've connected DI to 3 and RO to 2
İs there anything ı'm missing ?

you shouldn't mix Modbus and the debug outputs on the serial monitor.

One way would be:
use SoftSerial for your modbus
use HW Serial for your debug messages

when you link to the product you want to connect and provide a link to it's datasheet, we can check if your commands are correct.

Screenshot 2024-08-16 114649
ı'm trying to read mass flow and temp values.

please provide

  • link to the product.
  • link to the datasheet

for the product:

datasheet:

According to page 6, you have configure your modbus settings on display menu.
Set slave address to 1, baudrate to 9600 and parity to No

Modbus register addresses are presented strange way on that manual.
On your code, try either 0x00 or 0x7530

yes it's set that way

If you get weird symbols and error 226, either your wiring is incorrect, or those parameters are incorrect.
Where is Rs485 module RO pin going to?

Edit:

In your updated code, you don't have correct serial with serial monitor!