Air quality sensor
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
esp-airsensor/main/modbus_fn.h

85 lines
2.4 KiB

/**
* TODO file description
*
* Created on 2022/01/02.
*/
#ifndef ESPNODE_MODBUS_FN_H
#define ESPNODE_MODBUS_FN_H
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
/**
* Build "read holding registers" packet
*
* @param buf buf to write to
* @param len buf len
* @param resplen expected response length (if not exception)
* @param mbAddr mb address
* @param ref first register number
* @param qty register quantity
* @return number of buf bytes if success, negative if error
*/
int mb_build_fc3(uint8_t *buf, size_t len, size_t *resplen, uint8_t mbAddr, uint16_t ref, uint16_t qty);
/**
* Parse "read holding registers" response
*
* @param buf buf with the response bytes
* @param len response buf len
* @param dst store results here
* @param capacity capacity of "dst"
* @return num of results, negative if error
*/
int mb_parse_fc3(const uint8_t *buf, size_t len, uint16_t *dst, size_t capacity);
/**
* Build "read input registers" packet
*
* @param buf buf to write to
* @param len buf len
* @param resplen expected response length (if not exception)
* @param mbAddr mb address
* @param ref first register number
* @param qty register quantity
* @return number of buf bytes if success, negative if error
*/
int mb_build_fc4(uint8_t *buf, size_t len, size_t *resplen, uint8_t mbAddr, uint16_t ref, uint16_t qty);
/**
* Parse "read input registers" response
*
* @param buf buf with the response bytes
* @param len response buf len
* @param dst store results here
* @param capacity capacity of "dst"
* @return num of results, negative if error
*/
int mb_parse_fc4(const uint8_t *buf, size_t len, uint16_t *dst, size_t capacity);
/**
* Build "write holding registers" packet
*
* @param buf buf to write to
* @param len buf len
* @param resplen expected response length (if not exception)
* @param mbAddr mb address
* @param ref first register number
* @param data register data to write, as an array
* @param qty register quantity
* @return number of buf bytes if success, negative if error
*/
int mb_build_fc16(uint8_t *buf, size_t len, size_t *resplen, uint8_t mbAddr, uint16_t ref, const uint16_t *data, uint16_t qty);
/**
* Parse "write holding registers" response
*
* @param buf buf with the response bytes
* @param len response buf len
* @return 0 if success, negative if error
*/
bool mb_parse_fc16(const uint8_t *buf, size_t len);
#endif //ESPNODE_MODBUS_FN_H