Created modbus_transport_uart for bridging the nanomodbus library with HAL
This commit is contained in:
parent
174eacbbe1
commit
d707fdd988
31
Core/Src/modbus_transport_uart.c
Normal file
31
Core/Src/modbus_transport_uart.c
Normal file
@ -0,0 +1,31 @@
|
||||
#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;
|
||||
}
|
||||
@ -1 +1 @@
|
||||
Subproject commit 469b5e88319a303e1231890af06f2634b825eb27
|
||||
Subproject commit 8780d6b81f4943ae83fa5b0102402c6bd71e01a5
|
||||
Loading…
Reference in New Issue
Block a user