from Cam Marshall original modbus

Dependencies:   mbed

Fork of Modbus by Cam Marshall

Files at this revision

API Documentation at this revision

Comitter:
stanley1228
Date:
Tue Mar 14 14:25:27 2017 +0800
Parent:
3:3630d2e9e983
Child:
5:c712a10c9414
Commit message:
use Qmodbus to test can read input register
1.but need to test more high speed baudrate
2.check timeout

Changed in this revision

Modbus/mbrtu.cpp Show annotated file Show diff for this revision Revisions of this file
Modbus/portserial.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/Modbus/mbrtu.cpp	Mon Mar 13 13:12:06 2017 +0000
+++ b/Modbus/mbrtu.cpp	Tue Mar 14 14:25:27 2017 +0800
@@ -134,6 +134,7 @@
     eRcvState = STATE_RX_INIT;
     vMBPortSerialEnable( TRUE, FALSE );
     vMBPortTimersEnable(  );
+
     EXIT_CRITICAL_SECTION(  );
 }
 
@@ -154,12 +155,12 @@
 
     ENTER_CRITICAL_SECTION(  );
     assert( usRcvBufferPos < MB_SER_PDU_SIZE_MAX );
-        
+
     /* Length and CRC check */
     if( ( usRcvBufferPos >= MB_SER_PDU_SIZE_MIN )
         && ( usMBCRC16( ( UCHAR * ) ucRTUBuf, usRcvBufferPos ) == 0 ) )
     {
-        /* Save the address field. All frames are passed to the upper layer
+        /* Save the address field. All frames are passed to the upper layed
          * and the decision if a frame is used is done there.
          */
         *pucRcvAddress = ucRTUBuf[MB_SER_PDU_ADDR_OFF];
@@ -176,7 +177,7 @@
         // Added by Cam
         // Now that the poll routine knows about the received frame, 
         // clear the receive buffer position ready for the next frame received
-        usRcvBufferPos = 0;
+        //usRcvBufferPos = 0;
 
     }
     else
@@ -256,9 +257,10 @@
 
         /* In the idle state we wait for a new character. If a character
          * is received the t1.5 and t3.5 timers are started and the
-         * receiver is in the state STATE_RX_RECEIVE.
+         * receiver is in the state STATE_RX_RECEIVCE.
          */
     case STATE_RX_IDLE:
+        usRcvBufferPos = 0; //cam will comment this
         ucRTUBuf[usRcvBufferPos++] = ucByte;
         eRcvState = STATE_RX_RCV;
 
@@ -292,6 +294,7 @@
     BOOL            xNeedPoll = FALSE;
 
     assert( eRcvState == STATE_RX_IDLE );
+
     switch ( eSndState )
     {
         /* We should not get a transmitter event if the transmitter is in
@@ -326,7 +329,7 @@
 BOOL
 xMBRTUTimerT35Expired( void )
 {
-    BOOL    xNeedPoll = FALSE;
+    BOOL            xNeedPoll = FALSE;
 
     switch ( eRcvState )
     {
--- a/Modbus/portserial.cpp	Mon Mar 13 13:12:06 2017 +0000
+++ b/Modbus/portserial.cpp	Tue Mar 14 14:25:27 2017 +0800
@@ -37,8 +37,11 @@
 
 /* ----------------------- System Variables ---------------------------------*/
 //Serial pc(USBTX, USBRX);           // Cam - mbed USB serial port
-Serial ser3(D6, PC_11); //serial3
+Serial ser3(PB_10, PC_5); //serial3
 extern Serial pc;
+static uint8_t grx_buf=0;
+
+
 //Ticker simISR;                      // Cam - mbed ticker
                                     // we don't have the TX buff empty interrupt, so
                                     // we just interrupt every 1 mSec and read RX & TX
@@ -105,17 +108,28 @@
 	else
 		pc.printf("xTxDisable\n");
 
+
+	ser3.attach(NULL); //close all
+	
 	if(xRxEnable)
 		ser3.attach(&prvvUARTRxISR,Serial::RxIrq);
-	else
-		ser3.attach(NULL,Serial::RxIrq);
 	
-	/*if(xTxEnable)
+	if(xTxEnable)
+	{
 		ser3.attach(&prvvUARTTxReadyISR,Serial::TxIrq);
-	else
-		ser3.attach(NULL,Serial::TxIrq);*/
+		while(!ser3.writeable());
+		prvvUARTTxReadyISR();
+	}		
+	
+
 	//if(xTxEnable)
 		//¤£¥Î¦b³oÃä³]©wser3.write(tx_buf,TX_BUFF_LENGTH,serialTxCBEvent,SERIAL_EVENT_TX_COMPLETE);
+
+	//else
+	//{
+	//	ser3.attach(NULL,Serial::RxIrq);  //stanley  the test result find that this may disable RxIrq and TxIrq so have to enable TxIrq
+	//	ser3.attach(&prvvUARTTxReadyISR,Serial::TxIrq);  
+	//}
 }
 
 BOOL
@@ -144,7 +158,9 @@
      * by the protocol stack if pxMBFrameCBTransmitterEmpty( ) has been
      * called. */
     //ser3.putc( ucByte);
-	pc.printf("in put\n");
+	//pc.printf("p\n");
+	//pc.printf("%x",ucByte);
+
 	//stanley
 	//while(!ser3.writeable());
 	ser3.putc( ucByte);
@@ -158,9 +174,9 @@
     /* Return the byte in the UARTs receive buffer. This function is called
      * by the protocol stack after pxMBFrameCBByteReceived( ) has been called.
      */
-	pc.printf("in get\n");
-	if(ser3.readable())
-		*pucByte = ser3.getc();
+	//pc.printf("%x",grx_buf);
+	
+	*pucByte = grx_buf;//stanley
 
 	
 
@@ -175,7 +191,7 @@
  */
 static void prvvUARTTxReadyISR( void )
 {
-	pc.printf("in Tx_I\n");
+	//pc.printf("TR\n");
     pxMBFrameCBTransmitterEmpty();
 }
 
@@ -186,7 +202,11 @@
  */
 static void prvvUARTRxISR( void )
 {
-	pc.printf("in Rx_I\n");
+	//pc.printf("in Rx_I\n");
+
+	if(ser3.readable())	//stanlely RX ISR must contain getc
+		grx_buf=ser3.getc();
+
     pxMBFrameCBByteReceived();
 }
 
--- a/main.cpp	Mon Mar 13 13:12:06 2017 +0000
+++ b/main.cpp	Tue Mar 14 14:25:27 2017 +0800
@@ -27,6 +27,7 @@
 #include "mbed.h" //stanley
 DigitalOut myled(LED1); //stanley
 Serial pc(USBTX, USBRX);  //stanley
+
 /* ----------------------- Defines ------------------------------------------*/
 #define REG_INPUT_START 1000
 #define REG_INPUT_NREGS 4
@@ -55,9 +56,15 @@
     //eMBRegHoldingCB(myRegInputBuf,REG_INPUT_START+1,1,MB_REG_WRITE);//stanley
 	pc.printf("in main\n");
 	myled=1;
+
+	int err=0;
     for( ;; )
     {
-        ( void )eMBPoll(  );
+        //(void)eMBPoll(  ); origianl
+		//
+		err=eMBPoll();
+		if(err!=MB_ENOERR)
+			pc.printf("err=%d\n",err);
        
         /* Here we simply count the number of poll cycles. */
         usRegInputBuf[0]++;