Hello, I have a problem with reading the ds18b20 after the modbus rtu protocol. If it reads everything from the first data bus, everything works - it tests with the mbus.exe program and the fatek fbs-cm25 converter (I can set the scan rate to 10ms) but by adding each next bus I have to increase the scan rate up to 4000ms to eliminate errors. I will add that I am not a programmer.
#include <SimpleModbusSlave.h>
#include <OneWire.h>
#include <DallasTemperature.h>
//#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#define ONE_WIRE_BUS_1 3 //first bus for one ds18b20
#define ONE_WIRE_BUS_2 4 //second bus for one ds18b20
#define ONE_WIRE_BUS_3 5 //third bus for one ds18b20
#define ONE_WIRE_BUS_4 6 //fourth bus for one ds1b20
#define SENSOR_RESOLUTION 9 //set resoution for ds18b20
OneWire oneWire_in_gas(ONE_WIRE_BUS_1);
OneWire oneWire_out_gas(ONE_WIRE_BUS_2);
OneWire oneWire_in_ciecz(ONE_WIRE_BUS_3);
OneWire oneWire_out_ciecz(ONE_WIRE_BUS_4);
//LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
//LiquidCrystal_I2C lcd(0x27, 20, 4);
DallasTemperature sensor_gaz_we(&oneWire_in_gas);
DallasTemperature sensor_gaz_wy(&oneWire_out_gas);
DallasTemperature sensor_ciecz_we(&oneWire_in_ciecz);
DallasTemperature sensor_ciecz_wy(&oneWire_out_ciecz);
/*
SimpleModbusSlaveV10 supports function 3, 6 & 16.
This example code will receive the adc ch0 value from the arduino master.
It will then use this value to adjust the brightness of the led on pin 9.
The value received from the master will be stored in address 1 in its own
address space namely holdingRegs[].
In addition to this the slaves own adc ch0 value will be stored in
address 0 in its own address space holdingRegs[] for the master to
be read. The master will use this value to alter the brightness of its
own led connected to pin 9.
The modbus_update() method updates the holdingRegs register array and checks
communication.
Note:
The Arduino serial ring buffer is 64 bytes or 32 registers.
Most of the time you will connect the arduino to a master via serial
using a MAX485 or similar.
In a function 3 request the master will attempt to read from your
slave and since 5 bytes is already used for ID, FUNCTION, NO OF BYTES
and two BYTES CRC the master can only request 58 bytes or 29 registers.
In a function 16 request the master will attempt to write to your
slave and since a 9 bytes is already used for ID, FUNCTION, ADDRESS,
NO OF REGISTERS, NO OF BYTES and two BYTES CRC the master can only write
54 bytes or 27 registers.
Using a USB to Serial converter the maximum bytes you can send is
limited to its internal buffer which differs between manufactures.
*/
#define LED 9
// Using the enum instruction allows for an easy method for adding and
// removing registers. Doing it this way saves you #defining the size
// of your slaves register array each time you want to add more registers
// and at a glimpse informs you of your slaves register layout.
//////////////// registers of your slave ///////////////////
enum
{
// just add or remove registers and your good to go...
// The first register starts at address 0
ADC_VAL,
PWM_VAL,
PWM_VAL1,
PWM_VAL3,
PWM_VAL4,
PWM_VAL5,
PWM_VAL6,
PWM_VAL7,
PWM_VAL8,
HOLDING_REGS_SIZE // leave this one
// total number of registers for function 3 and 16 share the same register array
// i.e. the same address space
};
unsigned int holdingRegs[HOLDING_REGS_SIZE]; // function 3 and 16 register array
////////////////////////////////////////////////////////////
void setup()
{
// lcd.begin(20,4);
// lcd.clear();
// lcd.print(" inicjalizacja ");
// lcd.setCursor(0,1);
// lcd.print(" prosze czekac ");
// delay(3000);
sensor_gaz_we.begin();
sensor_gaz_wy.begin();
sensor_ciecz_we.begin();
sensor_ciecz_wy.begin();
sensor_gaz_we.setResolution (SENSOR_RESOLUTION);
sensor_gaz_wy.setResolution (SENSOR_RESOLUTION);
sensor_ciecz_we.setResolution (SENSOR_RESOLUTION);
sensor_ciecz_wy.setResolution (SENSOR_RESOLUTION);
/* parameters(HardwareSerial* SerialPort,
long baudrate,
unsigned char byteFormat,
unsigned char ID,
unsigned char transmit enable pin,
unsigned int holding registers size,
unsigned int* holding register array)
*/
/* Valid modbus byte formats are:
SERIAL_8N2: 1 start bit, 8 data bits, 2 stop bits
SERIAL_8E1: 1 start bit, 8 data bits, 1 Even parity bit, 1 stop bit
SERIAL_8O1: 1 start bit, 8 data bits, 1 Odd parity bit, 1 stop bit
You can obviously use SERIAL_8N1 but this does not adhere to the
Modbus specifications. That said, I have tested the SERIAL_8N1 option
on various commercial masters and slaves that were suppose to adhere
to this specification and was always able to communicate... Go figure.
These byte formats are already defined in the Arduino global name space.
*/
modbus_configure(&Serial, 9600, SERIAL_8N1, 1, 2, HOLDING_REGS_SIZE, holdingRegs);
// modbus_update_comms(baud, byteFormat, id) is not needed but allows for easy update of the
// port variables and slave id dynamically in any function.
modbus_update_comms(9600, SERIAL_8N1, 4);
pinMode(LED, OUTPUT);
}
void loop()
{
sensor_gaz_we.requestTemperatures();
sensor_gaz_wy.requestTemperatures();
sensor_ciecz_we.requestTemperatures();
sensor_ciecz_wy.requestTemperatures();
float temp_gaz_we = sensor_gaz_we.getTempCByIndex(0);
float temp_gaz_wy = sensor_gaz_wy.getTempCByIndex(0);
float temp_ciecz_we = sensor_ciecz_we.getTempCByIndex(0);
float temp_ciecz_wy = sensor_ciecz_wy.getTempCByIndex(0);
modbus_update();
holdingRegs[1] = temp_gaz_we;
holdingRegs[2] = temp_gaz_wy;
holdingRegs[3] = temp_ciecz_we;
holdingRegs[4] = temp_ciecz_wy;
}