Modbus RTU (RS485) receiving data/ reading the received data

I"m trying to receive data and use the received data. As far as i know i receive some data, or atleast have an connection with the slave. I can see the TX and RX indication leds blink while connected to the slave, and RX doesn’t blink while not connected. So ain’t sure if i receive some data.

For trying to read the (read) register i used the code:

mySerial.write(regs[0]);

For the monitoring of the mySerial i used an serial to ttl converted cause the RS485 uses the TX and RX, so i won’t be able to use the Serial monitor over usb cause that chips used the serial port (pin TX and RX) as far of my ankowledge. The ttl signal will be read to labview (tested and working at other projects and "test"commands).

anyone who can help me with reading the register, or receiving the data if that’s the problem?

Hardware:

  • Arduino uno
  • Arduino w5100 Shield (will be used so send the data to an server, i will be working on that after the modbus connection is done)
  • RS485 Shield from joy-it with jumpers at (RX = D0, TX = D1, TX_CTRL and 5V)
  • Custom Slave board

specs Custom Slave board:
(Everything is working with an RS485 connection with an PLC)

  • ModBus RTU
  • baud: 57600
  • Even Parity
  • 1 stop-bit
  • Sensors where i wanne read the data from are within the input register

The code for the Arduino master

#include <SimpleModbusMaster.h>
#include <SoftwareSerial.h>

 
/*
   The example will use packet1 to read a register from address 0 (the adc ch0 value)
   from the arduino slave (id=1). It will then use this value to adjust the brightness
   of an led on pin 9 using PWM.
   It will then use packet2 to write a register (its own adc ch0 value) to address 1 
   on the arduino slave (id=1) adjusting the brightness of an led on pin 9 using PWM.
*/

//////////////////// Port information ///////////////////
#define baud 57600
#define timeout 2000
#define polling 200 // the scan rate
#define retry_count 10

// used to toggle the receive/transmit pin on the driver
#define TxEnablePin 2 

// The total amount of available memory on the master to store data
#define TOTAL_NO_OF_REGISTERS 7

// This is the easiest way to create new packets
// Add as many as you want. TOTAL_NO_OF_PACKETS
// is automatically updated.
enum
{
  PACKET1,
  PACKET2,
  TOTAL_NO_OF_PACKETS // leave this last entry
};

// Create an array of Packets to be configured
Packet packets[TOTAL_NO_OF_PACKETS];

SoftwareSerial mySerial(10, 11); // RX, TX

// Masters register array
unsigned int regs[TOTAL_NO_OF_REGISTERS];

//Timers
unsigned long previoustime = 0;
const long interval = 250;     // interval logging
unsigned long currenttime;

void setup()
{
 mySerial.begin(57600);
  // Initialize each packet
  modbus_construct(&packets[PACKET1], 1, READ_INPUT_REGISTERS, 0, 7, 0);
  
  // Initialize the Modbus Finite State Machine
  modbus_configure(&Serial, baud, SERIAL_8E1, timeout, polling, retry_count, TxEnablePin, packets, TOTAL_NO_OF_PACKETS, regs);
  
}

void loop()
{
   unsigned long currenttime = millis ();
   
  if (currenttime - previoustime >= interval)  {
  previoustime = currenttime; 
  modbus_update();
  
  mySerial.write(regs[0]);

  }
}

SimpleModbusMasterManual.pdf (275 KB)

Hello,

Sorry but I did not understand, how would you be using "write" in a reading function?

mySerial.write(regs[0]);

Have you ever tried to run the "serial event" example, have you understood how it works?

[Tutorials > Built-In Examples > 04.Communication > SerialEvent]

What is/isn’t happening that you think shouldn’t/should be? Assuming your configuration is correct, and you say it works with PLC, there does not appear to be anything wrong with the code you’ve posted. The only issue might be that, according to the manual, you may be dealing with 32-bit data and regs[0] holds only the 2 MSBs of that value. You may need,

uint32_t temp = regs[0] << 16;
temp |= regs[1];

to get meaningful data.

Hey,

code:

 mySerial.write(regs[0]);

I use this code for sending data or etc towards the usb ttl converted, where labview is watching. So it isn’t really reading the register. It was an way i tried to get some information out of the register, which didn’t work, and so the reason for this post.

code:

  uint32_t temp = regs[0]<<16;
  temp |= regs[1];
  mySerial.write("hello");
  mySerial.write(temp);

I’ve tested this only without succes. While only writing the temp data toward the labview i receive nothing. When i also write any kind of tekst like “hello” i receive something. Like hello\A8 (HEX 6865 6C6C 6FA8) . So still no usefull data.

Any help on where it is going wrong or what i should to get some use full data, do will be appreciated.
(things i would like to get, some data out of an modbus RTU slave, and sending that data over tcp toward labview. For testing i use an ttl serial monitor on labview to get the data out of the register from the slave.)