portserial.c
#include "port.h"
#include "mb.h"
#include "mbport.h"
#define EVENT_SERIAL_TRANS_START (1<<0)
EventGroupHandle_t SlaveSerialSendEvent;
static void prvvUARTTxReadyISR(void);
static void prvvUARTRxISR(void);
BOOL xMBPortSerialInit(UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits,
eMBParity eParity)
{
(void)ucPORT;
(void)ucDataBits;
(void)eParity;
bsp_rs485_init(ulBaudRate);
SlaveSerialSendEvent = xEventGroupCreate();
return TRUE;
}
void vMBPortSerialEnable(BOOL xRxEnable, BOOL xTxEnable)
{
if(xRxEnable)
{
__HAL_UART_ENABLE_IT(&USART2_RS485Handler,USART_IT_RXNE);
RS485_RECV;
}
else
{
__HAL_UART_DISABLE_IT(&USART2_RS485Handler,USART_IT_RXNE);
RS485_SEND;
}
if(xTxEnable)
{
xEventGroupSetBits(SlaveSerialSendEvent,EVENT_SERIAL_TRANS_START);
}
else
{
xEventGroupWaitBits(SlaveSerialSendEvent,
EVENT_SERIAL_TRANS_START,
pdTRUE,
pdFALSE,
0);
}
}
void vMBPortClose(void)
{
}
BOOL xMBPortSerialPutByte(CHAR ucByte)
{
u8 ret = 0;
ret = HAL_UART_Transmit(&USART2_RS485Handler, (u8 *)(&ucByte),1,200);
return ret;
}
BOOL xMBPortSerialGetByte(CHAR * pucByte)
{
HAL_UART_Receive(&USART2_RS485Handler,(u8 *)pucByte,1,200);
return TRUE;
}
static void prvvUARTTxReadyISR(void)
{
pxMBFrameCBTransmitterEmpty();
}
static void prvvUARTRxISR(void)
{
pxMBFrameCBByteReceived();
}
void slaveSendIrq_task(void *pvParameters){
EventBits_t recved_event;
while (1)
{
recved_event = xEventGroupWaitBits(SlaveSerialSendEvent,
EVENT_SERIAL_TRANS_START,
pdFALSE,
pdFALSE,
portMAX_DELAY);
if(recved_event==EVENT_SERIAL_TRANS_START)
prvvUARTTxReadyISR();
}
}
#ifdef MODBUS_SLAVE_MODE
void USART2_IRQHandler(void)
{
if(__HAL_USART_GET_FLAG(&USART2_RS485Handler, USART_FLAG_RXNE) != RESET)
{
prvvUARTRxISR();
}
}
#endif
Modbus资料大全
笔者将Modbus 主机、从机协议完整资料整理如下:
文件 | 说明 |
---|
freemodbus-v1.6.zip | FreeModbusV1.6源码 | FreeModbusMasterSlave(Rev1.0.0).zip | 笔者移植好测试通过STM32+FreeRTOS+FreeModbus Master Slave代码 | ModbusSlave 7.0和ModbusPoll_7.0软件注册码.zip | FreeModbus Master Slave调试工具 | FreeModbus V1.6 主机使用说明.md | FreeModbus 主机使用说明文档 | FreeModbus 从机流程图.vsd | FreeModbus 从机流程图 | FreeModbus 主机流程图.vsd | FreeModbus 主机流程图 | Modbus应用协议.doc | 笔者整理的 FreeModbus 协议文档 | FreeModbus Datasheet | 笔者搜集 FreeModbus 官方协议文档 |
资料链接:👉FreeModbus资源传送门👈 资料介绍:👉FreeModbus资源介绍传送门👈
|