初级会员
积分 53
金钱 53
注册时间 2019-8-21
在线时间 65 小时
20 金钱
freemodbus,如果我把功能码1 改成这样
/*
*****************************************
该.c文件是一个自定义的命令,对应的功能码是 1
声明在“mbfunc.h”头文件里面
*/
/* ----------------------- System includes ----------------------------------*/
#include "stdlib.h"
#include "string.h"
/* ----------------------- Platform includes --------------------------------*/
#include "port.h"
/* ----------------------- Modbus includes ----------------------------------*/
#include "mb.h"
#include "mbframe.h"
#include "mbproto.h"
#include "mbconfig.h"
/* ----------------------- Defines ------------------------------------------*/
#define MB_PDU_FUNC_READ_ADDR_OFF ( MB_PDU_DATA_OFF )
#define MB_PDU_FUNC_READ_COILCNT_OFF ( MB_PDU_DATA_OFF + 2 )
#define MB_PDU_FUNC_READ_SIZE ( 4 )
#define MB_PDU_FUNC_READ_COILCNT_MAX ( 0x07D0 )
#define MB_PDU_FUNC_WRITE_ADDR_OFF ( MB_PDU_DATA_OFF )
#define MB_PDU_FUNC_WRITE_VALUE_OFF ( MB_PDU_DATA_OFF + 2 )
#define MB_PDU_FUNC_WRITE_SIZE ( 4 )
#define MB_PDU_FUNC_WRITE_MUL_ADDR_OFF ( MB_PDU_DATA_OFF )
#define MB_PDU_FUNC_WRITE_MUL_COILCNT_OFF ( MB_PDU_DATA_OFF + 2 )
#define MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF ( MB_PDU_DATA_OFF + 4 )
#define MB_PDU_FUNC_WRITE_MUL_VALUES_OFF ( MB_PDU_DATA_OFF + 5 )
#define MB_PDU_FUNC_WRITE_MUL_SIZE_MIN ( 5 )
#define MB_PDU_FUNC_WRITE_MUL_COILCNT_MAX ( 0x07B0 )
/* ----------------------- Static functions ---------------------------------*/
eMBException prveMBError2Exception( eMBErrorCode eErrorCode );
eMBException eMBFUNcZiDingYi ( UCHAR * pucFrame, USHORT * usLen )
{
/*这里写功能码9所要操作的命令*/
USHORT usRegAddress;//从机地址
USHORT usCoilCount;//线圈寄存器个数
UCHAR ucNBytes;
UCHAR *pucFrameCur;//发送缓冲区
eMBException eStatus = MB_EX_NONE;
eMBErrorCode eRegStatus;
if( *usLen == ( MB_PDU_FUNC_READ_SIZE + MB_PDU_SIZE_MIN/*1*/ ) )
{
usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF] << 8 );
usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF + 1] );
usCoilCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_COILCNT_OFF] << 8 );
usCoilCount |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_COILCNT_OFF + 1] );
if( ( usCoilCount >= 1 ) &&( usCoilCount < MB_PDU_FUNC_READ_COILCNT_MAX ) )
{
/*
*pucFrame --> PDU的首地址
在这个.c文件里,
-------------------------------------------------------------------------
|Funcode | the number of the Byte | Data
-------------------------------------------------------------------------
pucFrame[0] pucFrame[1] pucFrame[2]
*/
/* Set the current PDU data pointer to the beginning. */
pucFrameCur/*发送缓冲区*/ = &pucFrame[MB_PDU_FUNC_OFF];/*初始化PDU长度*/
*usLen = MB_PDU_FUNC_OFF;
/* First byte contains the function code. */
*pucFrameCur++ = MB_FUNC_READ_INPUT_REGISTER;/*先赋值“=”功能码,然后再自增“++”*/
*usLen += 1;
/* Second byte in the response contain the number of bytes. */
if( ( usCoilCount & 0x0007 ) != 0 )
{
ucNBytes = ( UCHAR )( usCoilCount / 8 + 1 );
}
else
{
ucNBytes = ( UCHAR )( usCoilCount / 8 );
}
*pucFrameCur++ = ucNBytes; ;
*usLen += 1;
if( eRegStatus != MB_ENOERR )
{
eStatus = prveMBError2Exception( eRegStatus );
}
else
{
*pucFrameCur++ ="0A 0B 0B 0B 0C 0D" //写法不对,大概是这个意思,发送"0A 0B 0B 0B 0C 0D"
}
}
else
{
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
}
}
else
{
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
}
return eStatus;
}
这样是不是就是可以自定义1的功能码了????????????????????????????????????????????????????????????
我来回答