32 lines
795 B
C
32 lines
795 B
C
#include "nanomodbus.h"
|
|
#include "main.h"
|
|
#include "usart.h" // for &huart2
|
|
|
|
static uint8_t rx_buf[256];
|
|
static uint8_t tx_buf[256];
|
|
|
|
// nanoMODBUS requires this structure:
|
|
mb_port_t modbus_port;
|
|
|
|
// Read function (blocking or interrupt/DMA based)
|
|
int modbus_read(uint8_t *dest, uint16_t len, uint32_t timeout_ms) {
|
|
if (HAL_UART_Receive(&huart2, dest, len, timeout_ms) == HAL_OK) {
|
|
return len;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
// Write function
|
|
int modbus_write(const uint8_t *src, uint16_t len, uint32_t timeout_ms) {
|
|
if (HAL_UART_Transmit(&huart2, (uint8_t*)src, len, timeout_ms) == HAL_OK) {
|
|
return len;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
// Register your transport functions
|
|
void modbus_transport_init(void) {
|
|
modbus_port.read = modbus_read;
|
|
modbus_port.write = modbus_write;
|
|
}
|