mirror of
https://github.com/RT-Thread/rt-thread.git
synced 2025-02-28 06:27:06 +08:00
modbus master
git-svn-id: https://rt-thread.googlecode.com/svn/trunk@785 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
parent
059f6d01a9
commit
dcff85f183
@ -24,13 +24,14 @@
|
|||||||
#include "mbfunc.h"
|
#include "mbfunc.h"
|
||||||
|
|
||||||
eMBErrorCode eMBMReadHoldingRegisters (UCHAR ucSlaveAddress, USHORT usRegStartAddress,
|
eMBErrorCode eMBMReadHoldingRegisters (UCHAR ucSlaveAddress, USHORT usRegStartAddress,
|
||||||
UBYTE ubNRegs, USHORT arusBufferOut[])
|
UBYTE ubNRegs, UBYTE arusBufferOut[])
|
||||||
{
|
{
|
||||||
static UCHAR ucMBFrame[5];
|
static UCHAR ucMBFrame[5];
|
||||||
eMBErrorCode eStatus = MB_ENOERR;
|
eMBErrorCode eStatus = MB_ENOERR;
|
||||||
eMBEventType eEvent;
|
eMBEventType eEvent;
|
||||||
static UCHAR ucRcvAddress;
|
static UCHAR ucRcvAddress;
|
||||||
static USHORT usLength;
|
static USHORT usLength;
|
||||||
|
UCHAR *ucRcvFrame;
|
||||||
|
|
||||||
/* make up request frame */
|
/* make up request frame */
|
||||||
ucMBFrame[0] = MB_FUNC_READ_HOLDING_REGISTER;
|
ucMBFrame[0] = MB_FUNC_READ_HOLDING_REGISTER;
|
||||||
@ -39,25 +40,23 @@ eMBErrorCode eMBMReadHoldingRegisters (UCHAR ucSlaveAddress, USHORT usRegStartA
|
|||||||
ucMBFrame[3] = (UCHAR)(ubNRegs >> 8);
|
ucMBFrame[3] = (UCHAR)(ubNRegs >> 8);
|
||||||
ucMBFrame[4] = (UCHAR)(ubNRegs);
|
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 */
|
/* send request frame to slave device */
|
||||||
eStatus = eMBRTUSend( ucSlaveAddress, ucMBFrame, 5 );
|
eStatus = eMBRTUSend( ucSlaveAddress, ucMBFrame, 5 );
|
||||||
|
|
||||||
/* wait on receive event */
|
/* wait on receive event */
|
||||||
if( xMBPortEventGet( &eEvent ) == TRUE )
|
if( xMBPortEventGet( &eEvent ) == TRUE )
|
||||||
{
|
{
|
||||||
eStatus = eMBRTUReceive( &ucRcvAddress, &ucMBFrame, &usLength );
|
eStatus = eMBRTUReceive( &ucRcvAddress, &ucRcvFrame, &usLength );
|
||||||
|
|
||||||
if( eStatus == MB_ENOERR )
|
if( eStatus == MB_ENOERR )
|
||||||
{
|
{
|
||||||
/* Check if the frame is for us. If not ignore the frame. */
|
/* Check if the frame is for us. If not ignore the frame. */
|
||||||
if( ( ucRcvAddress == ucSlaveAddress ) || ( ucRcvAddress == MB_ADDRESS_BROADCAST ) )
|
if( ( ucRcvAddress == ucSlaveAddress ) || ( ucRcvAddress == MB_ADDRESS_BROADCAST ) )
|
||||||
{
|
{
|
||||||
/* parse and restore data */
|
RT_ASSERT(ucRcvFrame[0] == MB_FUNC_READ_HOLDING_REGISTER);
|
||||||
rt_kprintf("parse and restore date here\n");
|
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
|
** @note
|
||||||
*/
|
*/
|
||||||
eMBErrorCode eMBMReadCoils (UCHAR ucSlaveAddress, USHORT usCoilStartAddress,
|
eMBErrorCode eMBMReadCoils (UCHAR ucSlaveAddress, USHORT usCoilStartAddress,
|
||||||
UBYTE ubNCoils, USHORT arusBufferOut[])
|
UBYTE ubNCoils, UBYTE arusBufferOut[])
|
||||||
{
|
{
|
||||||
static UCHAR ucMBFrame[5];
|
static UCHAR ucMBFrame[5];
|
||||||
eMBErrorCode eStatus = MB_ENOERR;
|
eMBErrorCode eStatus = MB_ENOERR;
|
||||||
eMBEventType eEvent;
|
eMBEventType eEvent;
|
||||||
static UCHAR ucRcvAddress;
|
static UCHAR ucRcvAddress;
|
||||||
static USHORT usLength;
|
static USHORT usLength;
|
||||||
|
UCHAR *ucRcvFrame;
|
||||||
|
|
||||||
/* make up request frame */
|
/* make up request frame */
|
||||||
ucMBFrame[0] = MB_FUNC_READ_COILS;
|
ucMBFrame[0] = MB_FUNC_READ_COILS;
|
||||||
ucMBFrame[1] = (UCHAR)(usCoilStartAddress >> 8);
|
ucMBFrame[1] = (UCHAR)(usCoilStartAddress >> 8);
|
||||||
ucMBFrame[2] = (UCHAR)(usCoilStartAddress);
|
ucMBFrame[2] = (UCHAR)(usCoilStartAddress);
|
||||||
ucMBFrame[3] = (UCHAR)(ubNCoils >> 8);
|
ucMBFrame[3] = (UCHAR)(ubNCoils >> 8);
|
||||||
ucMBFrame[4] = (UCHAR)(ubNCoils);
|
ucMBFrame[4] = (UCHAR)(ubNCoils);
|
||||||
|
|
||||||
|
/* send request frame to slave device */
|
||||||
|
eStatus = eMBRTUSend( ucSlaveAddress, ucMBFrame, 5 );
|
||||||
|
|
||||||
rt_kprintf("send frame [%x%x%x%x%x]\n",
|
/* wait on receive event */
|
||||||
ucMBFrame[0], ucMBFrame[1], ucMBFrame[2], ucMBFrame[3], ucMBFrame[4]);
|
if( xMBPortEventGet( &eEvent ) == TRUE )
|
||||||
|
{
|
||||||
|
eStatus = eMBRTUReceive( &ucRcvAddress, &ucRcvFrame, &usLength );
|
||||||
/* send request frame to slave device */
|
if( eStatus == MB_ENOERR )
|
||||||
eStatus = eMBRTUSend( ucSlaveAddress, ucMBFrame, 5 );
|
|
||||||
|
|
||||||
/* wait on receive event */
|
|
||||||
if( xMBPortEventGet( &eEvent ) == TRUE )
|
|
||||||
{
|
{
|
||||||
eStatus = eMBRTUReceive( &ucRcvAddress, &ucMBFrame, &usLength );
|
/* Check if the frame is for us. If not ignore the frame. */
|
||||||
if( eStatus == MB_ENOERR )
|
if( ucRcvAddress == ucSlaveAddress )
|
||||||
{
|
{
|
||||||
/* Check if the frame is for us. If not ignore the frame. */
|
RT_ASSERT(ucRcvFrame[0] == MB_FUNC_READ_COILS);
|
||||||
if( ( ucRcvAddress == ucSlaveAddress ) || ( ucRcvAddress == MB_ADDRESS_BROADCAST ) )
|
|
||||||
{
|
rt_memcpy((UCHAR *)arusBufferOut, &ucRcvFrame[2], ucRcvFrame[1]);
|
||||||
/* parse and restore data */
|
|
||||||
rt_kprintf("parse and restore date here\n");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else eStatus = MB_ETIMEDOUT;
|
}
|
||||||
|
else eStatus = MB_ETIMEDOUT;
|
||||||
|
|
||||||
return eStatus;
|
return eStatus;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -24,36 +24,36 @@
|
|||||||
#include "mbproto.h"
|
#include "mbproto.h"
|
||||||
#include "mbfunc.h"
|
#include "mbfunc.h"
|
||||||
|
|
||||||
|
#include "varible.h"
|
||||||
USHORT buf[256];
|
|
||||||
|
|
||||||
|
|
||||||
void rt_modbus_thread_entry(void* parameter)
|
void rt_modbus_thread_entry(void* parameter)
|
||||||
{
|
{
|
||||||
eMBErrorCode eStatus;
|
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 );
|
eStatus = eMBInit( MB_RTU, 0x0A, 0, 115200, MB_PAR_EVEN );
|
||||||
|
|
||||||
/* Enable the Modbus Protocol Stack. */
|
/* Enable the Modbus Protocol Stack. */
|
||||||
eStatus = eMBEnable( );
|
eStatus = eMBEnable();
|
||||||
rt_thread_delay(50);
|
rt_thread_delay(50);
|
||||||
|
|
||||||
while(1)
|
while(1)
|
||||||
{
|
{
|
||||||
|
int i = 0;
|
||||||
|
|
||||||
/* request holding reg */
|
var_group->table->hash_table[].
|
||||||
eMBMReadHoldingRegisters(0x0A, 0x1, 0x10, buf);
|
if(eMBMReadCoils(0x01, 0x0, 0x6, buf) != MB_ETIMEDOUT)
|
||||||
rt_kprintf("stop\n");
|
|
||||||
rt_thread_delay(100);
|
|
||||||
/* request coils */
|
|
||||||
eMBMReadCoils(0x0A, 0x1, 128, buf);
|
|
||||||
rt_thread_delay(100);
|
|
||||||
|
|
||||||
//while(1);
|
|
||||||
|
|
||||||
|
rt_thread_delay(100);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int modbus_demo_init(void)
|
int modbus_start(void)
|
||||||
{
|
{
|
||||||
rt_thread_t modbus_thread;
|
rt_thread_t modbus_thread;
|
||||||
|
|
||||||
|
@ -33,15 +33,16 @@ BOOL xMBPortEventPost( eMBEventType eEvent )
|
|||||||
{
|
{
|
||||||
/* only care abot EV_FRAME_RECEIVED event */
|
/* only care abot EV_FRAME_RECEIVED event */
|
||||||
if(eEvent == EV_FRAME_RECEIVED)
|
if(eEvent == EV_FRAME_RECEIVED)
|
||||||
|
{
|
||||||
rt_event_send(&event, 1<<eEvent);
|
rt_event_send(&event, 1<<eEvent);
|
||||||
|
}
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOL xMBPortEventGet( eMBEventType * eEvent )
|
BOOL xMBPortEventGet( eMBEventType * eEvent )
|
||||||
{
|
{
|
||||||
rt_uint32_t e;
|
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),
|
if(rt_event_recv(&event, (1<<EV_FRAME_RECEIVED),
|
||||||
RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR,
|
RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR,
|
||||||
@ -50,6 +51,11 @@ BOOL xMBPortEventGet( eMBEventType * eEvent )
|
|||||||
*eEvent = EV_FRAME_RECEIVED;
|
*eEvent = EV_FRAME_RECEIVED;
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
else return FALSE;
|
else
|
||||||
|
{
|
||||||
|
rt_kprintf("get event timeout\n");
|
||||||
|
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,7 +21,7 @@
|
|||||||
#include "mb.h"
|
#include "mb.h"
|
||||||
#include "mbport.h"
|
#include "mbport.h"
|
||||||
|
|
||||||
#define UART1 ((struct uartport *)U1BASE)
|
#define UART1 ((struct uartport *)&U1BASE)
|
||||||
|
|
||||||
/* ----------------------- static functions ---------------------------------*/
|
/* ----------------------- static functions ---------------------------------*/
|
||||||
static void rt_serial1_handler(int vector);
|
static void rt_serial1_handler(int vector);
|
||||||
@ -97,22 +97,21 @@ BOOL xMBPortSerialPutByte( CHAR ucByte )
|
|||||||
|
|
||||||
BOOL xMBPortSerialGetByte( CHAR * pucByte )
|
BOOL xMBPortSerialGetByte( CHAR * pucByte )
|
||||||
{
|
{
|
||||||
while ((USTAT1 & USTAT_RCV_READY) == 0);
|
while (!(USTAT1 & USTAT_RCV_READY));
|
||||||
|
|
||||||
*pucByte = URXB1;
|
*pucByte = URXH1;
|
||||||
|
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void rt_serial1_handler(int vector)
|
static void rt_serial1_handler(int vector)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (SUBSRCPND & BIT_SUB_RXD1)
|
if (SUBSRCPND & BIT_SUB_RXD1)
|
||||||
{
|
{
|
||||||
SUBSRCPND |= BIT_SUB_RXD1;
|
SUBSRCPND |= BIT_SUB_RXD1;
|
||||||
prvvUARTRxISR();
|
prvvUARTRxISR();
|
||||||
}
|
}
|
||||||
if (SUBSRCPND & BIT_SUB_TXD1)
|
else if (SUBSRCPND & BIT_SUB_TXD1)
|
||||||
{
|
{
|
||||||
SUBSRCPND |= BIT_SUB_TXD1;
|
SUBSRCPND |= BIT_SUB_TXD1;
|
||||||
prvvUARTTxReadyISR();
|
prvvUARTTxReadyISR();
|
||||||
|
@ -33,14 +33,14 @@ BOOL xMBPortTimersInit(USHORT usTim1Timerout50us)
|
|||||||
TCFG1 &= 0xffff0fff;
|
TCFG1 &= 0xffff0fff;
|
||||||
TCFG1 |= 0x00001000;
|
TCFG1 |= 0x00001000;
|
||||||
|
|
||||||
TCNTB3 = (rt_int32_t)(usTim1Timerout50us*(PCLK/ (4 *16* 20000))) - 1;
|
TCNTB3 = (rt_int32_t)(usTim1Timerout50us*(PCLK/ (4 *16* 1000))) - 1;
|
||||||
/* manual update */
|
/* manual update */
|
||||||
TCON = TCON & (~(0x0f<<16)) | (0x02<<16);
|
TCON = TCON & (~(0x0f<<16)) | (0x02<<16);
|
||||||
/* install interrupt handler */
|
/* install interrupt handler */
|
||||||
rt_hw_interrupt_install(INTTIMER3, prvvTIMERExpiredISR, RT_NULL);
|
rt_hw_interrupt_install(INTTIMER3, prvvTIMERExpiredISR, RT_NULL);
|
||||||
rt_hw_interrupt_umask(INTTIMER3);
|
rt_hw_interrupt_umask(INTTIMER3);
|
||||||
|
|
||||||
/* start timer4, reload */
|
/* start timer3, reload */
|
||||||
TCON = TCON & (~(0x0f<<16)) | (0x09<<16);
|
TCON = TCON & (~(0x0f<<16)) | (0x09<<16);
|
||||||
|
|
||||||
return TRUE;
|
return TRUE;
|
||||||
|
@ -207,8 +207,8 @@ eMBRTUSend( UCHAR ucSlaveAddress, const UCHAR * pucFrame, USHORT usLength )
|
|||||||
|
|
||||||
/* Calculate CRC16 checksum for Modbus-Serial-Line-PDU. */
|
/* Calculate CRC16 checksum for Modbus-Serial-Line-PDU. */
|
||||||
usCRC16 = usMBCRC16( ( UCHAR * ) pucSndBufferCur, usSndBufferCount );
|
usCRC16 = usMBCRC16( ( UCHAR * ) pucSndBufferCur, usSndBufferCount );
|
||||||
ucRTUBuf[usSndBufferCount++] = ( UCHAR )( usCRC16 & 0xFF );
|
pucSndBufferCur[usSndBufferCount++] = ( UCHAR )( usCRC16 & 0xFF );
|
||||||
ucRTUBuf[usSndBufferCount++] = ( UCHAR )( usCRC16 >> 8 );
|
pucSndBufferCur[usSndBufferCount++] = ( UCHAR )( usCRC16 >> 8 );
|
||||||
|
|
||||||
/* Activate the transmitter. */
|
/* Activate the transmitter. */
|
||||||
eSndState = STATE_TX_XMIT;
|
eSndState = STATE_TX_XMIT;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user