From 2360e84ec9661f96d9f67b4cfa98204781dd47b2 Mon Sep 17 00:00:00 2001 From: "lijin.unix" Date: Wed, 7 Apr 2010 06:52:56 +0000 Subject: [PATCH] modbus master: add request coils func. git-svn-id: https://rt-thread.googlecode.com/svn/trunk@590 bbd45198-f89e-11dd-88c7-29a3b14d5316 --- net/freemodbus/modbus/mbmaster.c | 60 +++++++++++++++++++++++++++++++ net/freemodbus/modbus/port/demo.c | 14 ++++++-- 2 files changed, 71 insertions(+), 3 deletions(-) diff --git a/net/freemodbus/modbus/mbmaster.c b/net/freemodbus/modbus/mbmaster.c index e3d1a5d3de..17c0fc05f9 100644 --- a/net/freemodbus/modbus/mbmaster.c +++ b/net/freemodbus/modbus/mbmaster.c @@ -66,3 +66,63 @@ eMBErrorCode eMBMReadHoldingRegisters (UCHAR ucSlaveAddress, USHORT usRegStartA return eStatus; } +/*! @fn eMBErrorCode eMBMReadCoils (UCHAR ucSlaveAddress, USHORT usCoilStartAddress, + UBYTE ubNCoils, USHORT arusBufferOut[]) +** @brief request coils +** @details +** @param ucSlaveAddress slave station address :from 1 to 247(max) +** @param usCoilStartAddress coils address +** @param ubNCoils request coils number +** @param arusBufferOut response packet buf +** @return eMBErrorCode +** @author LiJin +** @date 2010-04-07 +** @note +*/ +eMBErrorCode eMBMReadCoils (UCHAR ucSlaveAddress, USHORT usCoilStartAddress, + UBYTE ubNCoils, USHORT 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]); + + + /* send request frame to slave device */ + eStatus = eMBRTUSend( ucSlaveAddress, ucMBFrame, 5 ); + + /* wait on receive event */ + if( xMBPortEventGet( &eEvent ) == TRUE ) + { + eStatus = eMBRTUReceive( &ucRcvAddress, &ucMBFrame, &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"); + } + } + } + else eStatus = MB_ETIMEDOUT; + + return eStatus; + +} + + + + diff --git a/net/freemodbus/modbus/port/demo.c b/net/freemodbus/modbus/port/demo.c index e11a1e7ed7..0f23325693 100644 --- a/net/freemodbus/modbus/port/demo.c +++ b/net/freemodbus/modbus/port/demo.c @@ -10,6 +10,7 @@ * Change Logs: * Date Author Notes * 2010-04-04 yi.qiu first version + * 2010-04-07 LiJin */ /* ----------------------- Platform includes --------------------------------*/ @@ -23,10 +24,13 @@ #include "mbproto.h" #include "mbfunc.h" + +USHORT buf[256]; + + void rt_modbus_thread_entry(void* parameter) { eMBErrorCode eStatus; - USHORT buf[100]; eStatus = eMBInit( MB_RTU, 0x0A, 0, 115200, MB_PAR_EVEN ); @@ -37,10 +41,14 @@ void rt_modbus_thread_entry(void* parameter) while(1) { - eMBMReadHoldingRegisters(0x0A, 0x10000, 0x1, buf); + /* 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); + //while(1); } } -- GitLab