Преглед изворни кода

modbus master

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@785 bbd45198-f89e-11dd-88c7-29a3b14d5316
qiuyiuestc пре 15 година
родитељ
комит
dcff85f183

+ 35 - 42
components/net/freemodbus/modbus/mbmaster.c

@@ -24,13 +24,14 @@
 #include "mbfunc.h"
 
 eMBErrorCode eMBMReadHoldingRegisters  (UCHAR ucSlaveAddress, USHORT usRegStartAddress, 
-			UBYTE ubNRegs, USHORT arusBufferOut[]) 
+			UBYTE ubNRegs, UBYTE arusBufferOut[]) 
 {
 	static UCHAR ucMBFrame[5];
 	eMBErrorCode eStatus = MB_ENOERR;
 	eMBEventType eEvent;
 	static UCHAR ucRcvAddress;
 	static USHORT usLength;
+	UCHAR *ucRcvFrame;
 
 	/* make up request frame */	
 	ucMBFrame[0] = MB_FUNC_READ_HOLDING_REGISTER;
@@ -38,26 +39,24 @@ eMBErrorCode eMBMReadHoldingRegisters  (UCHAR ucSlaveAddress, USHORT usRegStartA
 	ucMBFrame[2] = (UCHAR)(usRegStartAddress);
 	ucMBFrame[3] = (UCHAR)(ubNRegs >> 8);
 	ucMBFrame[4] = (UCHAR)(ubNRegs);	
-
 	 
-		rt_kprintf("send frame [%x%x%x%x%x]\n", 
-		ucMBFrame[0], ucMBFrame[1], ucMBFrame[2], ucMBFrame[3], ucMBFrame[4]); 
-	
-	
 	/* send request frame to slave device */
 	eStatus = eMBRTUSend( ucSlaveAddress, ucMBFrame, 5 );
 
 	/* wait on receive event */
 	if( xMBPortEventGet( &eEvent ) == TRUE )
 	{		
-		eStatus = eMBRTUReceive( &ucRcvAddress, &ucMBFrame, &usLength );
+		eStatus = eMBRTUReceive( &ucRcvAddress, &ucRcvFrame, &usLength );
+
 		if( eStatus == MB_ENOERR )
 		{
 			/* Check if the frame is for us. If not ignore the frame. */
 			if( ( ucRcvAddress == ucSlaveAddress ) || ( ucRcvAddress == MB_ADDRESS_BROADCAST ) )
 			{
-				/* parse and restore data */
-				rt_kprintf("parse and restore date here\n");
+				RT_ASSERT(ucRcvFrame[0] == MB_FUNC_READ_HOLDING_REGISTER);
+				RT_ASSERT(ucRcvFrame[1] == 2*ubNRegs)
+
+				rt_memcpy((UCHAR *)arusBufferOut, &ucRcvFrame[2], 2*ubNRegs);
 			}
 		}
 	}
@@ -80,49 +79,43 @@ eMBErrorCode eMBMReadHoldingRegisters  (UCHAR ucSlaveAddress, USHORT usRegStartA
 ** @note
 */
 eMBErrorCode eMBMReadCoils  (UCHAR ucSlaveAddress, USHORT usCoilStartAddress,
-			UBYTE ubNCoils, USHORT arusBufferOut[])
+			UBYTE ubNCoils, UBYTE arusBufferOut[])
 {
 	static UCHAR ucMBFrame[5];
-		eMBErrorCode eStatus = MB_ENOERR;
-		eMBEventType eEvent;
-		static UCHAR ucRcvAddress;
-		static USHORT usLength;
-
-		/* make up request frame */
-		ucMBFrame[0] = MB_FUNC_READ_COILS;
-		ucMBFrame[1] = (UCHAR)(usCoilStartAddress >> 8);
-		ucMBFrame[2] = (UCHAR)(usCoilStartAddress);
-		ucMBFrame[3] = (UCHAR)(ubNCoils >> 8);
-		ucMBFrame[4] = (UCHAR)(ubNCoils);
-
-
-			rt_kprintf("send frame [%x%x%x%x%x]\n",
-			ucMBFrame[0], ucMBFrame[1], ucMBFrame[2], ucMBFrame[3], ucMBFrame[4]);
+	eMBErrorCode eStatus = MB_ENOERR;
+	eMBEventType eEvent;
+	static UCHAR ucRcvAddress;
+	static USHORT usLength;
+	UCHAR *ucRcvFrame;
 
+	/* make up request frame */
+	ucMBFrame[0] = MB_FUNC_READ_COILS;
+	ucMBFrame[1] = (UCHAR)(usCoilStartAddress >> 8);
+	ucMBFrame[2] = (UCHAR)(usCoilStartAddress);
+	ucMBFrame[3] = (UCHAR)(ubNCoils >> 8);
+	ucMBFrame[4] = (UCHAR)(ubNCoils);
 
-		/* send request frame to slave device */
-		eStatus = eMBRTUSend( ucSlaveAddress, ucMBFrame, 5 );
+	/* send request frame to slave device */
+	eStatus = eMBRTUSend( ucSlaveAddress, ucMBFrame, 5 );
 
-		/* wait on receive event */
-		if( xMBPortEventGet( &eEvent ) == TRUE )
+	/* wait on receive event */
+	if( xMBPortEventGet( &eEvent ) == TRUE )
+	{
+		eStatus = eMBRTUReceive( &ucRcvAddress, &ucRcvFrame, &usLength );
+		if( eStatus == MB_ENOERR )
 		{
-			eStatus = eMBRTUReceive( &ucRcvAddress, &ucMBFrame, &usLength );
-			if( eStatus == MB_ENOERR )
+			/* Check if the frame is for us. If not ignore the frame. */
+			if( ucRcvAddress == ucSlaveAddress )
 			{
-				/* Check if the frame is for us. If not ignore the frame. */
-				if( ( ucRcvAddress == ucSlaveAddress ) || ( ucRcvAddress == MB_ADDRESS_BROADCAST ) )
-				{
-					/* parse and restore data */
-					rt_kprintf("parse and restore date here\n");
-				}
+				RT_ASSERT(ucRcvFrame[0] == MB_FUNC_READ_COILS);
+
+				rt_memcpy((UCHAR *)arusBufferOut, &ucRcvFrame[2], ucRcvFrame[1]);
 			}
 		}
-		else eStatus = MB_ETIMEDOUT;
+	}
+	else eStatus = MB_ETIMEDOUT;
 
-		return eStatus;
+	return eStatus;
 
 }
 
-
-
-

+ 14 - 14
components/net/freemodbus/modbus/port/demo.c

@@ -24,36 +24,36 @@
 #include "mbproto.h"
 #include "mbfunc.h"
 
-
-USHORT buf[256];
-
+#include "varible.h"
 
 void rt_modbus_thread_entry(void* parameter)
 {
 	eMBErrorCode eStatus;
-		
+	USHORT buf[1];
+	varible_group_t var_group;
+	
+	var_group = varible_group_get();
+	
 	eStatus = eMBInit( MB_RTU, 0x0A, 0, 115200, MB_PAR_EVEN );
 
 	/* Enable the Modbus Protocol Stack. */
-	eStatus = eMBEnable(  );
+	eStatus = eMBEnable();
 	rt_thread_delay(50);
 	
 	while(1)
 	{
+		int i = 0;
 
-		/* request holding reg  */
-		eMBMReadHoldingRegisters(0x0A, 0x1, 0x10, buf);
-		rt_kprintf("stop\n");
-		rt_thread_delay(100);
-		/* request coils  */
-		eMBMReadCoils(0x0A, 0x1, 128, buf);
-		rt_thread_delay(100);
+		var_group->table->hash_table[].
+		if(eMBMReadCoils(0x01, 0x0, 0x6, buf) != MB_ETIMEDOUT)
 
-		//while(1);
+		
+
+		rt_thread_delay(100);
 	}
 }
 
-int modbus_demo_init(void)
+int modbus_start(void)
 {
 	rt_thread_t modbus_thread;
 

+ 9 - 3
components/net/freemodbus/modbus/port/portevent.c

@@ -33,15 +33,16 @@ BOOL xMBPortEventPost( eMBEventType eEvent )
 {
 	/* only care abot EV_FRAME_RECEIVED event */
 	if(eEvent == EV_FRAME_RECEIVED)
+	{	
 		rt_event_send(&event, 1<<eEvent);
-
+	}
 	return TRUE;
 }
 
 BOOL xMBPortEventGet( eMBEventType * eEvent )
 {
 	rt_uint32_t e;
-	rt_int32_t time_out = 100/(1000/RT_TICK_PER_SECOND);
+	rt_int32_t time_out = 3000/(1000/RT_TICK_PER_SECOND);
 		
 	if(rt_event_recv(&event, (1<<EV_FRAME_RECEIVED),
 			RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR,
@@ -50,6 +51,11 @@ BOOL xMBPortEventGet( eMBEventType * eEvent )
 		*eEvent = EV_FRAME_RECEIVED;
 		return TRUE;
 	}		
-	else return FALSE;
+	else 
+	{
+		rt_kprintf("get event timeout\n");
+
+		return FALSE;
+	}	
 }
 

+ 8 - 9
components/net/freemodbus/modbus/port/portserial.c

@@ -21,7 +21,7 @@
 #include "mb.h"
 #include "mbport.h"
 
-#define UART1	((struct uartport *)U1BASE)
+#define UART1	((struct uartport *)&U1BASE)
 
 /* ----------------------- static functions ---------------------------------*/
 static void rt_serial1_handler(int vector);
@@ -73,7 +73,7 @@ BOOL xMBPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBPar
 	UART1->ucon = 0x245;
 	/* Set uart0 bps */
 	UART1->ubrd = (rt_int32_t)(PCLK / (ulBaudRate * 16)) - 1;
-
+	
 	for (i = 0; i < 100; i++);
 
 	SUBSRCPND |= BIT_SUB_RXD1;
@@ -97,26 +97,25 @@ BOOL xMBPortSerialPutByte( CHAR ucByte )
 
 BOOL xMBPortSerialGetByte( CHAR * pucByte )
 {
-	while ((USTAT1 & USTAT_RCV_READY) == 0);
-
-	*pucByte = URXB1;
+	while (!(USTAT1 & USTAT_RCV_READY));
 
+	*pucByte = URXH1;
+	
 	return TRUE;
 }
 
 static void rt_serial1_handler(int vector)
 {	
-
 	if (SUBSRCPND & BIT_SUB_RXD1)
 	{			
 		SUBSRCPND |= BIT_SUB_RXD1;
 		prvvUARTRxISR();
 	}	
-	if (SUBSRCPND & BIT_SUB_TXD1)
-	{	
+	else if (SUBSRCPND & BIT_SUB_TXD1)
+	{
 		SUBSRCPND |= BIT_SUB_TXD1;	
 		prvvUARTTxReadyISR();
-	}	
+	}		
 }
 
 /* 

+ 2 - 2
components/net/freemodbus/modbus/port/porttimer.c

@@ -33,14 +33,14 @@ BOOL xMBPortTimersInit(USHORT usTim1Timerout50us)
 	TCFG1  &= 0xffff0fff;
 	TCFG1  |= 0x00001000;
 
-	TCNTB3 = (rt_int32_t)(usTim1Timerout50us*(PCLK/ (4 *16* 20000))) - 1;
+	TCNTB3 = (rt_int32_t)(usTim1Timerout50us*(PCLK/ (4 *16* 1000))) - 1;
 	/* manual update */
 	TCON = TCON & (~(0x0f<<16)) | (0x02<<16);
 	/* install interrupt handler */
 	rt_hw_interrupt_install(INTTIMER3, prvvTIMERExpiredISR, RT_NULL);
 	rt_hw_interrupt_umask(INTTIMER3);
 
-	/* start timer4, reload */
+	/* start timer3, reload */
 	TCON = TCON & (~(0x0f<<16)) | (0x09<<16);
 
 	return TRUE;

+ 2 - 2
components/net/freemodbus/modbus/rtu/mbrtu.c

@@ -207,8 +207,8 @@ eMBRTUSend( UCHAR ucSlaveAddress, const UCHAR * pucFrame, USHORT usLength )
 
         /* Calculate CRC16 checksum for Modbus-Serial-Line-PDU. */
         usCRC16 = usMBCRC16( ( UCHAR * ) pucSndBufferCur, usSndBufferCount );
-        ucRTUBuf[usSndBufferCount++] = ( UCHAR )( usCRC16 & 0xFF );
-        ucRTUBuf[usSndBufferCount++] = ( UCHAR )( usCRC16 >> 8 );
+        pucSndBufferCur[usSndBufferCount++] = ( UCHAR )( usCRC16 & 0xFF );
+        pucSndBufferCur[usSndBufferCount++] = ( UCHAR )( usCRC16 >> 8 );
 
         /* Activate the transmitter. */
         eSndState = STATE_TX_XMIT;