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.
74 lines
2.0 KiB
74 lines
2.0 KiB
3 years ago
|
/**
|
||
|
* TODO file description
|
||
|
*/
|
||
|
|
||
|
#ifndef MODBUS_H
|
||
|
#define MODBUS_H
|
||
|
|
||
|
#include <stdint.h>
|
||
|
#include <stdbool.h>
|
||
|
#include <sys/types.h>
|
||
|
|
||
|
typedef enum ModbusException {
|
||
|
MB_EXCEPTION_OK = 0,
|
||
|
MB_EXCEPTION_ILLEGAL_FUNCTION = 1,
|
||
|
MB_EXCEPTION_ILLEGAL_DATA_ADDRESS = 2,
|
||
|
MB_EXCEPTION_ILLEGAL_DATA_VALUE = 3,
|
||
|
MB_EXCEPTION_SLAVE_DEVICE_FAILURE = 4,
|
||
|
// other codes exist but are not meaningful for simple slave devices
|
||
|
} ModbusException_t;
|
||
|
|
||
|
typedef enum ModbusFunction {
|
||
|
FC01_READ_COILS = 1,
|
||
|
FC02_READ_DISCRETES = 2,
|
||
|
FC03_READ_HOLDING_REGISTERS = 3,
|
||
|
FC04_READ_INPUT_REGISTERS = 4,
|
||
|
FC05_WRITE_SINGLE_COIL = 5,
|
||
|
FC06_WRITE_SINGLE_REGISTER = 6,
|
||
|
FC15_WRITE_MULTIPLE_COILS = 15,
|
||
|
FC16_WRITE_MULTIPLE_REGISTERS = 16,
|
||
|
FC22_MASK_WRITE_REGISTER = 22,
|
||
|
FC23_READ_WRITE_MULTIPLE_REGISTERS = 23,
|
||
|
} ModbusFunction_t;
|
||
|
|
||
|
typedef enum ModbusError {
|
||
|
MB_OK = 0,
|
||
|
MB_ERROR = 1,
|
||
|
MB_ERR_NOTFORME,
|
||
|
MB_ERR_NEEDMORE,
|
||
|
MB_ERR_CHECKSUM,
|
||
|
MB_ERR_BADPROTO,
|
||
|
} ModbusError_t;
|
||
|
|
||
|
typedef enum ModbusProtocol {
|
||
|
MB_PROTO_RTU,
|
||
|
MB_PROTO_TCP
|
||
|
} ModbusProtocol_t;
|
||
|
|
||
|
typedef struct ModbusSlave ModbusSlave_t;
|
||
|
|
||
|
struct ModbusSlave {
|
||
|
uint8_t addr;
|
||
|
ModbusProtocol_t proto;
|
||
|
void *userData;
|
||
|
enum ModbusException (*startOfAccess)(ModbusSlave_t *ms, ModbusFunction_t fcx, uint8_t slave_id);
|
||
|
void (*endOfAccess)(ModbusSlave_t *ms);
|
||
|
enum ModbusException (*readCoil)(ModbusSlave_t *ms, uint16_t reference, bool *value);
|
||
|
enum ModbusException (*readDiscrete)(ModbusSlave_t *ms, uint16_t reference, bool *value);
|
||
|
enum ModbusException (*readHolding)(ModbusSlave_t *ms, uint16_t reference, uint16_t *value);
|
||
|
enum ModbusException (*readInput)(ModbusSlave_t *ms, uint16_t reference, uint16_t *value);
|
||
|
enum ModbusException (*writeCoil)(ModbusSlave_t *ms, uint16_t reference, bool value);
|
||
|
enum ModbusException (*writeHolding)(ModbusSlave_t *ms, uint16_t reference, uint16_t value);
|
||
|
};
|
||
|
|
||
|
ModbusError_t mb_handleRequest(
|
||
|
ModbusSlave_t *ms,
|
||
|
const uint8_t *req,
|
||
|
size_t req_size,
|
||
|
uint8_t* resp,
|
||
|
size_t resp_capacity,
|
||
|
size_t *resp_size
|
||
|
);
|
||
|
|
||
|
#endif //MODBUS_H
|