搜索
bottom↓
回复: 0

freemodbus 485通讯会通不上 232正常

[复制链接]

出0入0汤圆

发表于 2020-5-18 17:35:56 | 显示全部楼层 |阅读模式
在线提问  freemodbus 485通讯开机的时候也能通上 但是过一段时间就不行了 使能脚也已经加上,要是485断开就都通不上,重新开机又能通上 但是过段时间不行,232正常
/* ----------------------- Start implementation -----------------------------*/
/**
  * @brief  控制接收和发送状态
  * @param  xRxEnable 接收使能、
  *         xTxEnable 发送使能
  * @retval None
  */
void
vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable )
{
    /* If xRXEnable enable serial receive interrupts. If xTxENable enable启用串口接收中断。如果xTxENable启用
     * transmitter empty interrupts.发送机空中断
     */       
        if(xRxEnable)
  {
                __HAL_UART_ENABLE_IT(&huart2,UART_IT_RXNE);   //使能接收和接收中断
                HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_RESET);       
                #if EN_USART1_RX==1                                                                         //MAX485操作 低电平为接收模式
                UART1_RX_MODE();
                #endif
//   printf("xRxEnable\n");
        }
  else
  {
                __HAL_UART_DISABLE_IT(&huart2,UART_IT_RXNE);
                HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_SET);       
                #if EN_USART1_RX==1                                                                     //MAX485操作 高电平为发送模式
                UART1_TX_MODE();
                #endif
//  printf("xRxEEEE\n");
        }
  if(xTxEnable)
  {   
                __HAL_UART_ENABLE_IT(&huart2,UART_IT_TXE);  //使能发送完成中断       
        }
  else
  {   
          __HAL_UART_DISABLE_IT(&huart2,UART_IT_TXE);     //禁止发送完成中断
  }
}

/**
  * @brief  串口初始化
  * @param  ucPORT      串口号
  *         ulBaudRate  波特率
  *         ucDataBits  数据位
  *         eParity     校验位
  * @retval None
  */
BOOL
xMBPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity )
{  
        static  uint32_t  ePar_ity;       
        (void)ucPORT;     //不修改串口
  (void)ucDataBits; //不修改数据位长度
        if (eParity ==MB_PAR_NONE)                                          //校验格式
        {
                ePar_ity= UART_PARITY_NONE;
        }else if (eParity ==MB_PAR_ODD)                                                
  {
                ePar_ity= UART_PARITY_ODD;
        }else if (eParity ==MB_PAR_EVEN)       
        {
          ePar_ity=UART_PARITY_EVEN;                       
  }                                                
        MX_USART2_UART_Init((uint32_t) ulBaudRate,ePar_ity);
  return TRUE;
}
/**
  * @brief  通过串口发送数据
  * @param  None
  * @retval None
  */
BOOL
xMBPortSerialPutByte( CHAR ucByte )
{
  /* Put a byte in the UARTs transmit buffer. This function is called在UARTs传输缓冲区中放置一个字节。这个函数被调用
   * by the protocol stack if pxMBFrameCBTransmitterEmpty( ) has been按照协议栈,如果pxMBFrameCBTransmitterEmpty( )已经发送数据
   * called. */

        #if EN_USART1_RX==1
        UART1_TX_MODE();
        #endif
        if(HAL_UART_Transmit(&huart2,(uint8_t*)&ucByte,1,0x05)!=HAL_OK)  //发送数据
  return FALSE;
  else
  return TRUE;
}
/**
  * @brief  从串口获得数据
  * @param  None
  * @retval None
  */
BOOL
xMBPortSerialGetByte( CHAR * pucByte )
{
  /* Return the byte in the UARTs receive buffer. This function is called将字节返回到UARTs receive接收缓冲区
   * by the protocol stack after pxMBFrameCBByteReceived( ) has been called.
   */       
        /**pucByte = USART_ReceiveData(USART1); */

        #if EN_USART1_RX==1
        UART1_RX_MODE();
        #endif         
        if(HAL_UART_Receive(&huart2,(uint8_t*)pucByte,1,0x05)!=HAL_OK)  //接收数据
  return FALSE;
  else
        return TRUE;
}
/* Create an interrupt handler for the transmit buffer empty interrupt
* (or an equivalent) for your target processor. This function should then
* call pxMBFrameCBTransmitterEmpty( ) which tells the protocol stack that
* a new character can be sent. The protocol stack will then call
* xMBPortSerialPutByte( ) to send the character.
*/
void prvvUARTTxReadyISR( void )
{       
  //pxMBFrameCBTransmitterEmpty = xMBRTUTransmitFSM //mb.c eMBInit函数中
  pxMBFrameCBTransmitterEmpty();  //发送状态机
}
/* Create an interrupt handler for the receive interrupt for your target
* processor. This function should then call pxMBFrameCBByteReceived( ). The
* protocol stack will then call xMBPortSerialGetByte( ) to retrieve the
* character.
*/
void prvvUARTRxISR( void )
{
  //pxMBFrameCBByteReceived = xMBRTUReceiveFSM        //mb.c eMBInit函数中
  pxMBFrameCBByteReceived();  //接收状态机
}


/**
  * @brief  USART1中断服务函数
  * @param  None
  * @retval None
*/

阿莫论坛20周年了!感谢大家的支持与爱护!!

月入3000的是反美的。收入3万是亲美的。收入30万是移民美国的。收入300万是取得绿卡后回国,教唆那些3000来反美的!
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-4-27 11:24

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表