Procházet zdrojové kódy

完成发送消息的对接

mysterywolf před 5 roky
rodič
revize
b27fe46861
4 změnil soubory, kde provedl 50 přidání a 158 odebrání
  1. 1 71
      Source/mb.c
  2. 0 17
      Source/mb.h
  3. 38 59
      Source/mb_bsp.c
  4. 11 11
      readme.md

+ 1 - 71
Source/mb.c

@@ -477,7 +477,7 @@ void  MB_RxByte (MODBUS_CH  *pch,
     }
 }
 
- 
+
 /*
 *********************************************************************************************************
 *                                              MB_RxTask()
@@ -504,78 +504,8 @@ void  MB_RxTask (MODBUS_CH *pch)
     }
 #endif
 }
-/*
-*********************************************************************************************************
-*                                                MB_Tx()
-*
-* Description : This function is called to start transmitting a packet to a modbus channel.
-*
-* Argument(s) : pch      Is a pointer to the Modbus channel's data structure.
-*
-* Return(s)   : none.
-*
-* Caller(s)   : MB_ASCII_Tx(),
-*               MB_RTU_Tx().
-*
-* Note(s)     : none.
-*********************************************************************************************************
-*/
 
-void  MB_Tx (MODBUS_CH  *pch)
-{
-    pch->TxBufPtr = &pch->TxBuf[0];
-    MB_TxByte(pch);
-    MB_CommRxIntDis(pch);
-    MB_CommTxIntEn(pch);
-}
 
- 
-/*
-*********************************************************************************************************
-*                                              MB_TxByte()
-*
-* Description : This function is called to obtain the next byte to send from the transmit buffer.  When
-*               all bytes in the reply have been sent, transmit interrupts are disabled and the receiver
-*               is enabled to accept the next Modbus request.
-*
-* Argument(s) : pch      Is a pointer to the Modbus channel's data structure.
-*
-* Return(s)   : none.
-*
-* Caller(s)   : MB_CommRxTxISR_Handler(),
-*               MB_Tx().
-*
-* Note(s)     : none.
-*********************************************************************************************************
-*/
-
-void  MB_TxByte (MODBUS_CH  *pch)
-{
-    CPU_INT08U  c;
-
-
-    if (pch->TxBufByteCtr > 0) {
-        pch->TxBufByteCtr--;
-        pch->TxCtr++;
-        c = *pch->TxBufPtr++;
-        MB_CommTx1(pch,                                         /* Write one byte to the serial port                  */
-                   c);
-#if (MODBUS_CFG_MASTER_EN == DEF_ENABLED)
-        if (pch->MasterSlave == MODBUS_MASTER) {
-#if (MODBUS_CFG_RTU_EN == DEF_ENABLED)
-            pch->RTU_TimeoutEn = MODBUS_FALSE;                  /* Disable RTU timeout timer until we start receiving */
-#endif
-            pch->RxBufByteCtr  = 0;                             /* Flush Rx buffer                                    */
-        }
-#endif
-    } else {                                                    /* If there is nothing to do end transmission         */
-        pch->TxBufPtr = &pch->TxBuf[0];                         /* Reset at beginning of buffer                       */
-        MB_CommTxIntDis(pch);                                   /* No more data to send, disable Tx interrupts        */
-        MB_CommRxIntEn(pch);                                    /* Re-enable the receiver for the next packet         */
-    }
-}
-
- 
 /*
 *********************************************************************************************************
 *                                           MB_ASCII_RxByte()

+ 0 - 17
Source/mb.h

@@ -437,28 +437,11 @@ void         MB_CommPortCfg             (MODBUS_CH   *pch,
                                          CPU_INT08U   parity,
                                          CPU_INT08U   stops);
 
-void         MB_CommRxTxISR_0_Handler   (void);
-void         MB_CommRxTxISR_1_Handler   (void);
-void         MB_CommRxTxISR_2_Handler   (void);
-void         MB_CommRxTxISR_3_Handler   (void);
-void         MB_CommRxTxISR_4_Handler   (void);
-void         MB_CommRxTxISR_5_Handler   (void);
-void         MB_CommRxTxISR_6_Handler   (void);
-void         MB_CommRxTxISR_7_Handler   (void);
-void         MB_CommRxTxISR_8_Handler   (void);
-void         MB_CommRxTxISR_9_Handler   (void);
 
 void         MB_CommRxIntEn             (MODBUS_CH   *pch);           /* Enable  Rx interrupts                                        */
 
 void         MB_CommRxIntDis            (MODBUS_CH   *pch);           /* Disable Rx interrupts                                        */
 
-void         MB_CommTx1                 (MODBUS_CH   *pch,
-                                         CPU_INT08U   c);
-
-void         MB_CommTxIntEn             (MODBUS_CH   *pch);           /* Enable  Tx interrupts                                        */
-
-void         MB_CommTxIntDis            (MODBUS_CH   *pch);           /* Disable Tx interrupts                                        */
-
 
 #if (MODBUS_CFG_RTU_EN == DEF_ENABLED)
 void         MB_RTU_TmrInit             (void);                       /* Initialize the timer used for RTU framing                    */

+ 38 - 59
Source/mb_bsp.c

