from Cam Marshall original modbus

Dependencies:   mbed

Fork of Modbus by Cam Marshall

Committer:
stanley1228
Date:
Tue Mar 14 14:25:27 2017 +0800
Revision:
4:5000041d2dc2
Parent:
2:5f360876388e
Child:
5:c712a10c9414
use Qmodbus to test can read input register
1.but need to test more high speed baudrate
2.check timeout

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cam 0:0453a0a7e500 1 /*
cam 0:0453a0a7e500 2 * FreeModbus Libary: BARE Port
cam 0:0453a0a7e500 3 * Copyright (C) 2006 Christian Walter <wolti@sil.at>
cam 0:0453a0a7e500 4 *
cam 0:0453a0a7e500 5 * This library is free software; you can redistribute it and/or
cam 0:0453a0a7e500 6 * modify it under the terms of the GNU Lesser General Public
cam 0:0453a0a7e500 7 * License as published by the Free Software Foundation; either
cam 0:0453a0a7e500 8 * version 2.1 of the License, or (at your option) any later version.
cam 0:0453a0a7e500 9 *
cam 0:0453a0a7e500 10 * This library is distributed in the hope that it will be useful,
cam 0:0453a0a7e500 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
cam 0:0453a0a7e500 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
cam 0:0453a0a7e500 13 * Lesser General Public License for more details.
cam 0:0453a0a7e500 14 *
cam 0:0453a0a7e500 15 * You should have received a copy of the GNU Lesser General Public
cam 0:0453a0a7e500 16 * License along with this library; if not, write to the Free Software
cam 0:0453a0a7e500 17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
cam 0:0453a0a7e500 18 *
cam 0:0453a0a7e500 19 * File: $Id: portserial.c,v 1.1 2006/08/22 21:35:13 wolti Exp $
cam 0:0453a0a7e500 20 */
cam 0:0453a0a7e500 21
cam 0:0453a0a7e500 22 /* ----------------------- System includes ----------------------------------*/
cam 0:0453a0a7e500 23 #include "mbed.h" // Cam
cam 0:0453a0a7e500 24
cam 0:0453a0a7e500 25 /* ----------------------- Platform includes --------------------------------*/
cam 0:0453a0a7e500 26 #include "port.h"
cam 0:0453a0a7e500 27
cam 0:0453a0a7e500 28 /* ----------------------- Modbus includes ----------------------------------*/
cam 0:0453a0a7e500 29 #include "mb.h"
cam 0:0453a0a7e500 30 #include "mbport.h"
cam 0:0453a0a7e500 31
cam 0:0453a0a7e500 32
cam 0:0453a0a7e500 33 /* ----------------------- static functions ---------------------------------*/
cam 0:0453a0a7e500 34 static void prvvUARTTxReadyISR( void );
cam 0:0453a0a7e500 35 static void prvvUARTRxISR( void );
stanley1228 2:5f360876388e 36 //static void prvvUARTISR( void );
cam 0:0453a0a7e500 37
cam 0:0453a0a7e500 38 /* ----------------------- System Variables ---------------------------------*/
stanley1228 2:5f360876388e 39 //Serial pc(USBTX, USBRX); // Cam - mbed USB serial port
stanley1228 4:5000041d2dc2 40 Serial ser3(PB_10, PC_5); //serial3
stanley1228 2:5f360876388e 41 extern Serial pc;
stanley1228 4:5000041d2dc2 42 static uint8_t grx_buf=0;
stanley1228 4:5000041d2dc2 43
stanley1228 4:5000041d2dc2 44
stanley1228 2:5f360876388e 45 //Ticker simISR; // Cam - mbed ticker
cam 0:0453a0a7e500 46 // we don't have the TX buff empty interrupt, so
cam 0:0453a0a7e500 47 // we just interrupt every 1 mSec and read RX & TX
cam 0:0453a0a7e500 48 // status to simulate the proper ISRs.
cam 0:0453a0a7e500 49
stanley1228 2:5f360876388e 50 //static BOOL RxEnable, TxEnable; // Cam - keep a static copy of the RxEnable and TxEnable
cam 0:0453a0a7e500 51 // status for the simulated ISR (ticker)
cam 0:0453a0a7e500 52
cam 0:0453a0a7e500 53
cam 0:0453a0a7e500 54 /* ----------------------- Start implementation -----------------------------*/
stanley1228 2:5f360876388e 55 //stanley
stanley1228 2:5f360876388e 56 //#define RX_BUFF_LENGTH 1
stanley1228 2:5f360876388e 57 //#define TX_BUFF_LENGTH 1
stanley1228 2:5f360876388e 58 //event_callback_t serialRxCBEvent;
stanley1228 2:5f360876388e 59 //event_callback_t serialTxCBEvent;
stanley1228 2:5f360876388e 60
stanley1228 2:5f360876388e 61 //uint8_t rx_buf[RX_BUFF_LENGTH];
stanley1228 2:5f360876388e 62 //uint8_t tx_buf[TX_BUFF_LENGTH];
stanley1228 2:5f360876388e 63
stanley1228 2:5f360876388e 64
stanley1228 2:5f360876388e 65 //static void serialRxCBFn(int events)
stanley1228 2:5f360876388e 66 //{
stanley1228 2:5f360876388e 67 //
stanley1228 2:5f360876388e 68 // prvvUARTRxISR();
stanley1228 2:5f360876388e 69 //
stanley1228 2:5f360876388e 70 //}
stanley1228 2:5f360876388e 71 //static void serialTxCBFn(int events)
stanley1228 2:5f360876388e 72 //{
stanley1228 2:5f360876388e 73 // prvvUARTTxReadyISR();
stanley1228 2:5f360876388e 74 //
stanley1228 2:5f360876388e 75 //}
cam 0:0453a0a7e500 76 // Cam - This is called every 1mS to simulate Rx character received ISR and
cam 0:0453a0a7e500 77 // Tx buffer empty ISR.
stanley1228 2:5f360876388e 78 //static void
stanley1228 2:5f360876388e 79 //prvvUARTISR( void )
stanley1228 2:5f360876388e 80 //{
stanley1228 2:5f360876388e 81 // if (TxEnable)
stanley1228 2:5f360876388e 82 // if(ser3.writeable())
stanley1228 2:5f360876388e 83 // prvvUARTTxReadyISR();
stanley1228 2:5f360876388e 84 //
stanley1228 2:5f360876388e 85 // if (RxEnable)
stanley1228 2:5f360876388e 86 // if(ser3.readable())
stanley1228 2:5f360876388e 87 // prvvUARTRxISR();
stanley1228 2:5f360876388e 88 //}
stanley1228 2:5f360876388e 89
cam 0:0453a0a7e500 90
cam 0:0453a0a7e500 91 void
cam 0:0453a0a7e500 92 vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable )
cam 0:0453a0a7e500 93 {
cam 0:0453a0a7e500 94 /* If xRXEnable enable serial receive interrupts. If xTxENable enable
cam 0:0453a0a7e500 95 * transmitter empty interrupts.
cam 0:0453a0a7e500 96 */
stanley1228 2:5f360876388e 97 //RxEnable = xRxEnable;
stanley1228 2:5f360876388e 98 //TxEnable = xTxEnable;
stanley1228 2:5f360876388e 99
stanley1228 2:5f360876388e 100 //stanley
stanley1228 2:5f360876388e 101 if(xRxEnable)
stanley1228 2:5f360876388e 102 pc.printf("xRxEnable\n");
stanley1228 2:5f360876388e 103 else
stanley1228 2:5f360876388e 104 pc.printf("xRxDisable\n");
stanley1228 2:5f360876388e 105
stanley1228 2:5f360876388e 106 if(xTxEnable)
stanley1228 2:5f360876388e 107 pc.printf("xTxEnable\n");
stanley1228 2:5f360876388e 108 else
stanley1228 2:5f360876388e 109 pc.printf("xTxDisable\n");
stanley1228 2:5f360876388e 110
stanley1228 4:5000041d2dc2 111
stanley1228 4:5000041d2dc2 112 ser3.attach(NULL); //close all
stanley1228 4:5000041d2dc2 113
stanley1228 2:5f360876388e 114 if(xRxEnable)
stanley1228 2:5f360876388e 115 ser3.attach(&prvvUARTRxISR,Serial::RxIrq);
stanley1228 2:5f360876388e 116
stanley1228 4:5000041d2dc2 117 if(xTxEnable)
stanley1228 4:5000041d2dc2 118 {
stanley1228 2:5f360876388e 119 ser3.attach(&prvvUARTTxReadyISR,Serial::TxIrq);
stanley1228 4:5000041d2dc2 120 while(!ser3.writeable());
stanley1228 4:5000041d2dc2 121 prvvUARTTxReadyISR();
stanley1228 4:5000041d2dc2 122 }
stanley1228 4:5000041d2dc2 123
stanley1228 4:5000041d2dc2 124
stanley1228 2:5f360876388e 125 //if(xTxEnable)
stanley1228 2:5f360876388e 126 //¤£¥Î¦b³oÃä³]©wser3.write(tx_buf,TX_BUFF_LENGTH,serialTxCBEvent,SERIAL_EVENT_TX_COMPLETE);
stanley1228 4:5000041d2dc2 127
stanley1228 4:5000041d2dc2 128 //else
stanley1228 4:5000041d2dc2 129 //{
stanley1228 4:5000041d2dc2 130 // ser3.attach(NULL,Serial::RxIrq); //stanley the test result find that this may disable RxIrq and TxIrq so have to enable TxIrq
stanley1228 4:5000041d2dc2 131 // ser3.attach(&prvvUARTTxReadyISR,Serial::TxIrq);
stanley1228 4:5000041d2dc2 132 //}
cam 0:0453a0a7e500 133 }
cam 0:0453a0a7e500 134
cam 0:0453a0a7e500 135 BOOL
cam 0:0453a0a7e500 136 xMBPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity )
cam 0:0453a0a7e500 137 {
stanley1228 2:5f360876388e 138 //simISR.attach_us(&prvvUARTISR,1000); // Cam - attach prvvUARTISR to a 1mS ticker to simulate serial interrupt behaviour
cam 0:0453a0a7e500 139 // 1mS is just short of a character time at 9600 bps, so quick enough to pick
cam 0:0453a0a7e500 140 // up status on a character by character basis.
stanley1228 2:5f360876388e 141
stanley1228 2:5f360876388e 142 pc.printf("in Ini\n");
stanley1228 2:5f360876388e 143 //stanley
stanley1228 2:5f360876388e 144 //serialRxCBEvent.attach(serialRxCBFn);
stanley1228 2:5f360876388e 145 // serialTxCBEvent.attach(serialTxCBFn);
stanley1228 2:5f360876388e 146
stanley1228 2:5f360876388e 147 //ser3.attach(&prvvUARTRxISR,Serial::RxIrq);
stanley1228 2:5f360876388e 148 //ser3.attach(&prvvUARTTxReadyISR,Serial::TxIrq);
stanley1228 2:5f360876388e 149
stanley1228 2:5f360876388e 150
stanley1228 2:5f360876388e 151 return TRUE;
cam 0:0453a0a7e500 152 }
cam 0:0453a0a7e500 153
cam 0:0453a0a7e500 154 BOOL
cam 0:0453a0a7e500 155 xMBPortSerialPutByte( CHAR ucByte )
cam 0:0453a0a7e500 156 {
cam 0:0453a0a7e500 157 /* Put a byte in the UARTs transmit buffer. This function is called
cam 0:0453a0a7e500 158 * by the protocol stack if pxMBFrameCBTransmitterEmpty( ) has been
cam 0:0453a0a7e500 159 * called. */
stanley1228 2:5f360876388e 160 //ser3.putc( ucByte);
stanley1228 4:5000041d2dc2 161 //pc.printf("p\n");
stanley1228 4:5000041d2dc2 162 //pc.printf("%x",ucByte);
stanley1228 4:5000041d2dc2 163
stanley1228 2:5f360876388e 164 //stanley
stanley1228 2:5f360876388e 165 //while(!ser3.writeable());
stanley1228 2:5f360876388e 166 ser3.putc( ucByte);
stanley1228 2:5f360876388e 167
cam 0:0453a0a7e500 168 return TRUE;
cam 0:0453a0a7e500 169 }
cam 0:0453a0a7e500 170
cam 0:0453a0a7e500 171 BOOL
cam 0:0453a0a7e500 172 xMBPortSerialGetByte( CHAR * pucByte )
cam 0:0453a0a7e500 173 {
cam 0:0453a0a7e500 174 /* Return the byte in the UARTs receive buffer. This function is called
cam 0:0453a0a7e500 175 * by the protocol stack after pxMBFrameCBByteReceived( ) has been called.
cam 0:0453a0a7e500 176 */
stanley1228 4:5000041d2dc2 177 //pc.printf("%x",grx_buf);
stanley1228 4:5000041d2dc2 178
stanley1228 4:5000041d2dc2 179 *pucByte = grx_buf;//stanley
stanley1228 2:5f360876388e 180
stanley1228 2:5f360876388e 181
stanley1228 2:5f360876388e 182
cam 0:0453a0a7e500 183 return TRUE;
cam 0:0453a0a7e500 184 }
cam 0:0453a0a7e500 185
cam 0:0453a0a7e500 186 /* Create an interrupt handler for the transmit buffer empty interrupt
cam 0:0453a0a7e500 187 * (or an equivalent) for your target processor. This function should then
cam 0:0453a0a7e500 188 * call pxMBFrameCBTransmitterEmpty( ) which tells the protocol stack that
cam 0:0453a0a7e500 189 * a new character can be sent. The protocol stack will then call
cam 0:0453a0a7e500 190 * xMBPortSerialPutByte( ) to send the character.
cam 0:0453a0a7e500 191 */
cam 0:0453a0a7e500 192 static void prvvUARTTxReadyISR( void )
cam 0:0453a0a7e500 193 {
stanley1228 4:5000041d2dc2 194 //pc.printf("TR\n");
stanley1228 2:5f360876388e 195 pxMBFrameCBTransmitterEmpty();
cam 0:0453a0a7e500 196 }
cam 0:0453a0a7e500 197
cam 0:0453a0a7e500 198 /* Create an interrupt handler for the receive interrupt for your target
cam 0:0453a0a7e500 199 * processor. This function should then call pxMBFrameCBByteReceived( ). The
cam 0:0453a0a7e500 200 * protocol stack will then call xMBPortSerialGetByte( ) to retrieve the
cam 0:0453a0a7e500 201 * character.
cam 0:0453a0a7e500 202 */
cam 0:0453a0a7e500 203 static void prvvUARTRxISR( void )
cam 0:0453a0a7e500 204 {
stanley1228 4:5000041d2dc2 205 //pc.printf("in Rx_I\n");
stanley1228 4:5000041d2dc2 206
stanley1228 4:5000041d2dc2 207 if(ser3.readable()) //stanlely RX ISR must contain getc
stanley1228 4:5000041d2dc2 208 grx_buf=ser3.getc();
stanley1228 4:5000041d2dc2 209
stanley1228 2:5f360876388e 210 pxMBFrameCBByteReceived();
cam 0:0453a0a7e500 211 }
cam 0:0453a0a7e500 212
cam 0:0453a0a7e500 213