- !! this packet: 1,16,22,2 used with rs485_read makes program crash
package to read and write on modbus rtu slaves using a serial connection.
This library is prepared to receive a decoded packet, calculate crc-16, build the frame, send, wait for response and return data received. In case of error, the error can be obtained calling getLastError method.
Run programs inside examples folder to check how it works
Request data from slave
chmod u+x make.sh
./make.sh
If needed uncomment DEBUG FLAGS in Makefile
Allow configurations for serial port
rs485_read method can only store MAX_VALUE_LEN bytes of retrieved payload (check modbus-rtu file to see macro value)
- @param unit_id - unit id
- @param fc - function code
- @param address
- @param len - number of registers to be read
- @param data - pointer to received payload
- @param size - maximum size of passed pointer, returns length of data read
- @return error - 0 in case of no error
static uint8_t rs485_read(uint8_t unit_id, uint8_t fc, uint16_t address, uint16_t len, uint8_t* data, uint16_t* size)
uint16_t size = 32;
uint8_t* data = (uint8_t*)malloc(size);
if(data != nullptr){
uint8_t error = modbusrtu.rs485_read(1,3,0,2,data,&size);
}
free(data);
- @param unit_id - unit id
- @param fc - function code
- @param address
- @param len - number of registers to be read
- @param data - pointer to payload to be written
- @param size - size of payload to be written
- @return error - 0 in case of no error
static uint8_t rs485_write(uint8_t unit_id, uint8_t fc, uint16_t address, uint16_t len, uint8_t* data, uint16_t* size)
uint16_t size = 32;
uint8_t* data = (uint8_t*)malloc(size);
if(data != nullptr){
data[0] = 0x00;
data[1] = 0x00;
data[2] = 0x00;
data[3] = loop_counter++;
uint8_t error = modbusrtu.rs485_write(1,16,0,2,data,&size);
}
free(data);
- @return error in string format
static String getLastError()
String error_msg = modbusrtu.getLastError();
if(error_msg != "")
Serial.println("error msg: "+error_msg);