@@ -199,112 +199,91 @@ CPU_VOID  MB_CommPortCfg (MODBUS_CH  *pch,
 
 /*
 *********************************************************************************************************
-*                                         MB_CommRxIntDis()
-*
-* Description : This function disables Rx interrupts.
-*
-* Argument(s) : pch        is a pointer to the Modbus channel
-*
-* Return(s)   : none.
-*
-* Caller(s)   : MB_CommExit()
-*
-* Note(s)     : none.
-*********************************************************************************************************
-*/
-
-void  MB_CommRxIntDis (MODBUS_CH  *pch)
-{
-}
-
-/*
-*********************************************************************************************************
-*                                          MB_CommRxIntEn()
+*                                                MB_Tx()
 *
-* Description : This function enables Rx interrupts.
+* Description : This function is called to start transmitting a packet to a modbus channel.
 *
-* Argument(s) : pch        is a pointer to the Modbus channel
+* Argument(s) : pch      Is a pointer to the Modbus channel's data structure.
 *
 * Return(s)   : none.
 *
-* Caller(s)   : MB_TxByte()
+* Caller(s)   : MB_ASCII_Tx(),
+*               MB_RTU_Tx().
 *
 * Note(s)     : none.
 *********************************************************************************************************
 */
-
-void  MB_CommRxIntEn (MODBUS_CH  *pch)
+/* combine MB_Tx() and MB_TxByte() */
+void  MB_Tx (MODBUS_CH  *pch)
 {
+    char uart_dev_name[RT_NAME_MAX]={0};
+    rt_device_t uart_dev;
+    
+    pch->TxBufPtr = &pch->TxBuf[0];
+    if (pch->TxBufByteCtr > 0) {
+#if (MODBUS_CFG_MASTER_EN == DEF_ENABLED)
+        if (pch->MasterSlave == MODBUS_MASTER) {
+#if (MODBUS_CFG_RTU_EN == DEF_ENABLED)
+            pch->RTU_TimeoutEn = MODBUS_FALSE;                  /* Disable RTU timeout timer until we start receiving */
+#endif
+            pch->RxBufByteCtr  = 0;                             /* Flush Rx buffer                                    */
+        }
+#endif
+        rt_snprintf(uart_dev_name, RT_NAME_MAX, "uart%d", pch->PortNbr);   
+        uart_dev = rt_device_find(uart_dev_name);
+        if(uart_dev == (rt_device_t)0){
+            return;
+        }
+        
+        rt_device_write(uart_dev,0,pch->TxBufPtr,pch->TxBufByteCtr); /* send a message */
+        
+        /* end of transmission */
+        pch->TxCtr = pch->TxBufByteCtr;
+        pch->TxBufByteCtr = 0;
+    }
 }
 
 /*
 *********************************************************************************************************
-*                                         MB_CommTxIntDis()
+*                                         MB_CommRxIntDis()
 *
-* Description : This function disables Tx interrupts.
+* Description : This function disables Rx interrupts.
 *
 * Argument(s) : pch        is a pointer to the Modbus channel
 *
 * Return(s)   : none.
 *
 * Caller(s)   : MB_CommExit()
-*               MB_TxByte()
 *
 * Note(s)     : none.
 *********************************************************************************************************
 */
 
-void  MB_CommTxIntDis (MODBUS_CH  *pch)
+void  MB_CommRxIntDis (MODBUS_CH  *pch)
 {
 }
 
 /*
 *********************************************************************************************************
-*                                         MB_CommTxIntEn()
+*                                          MB_CommRxIntEn()
 *
-* Description : This function enables Tx interrupts.
+* Description : This function enables Rx interrupts.
 *
 * Argument(s) : pch        is a pointer to the Modbus channel
 *
 * Return(s)   : none.
 *
-* Caller(s)   : MB_Tx()
-*
-* Note(s)     : none.
-*********************************************************************************************************
-*/
-
-void  MB_CommTxIntEn (MODBUS_CH  *pch)
-{
-}
-
-/*
-*********************************************************************************************************
-*                                             MB_CommTx1()
-*
-* Description : This function is called to obtain the next byte to send from the transmit buffer.  When
-*               all bytes in the reply have been sent, transmit interrupts are disabled and the receiver
-*               is enabled to accept the next Modbus request.
-*
-* Argument(s) : c     is the byte to send to the serial port
-*
-* Return(s)   : none.
-*
 * Caller(s)   : MB_TxByte()
 *
 * Note(s)     : none.
 *********************************************************************************************************
 */
 
-void  MB_CommTx1 (MODBUS_CH  *pch,
-                  CPU_INT08U  c)
-
+void  MB_CommRxIntEn (MODBUS_CH  *pch)
 {
 }
 
 
-
-
 /*
 *********************************************************************************************************
 *                                           MB_RTU_TmrInit()

+ 11 - 11
readme.md

@@ -12,17 +12,17 @@ https://github.com/SiliconLabs/uC-Modbus
 
 ## 串口设备移植文件`mb_bsp.c`
 
-|     函数名称      | 说明 |
-| :---------------: | :--: |
-|   MB_CommExit()   |      |
-| MB_CommPortCfg()  |      |
-| MB_CommRxIntDis() |      |
-| MB_CommRxIntEn()  |      |
-|   MB_CommTx1()    |      |
-| MB_CommTxIntDis() |      |
-| MB_CommTxIntEn()  |      |
-| MB_RTU_TmrExit()  |      |
-| MB_RTU_TmrInit()  |      |
+|     函数名称      |     说明     |
+| :---------------: | :----------: |
+|   MB_CommExit()   |              |
+| MB_CommPortCfg()  |              |
+| MB_CommRxIntDis() |              |
+| MB_CommRxIntEn()  |              |
+|   MB_CommTx1()    | 无用,已删除 |
+| MB_CommTxIntDis() | 无用,已删除 |
+| MB_CommTxIntEn()  | 无用,已删除 |
+| MB_RTU_TmrExit()  |              |
+| MB_RTU_TmrInit()  |              |