Classes | |
| struct | ModbusSensorParam |
| Defines the configuration parameters for an individual sensor. More... | |
| struct | ModbusSlaveParam |
| Global vector that stores the configuration and state of all discovered Modbus slaves. More... | |
| struct | ModbusRequestInfo |
| Context of an in-flight Modbus request. More... | |
| struct | ResponseFormat |
| Intermediate structure to pass data from the Modbus callback to tasks. More... | |
| struct | SensorSchedule |
| Planning element for sensor sampling. More... | |
Macros | |
| #define | RX_PIN 13 |
| < Utilidades de memoria (memcpy). | |
| #define | TX_PIN 12 |
| UART transmission pin for RS485. | |
| #define | MAX_MODBUS_RESPONSE_LENGTH 256 |
| Maximum expected length for a Modbus response. | |
Enumerations | |
| enum | DiscoveryOrder { DISCOVERY_GET_COUNT = 1 , DISCOVERY_GET_DATA_OFFSET = 255 , DISCOVERY_READ_SENSOR_PARAM = 8 } |
| Special commands used during the sensor discovery phase. More... | |
| enum | RequestType { REQUEST_UNKNOWN , REQUEST_DISCOVERY , REQUEST_SAMPLING } |
| Classification of the purpose of a Modbus request. More... | |
Functions | |
| void | addRequest (uint32_t token, uint8_t slaveId, uint8_t sensorId, uint8_t functionCode, RequestType type) |
| Registers a new request in the circular buffer. | |
| ModbusRequestInfo * | findRequestByToken (uint32_t token) |
| Searches for a pending request by its token. | |
| void | initScheduler () |
| Initializes or updates the scheduling list (Scheduler). | |
| void | handleData (ModbusMessage response, uint32_t token) |
| Callback executed upon receiving valid Modbus data. | |
| void | handleError (Error error, uint32_t token) |
| Callback executed when a Modbus error occurs. | |
| bool | discoverDeviceSensors (uint8_t deviceId) |
| Starts the discovery process for a specific device. | |
| void | initialDiscoveryTask (void *pvParameters) |
| One-shot task for the initial discovery of sensors. | |
| void | DataRequestScheduler (void *pvParameters) |
| Main task of the Scheduler. | |
| bool | getSensorParams (uint8_t slaveId, uint8_t sensorID, uint16_t &startAddr, uint16_t &numRegs) |
| Gets the configuration parameters of a specific sensor. | |
| uint8_t | getRegistersPerChannel (uint8_t slaveId, uint8_t sensorID) |
| Calculates the number of registers per channel for a sensor. | |
| void | parseAndStoreDiscoveryResponse (const ResponseFormat &response, uint8_t slaveId) |
| Parses the discovery response and updates the slave list. | |
| void | EventManager (void *pvParameters) |
| Event Manager task. | |
This module implements the Modbus RTU client (ESP32/TTGO) and the complete flow of:
Flow summary:
1) Modbus startup
2) Discovery
3) Periodic sampling
4) Modbus errors
Warnings and best practices:
| #define RX_PIN 13 |
< Utilidades de memoria (memcpy).
< Necesario para definiciones de tipos enteros de tamaño fijo (uint8_t, etc). < Estructura de datos para mapeo de sensores. < Utilizado para la generación de timestamps UNIX.
UART reception pin for RS485.
| enum DiscoveryOrder |
| enum RequestType |
| void addRequest | ( | uint32_t | token, |
| uint8_t | slaveId, | ||
| uint8_t | sensorId, | ||
| uint8_t | functionCode, | ||
| RequestType | type ) |
Registers a new request in the circular buffer.
| token | Unique identifier. |
| slaveId | Slave ID. |
| sensorId | Sensor ID. |
| functionCode | Modbus function. |
| type | Request type. |
| void DataRequestScheduler | ( | void * | pvParameters | ) |
Main task of the Scheduler.
Periodically checks which sensors should be sampled and generates events for the EventManager.
| bool discoverDeviceSensors | ( | uint8_t | deviceId | ) |
Starts the discovery process for a specific device.
| deviceId | Modbus ID of the device to query. |
| void EventManager | ( | void * | pvParameters | ) |
Event Manager task.
Centralizes requests from the queues (Scheduler and Peripheral) and converts them into actual Modbus transactions.
| ModbusRequestInfo * findRequestByToken | ( | uint32_t | token | ) |
Searches for a pending request by its token.
| token | Token to search for. |
| uint8_t getRegistersPerChannel | ( | uint8_t | slaveId, |
| uint8_t | sensorID ) |
Calculates the number of registers per channel for a sensor.
| bool getSensorParams | ( | uint8_t | slaveId, |
| uint8_t | sensorID, | ||
| uint16_t & | startAddr, | ||
| uint16_t & | numRegs ) |
Gets the configuration parameters of a specific sensor.
| slaveId | Slave ID. |
| sensorID | Sensor ID. |
| startAddr | Reference to return the start address. |
| numRegs | Reference to return the number of registers. |
| void handleData | ( | ModbusMessage | response, |
| uint32_t | token ) |
Callback executed upon receiving valid Modbus data.
| response | Object with the raw response. |
| token | Token of the original request. |
| void handleError | ( | Error | error, |
| uint32_t | token ) |
Callback executed when a Modbus error occurs.
Manages retry logic and removal of inactive slaves (Timeout).
| error | Error code. |
| token | Token of the failed request. |
| void initialDiscoveryTask | ( | void * | pvParameters | ) |
One-shot task for the initial discovery of sensors.
Iterates over dispositivosAConsultar, launches discovery, and then self-deletes.
| void initScheduler | ( | ) |
Initializes or updates the scheduling list (Scheduler).
Iterates through slaveList, calculates effective intervals based on channels and registers, and populates scheduleList. It is thread-safe using schedulerMutex.
| void parseAndStoreDiscoveryResponse | ( | const ResponseFormat & | response, |
| uint8_t | slaveId ) |
Parses the discovery response and updates the slave list.
Decodes the 8 parameter registers and creates or updates the entry in slaveList.
| response | Raw data received. |
| slaveId | ID of the slave that responded. |