Modbus RTU/ASCII/TCP with lwip TCP working partial, but with errors (retransmitions)
Dependencies: EthernetNetIf mbed
portserial.cpp
00001 /* 00002 * FreeModbus Libary: BARE Port 00003 * Copyright (C) 2006 Christian Walter <wolti@sil.at> 00004 * 00005 * This library is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU Lesser General Public 00007 * License as published by the Free Software Foundation; either 00008 * version 2.1 of the License, or (at your option) any later version. 00009 * 00010 * This library is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 * Lesser General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU Lesser General Public 00016 * License along with this library; if not, write to the Free Software 00017 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00018 * 00019 * File: $Id: portserial.c,v 1.1 2006/08/22 21:35:13 wolti Exp $ 00020 */ 00021 00022 /* ----------------------- System includes ----------------------------------*/ 00023 #include "mbed.h" // Cam 00024 00025 /* ----------------------- Platform includes --------------------------------*/ 00026 #include "port.h" 00027 00028 /* ----------------------- Modbus includes ----------------------------------*/ 00029 #include "mb.h" 00030 #include "mbport.h" 00031 00032 DigitalOut RXLed(LED2); 00033 DigitalOut TXLed(LED3); 00034 Timeout RXTimeout; 00035 Timeout TXTimeout; 00036 00037 void RXTimeoutFunc(void) 00038 { 00039 RXLed = 0; 00040 } 00041 00042 void TXTimeoutFunc(void) 00043 { 00044 TXLed = 0; 00045 } 00046 00047 /* ----------------------- static functions ---------------------------------*/ 00048 static void prvvUARTTxReadyISR( void ); 00049 static void prvvUARTRxISR( void ); 00050 static void prvvUARTISR( void ); 00051 00052 /* ----------------------- System Variables ---------------------------------*/ 00053 Serial pc(USBTX, USBRX); // Cam - mbed USB serial port 00054 00055 Ticker simISR; // Cam - mbed ticker 00056 // we don't have the TX buff empty interrupt, so 00057 // we just interrupt every 1 mSec and read RX & TX 00058 // status to simulate the proper ISRs. 00059 00060 static BOOL RxEnable, TxEnable; // Cam - keep a static copy of the RxEnable and TxEnable 00061 // status for the simulated ISR (ticker) 00062 00063 00064 /* ----------------------- Start implementation -----------------------------*/ 00065 // Cam - This is called every 1mS to simulate Rx character received ISR and 00066 // Tx buffer empty ISR. 00067 static void 00068 prvvUARTISR( void ) 00069 { 00070 if (TxEnable) 00071 { 00072 if(pc.writeable()) 00073 prvvUARTTxReadyISR(); 00074 } 00075 if (RxEnable) 00076 { 00077 if(pc.readable()) 00078 prvvUARTRxISR(); 00079 } 00080 } 00081 00082 void 00083 vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable ) 00084 { 00085 /* If xRXEnable enable serial receive interrupts. If xTxENable enable 00086 * transmitter empty interrupts. 00087 */ 00088 RxEnable = xRxEnable; 00089 TxEnable = xTxEnable; 00090 } 00091 00092 BOOL 00093 xMBPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity ) 00094 { 00095 simISR.attach_us(&prvvUARTISR,1000); // Cam - attach prvvUARTISR to a 1mS ticker to simulate serial interrupt behaviour 00096 // 1mS is just short of a character time at 9600 bps, so quick enough to pick 00097 // up status on a character by character basis. 00098 return TRUE; 00099 } 00100 00101 BOOL 00102 xMBPortSerialPutByte( CHAR ucByte ) 00103 { 00104 /* Put a byte in the UARTs transmit buffer. This function is called 00105 * by the protocol stack if pxMBFrameCBTransmitterEmpty( ) has been 00106 * called. */ 00107 pc.putc( ucByte); 00108 TXLed = 1; 00109 TXTimeout.attach(TXTimeoutFunc, 0.020); 00110 return TRUE; 00111 } 00112 00113 BOOL 00114 xMBPortSerialGetByte( CHAR * pucByte ) 00115 { 00116 /* Return the byte in the UARTs receive buffer. This function is called 00117 * by the protocol stack after pxMBFrameCBByteReceived( ) has been called. 00118 */ 00119 * pucByte = pc.getc(); 00120 RXLed = 1; 00121 RXTimeout.attach(RXTimeoutFunc, 0.020); 00122 return TRUE; 00123 } 00124 00125 /* Create an interrupt handler for the transmit buffer empty interrupt 00126 * (or an equivalent) for your target processor. This function should then 00127 * call pxMBFrameCBTransmitterEmpty( ) which tells the protocol stack that 00128 * a new character can be sent. The protocol stack will then call 00129 * xMBPortSerialPutByte( ) to send the character. 00130 */ 00131 static void prvvUARTTxReadyISR( void ) 00132 { 00133 pxMBFrameCBTransmitterEmpty( ); 00134 } 00135 00136 /* Create an interrupt handler for the receive interrupt for your target 00137 * processor. This function should then call pxMBFrameCBByteReceived( ). The 00138 * protocol stack will then call xMBPortSerialGetByte( ) to retrieve the 00139 * character. 00140 */ 00141 static void prvvUARTRxISR( void ) 00142 { 00143 pxMBFrameCBByteReceived( ); 00144 } 00145 00146
Generated on Tue Jul 12 2022 21:29:48 by 1.7.2