Hello to all,
I am currently working on a project to recover analog information (temperature in particular...) from an Ascon controller, the X5 to be precise, in order to be able to do monitoring.
To communicate, the controller uses the Modbus protocol in RS485, I have at my disposal the specific Modbus table for my controller (see Modbus ascon from page 59 to page 75).
On the hardware side, I have an Arduino Mega and a RS485 TTL Shield (https://www.fiabiparkp.com/index.php?main_page=product_info∏ucts_id=529549).
I have pretty much understood how the Modbus protocol works:
you enter the address of the slave, the function you want it to perform, the address from which it starts to perform this function and then the number of registers to which it is applied.
For example, in my case if I want to read a value: Ox02 (address of my controller)
Ox03 or 04 ( to read) Ox40 ( 64 in decimal to read a status ) Ox 01 ( we read a register)
In my case, my arduino board works as a master while my controller works as a slave.
I come to my problem(s):
*The first one is the pure wiring (see wiring diagram), I don't know where to connect the RxD & TxD pins of my shield, for the moment they are at the level of pins 10 & 11 because I use the "software serial" library as well as if I have to wire my controller in Slave mode.
*The second one is on the software level, I'm a bit overwhelmed with the information and I don't know which libraries to use. For the moment, I use the serial software libraries and ModbusRtu.
Here is my current code:
#include <ModbusRtu.h>
#include <SoftwareSerial.h>
// data array for modbus network sharing
uint16_t au16data[16];
uint8_t u8state;
/**
* Modbus object declaration
* u8id : node id = 0 for master, = 1..247 for slave
* u8serno : serial port (use 0 for Serial)
* u8txenpin : 0 for RS-232 and USB-FTDI
* or any pin number > 1 for RS-485
*/
Modbus master(0); // this is master and RS-232 or USB-FTDI via software serial
/**
* This is an structe which contains a query to an slave device
*/
modbus_t telegram;
unsigned long u32wait;
SoftwareSerial mySerial(10, 11);//Create a SoftwareSerial object so that we can use software serial. Search "software serial" on Arduino.cc to find out more details.
void setup() {
Serial.begin(9600);//use the hardware serial if you want to connect to your computer via usb cable, etc.
master.begin( &mySerial, 9600 ); // begin the ModBus object. The first parameter is the address of your SoftwareSerial address. Do not forget the "&". 9600 means baud-rate at 9600
master.setTimeOut( 2000 ); // if there is no answer in 2000 ms, roll over
u32wait = millis() + 1000;
u8state = 0;
}
void loop() {
switch( u8state ) {
case 0:
if (millis() > u32wait) u8state++; // wait state
break;
case 1:
telegram.u8id = 0x02; // slave address
telegram.u8fct = 0x03; // function code (this one is registers read)
telegram.u16RegAdd = 0x40; // start address in slave
telegram.u16CoilsNo = 0x03; // number of elements (coils or registers) to read
telegram.au16reg = au16data; // pointer to a memory array in the Arduino
master.query( telegram ); // send query (only once)
u8state++;
break;
case 2:
master.poll(); // check incoming messages
if (master.getState() == COM_IDLE) {
u8state = 0;
u32wait = millis() + 2000;
Serial.println(au16data[0]);//Or do something else!
Serial.println(au16data[1]);//Or do something else!
Serial.println(au16data[2]);//Or do something else!
Serial.println(au16data[3]);//Or do something else!
Serial.println(au16data[4]);//Or do something else!
Serial.println(au16data[5]);//Or do something else!
Serial.println(au16data[6]);//Or do something else!
Serial.println(au16data[7]);//Or do something else!
Serial.println(au16data[8]);//Or do something else!
Serial.println(au16data[9]);//Or do something else!
Serial.println(au16data[10]);//Or do something else!
Serial.println(au16data[11]);//Or do something else!
Serial.println(au16data[12]);//Or do something else!
Serial.println(au16data[13]);//Or do something else!
Serial.println(au16data[14]);//Or do something else!
Serial.println(au16data[15]);//Or do something else!
}
break;
}
}
thank you in advance
Regards
Schéma cablage.pdf (168 KB)