OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 2680|回复: 1

freemodbus自定义功能码和功能

[复制链接]

9

主题

38

帖子

0

精华

初级会员

Rank: 2

积分
53
金钱
53
注册时间
2019-8-21
在线时间
65 小时
发表于 2019-11-21 15:41:16 | 显示全部楼层 |阅读模式
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的功能码了????????????????????????????????????????????????????????????

正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

51

主题

2166

帖子

2

精华

论坛元老

Rank: 8Rank: 8

积分
10653
金钱
10653
注册时间
2017-4-14
在线时间
2780 小时
发表于 2019-11-21 18:55:27 | 显示全部楼层
自带的功能码难道还不够你用
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-5-25 13:12

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表