MODBUS协议.docx
- 文档编号:27342766
- 上传时间:2023-06-29
- 格式:DOCX
- 页数:10
- 大小:16.63KB
MODBUS协议.docx
《MODBUS协议.docx》由会员分享,可在线阅读,更多相关《MODBUS协议.docx(10页珍藏版)》请在冰豆网上搜索。
MODBUS协议
/*
*MODBUS协议
*
*
*介绍:
*此modbus上位机协议类具有较强的通用性
*本协议类最主要的思想是把所有向下位机发送的指令先存放在缓冲区中(命名为管道)
*再将管道中的指令逐个发送出去。
*管道遵守FIFO的模式。
管道中所存放指令的个数在全局变量中定义。
*管道内主要分为两部分:
1,定时循环发送指令。
2,一次性发送指令。
*定时循环发送指令:
周期性间隔时间发送指令,一般针对“输入寄存器”或“输入线圈”等实时更新的变量。
*这两部分的长度由用户所添加指令个数决定(所以自由性强)。
*指令的最大发送次数,及管道中最大存放指令的个数在常量定义中可进行设定。
*
*使用说明:
*1,首先对所定义的寄存器或线圈进行分组定义,并定义首地址。
*2,在MBDataTable数组中添加寄存器或线圈所对应的地址。
注意寄存器:
ob=newUInt16()。
线圈:
ob=newbyte()。
*3,对所定义的地址用属性进行定义以方便在类外进行访问及了解所对应地址的含义。
*4,GetAddressValueLength函数中对使用说明的"第一步"分组的元素个数进行指定。
*5,在主程序中调用MBConfig进行协议初始化(初始化内容参考函数)。
*6,在串口中断函数中调用MBDataReceive()。
*7,定时器调用MBRefresh()。
(10ms以下)
*指令发送间隔时间等于实时器乘以10。
例:
定时器5ms调用一次指令发送间隔为50ms。
*8,在主程序初始化中添加固定实时发送的指令操作用MBAddRepeatCmd函数。
*9,在主程序运行过程中根据需要添加单个的指令操作(非固定重复发送的指令)用MBAddCmd函数。
*
*
*作者:
王宏强
*时间:
2012.7.2
*
*
*
*
*
*
*/
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.IO.Ports;
namespaceWindowsApplication1
{
publicclassModbus
{
#region所用结构体
///
///地址对应表元素单元
///
publicstructOPTable{
publicvolatileintaddr;
publicvolatilebytetype;
publicvolatileobjectob;
};
///
///当前的指令
///
publicstructMBCmd
{
publicvolatileintaddr;//指令首地址
publicvolatileintstat;//功能码
publicvolatileintlen;//所操作的寄存器或线圈的个数
publicvolatileintres;//返回码的状态,0:
无返回,1:
正确返回};
///
///当前操作的指令管道
///
publicstructMBSci
{
publicvolatileMBCmd[]cmd;//指令结构体
publicvolatileintindex;//当前索引
publicvolatileintcount;//当前功能码执行的次数
publicvolatileintmaxRepeatCount;//最大发送次数
publicvolatileintrtCount;//实时读取的指令各数(无限间隔时间读取)};
#endregion
#region常量定义
publicconstbyteMB_READ_COILS=0x01;//读线圈寄存器
publicconstbyteMB_READ_DISCRETE=0x02;//读离散输入寄存器
publicconstbyteMB_READ_HOLD_REG=0x03;//读保持寄存器
publicconstbyteMB_READ_INPUT_REG=0x04;//读输入寄存器
publicconstbyteMB_WRITE_SINGLE_COIL=0x05;//写单个线圈
publicconstbyteMB_WRITE_SINGLE_REG=0x06;//写单寄存器
publicconstbyteMB_WRITE_MULTIPLE_COILS=0x0f;//写多线圈
publicconstbyteMB_WRITE_MULTIPLE_REGS=0x10;//写多寄存器
privateconstintMB_MAX_LENGTH=120;//最大数据长度
privateconstintMB_SCI_MAX_COUNT=15;//指令管道最大存放的指令各数
privateconstintMB_MAX_REPEAT_COUNT=3;//指令最多发送次数
#endregion
#region全局变量
privatestaticvolatileboolsciLock=false;//调度器锁true:
加锁false:
解锁
privatestaticvolatilebyte[]buff=newbyte[MB_MAX_LENGTH];//接收缓冲器
privatestaticvolatileintbuffLen=0;
privatestaticvolatilebyte[]rBuff=null;//正确接收缓冲器
privatestaticvolatilebyte[]wBuff=null;//正确发送缓冲器
publicstaticMBScigMBSci=newMBSci(){cmd=newMBCmd[MB_SCI_MAX_COUNT],index=0,maxRepeatCount=MB_MAX_REPEAT_COUNT,rtCount=0,count=0};
privatestaticSerialPortcomm=null;
privatestaticintmbRefreshTime=0;
#endregion
#regionMODBUS地址对应表
//modbus寄存器和线圈分组首地址定义
publicconstintD_DIO=0x0000;
publicconstintD_BASE=0x0014;
publicconstintD_RANGE=0x0018;
publicconstintD_PWM=0x001A;
publicconstintD_PID=0x001E;
///
///变量所对应的地址在此位置
///
publicstaticvolatileOPTable[]MBDataTable=
{
newOPTable(){addr=D_DIO,type=MB_READ_INPUT_REG,ob=newUInt16()},//0
newOPTable(){addr=D_DIO+1,type=MB_READ_INPUT_REG,ob=newUInt16()},
newOPTable(){addr=D_DIO+2,type=MB_READ_INPUT_REG,ob=newUInt16()},
newOPTable(){addr=D_DIO+3,type=MB_READ_INPUT_REG,ob=newUInt16()},
newOPTable(){addr=D_DIO+4,type=MB_READ_INPUT_REG,ob=newInt16()},
newOPTable(){addr=D_DIO+5,type=MB_READ_INPUT_REG,ob=newInt16()},
newOPTable(){addr=D_BASE,type=MB_READ_HOLD_REG,ob=newInt16()},//6
newOPTable(){addr=D_BASE+1,type=MB_READ_HOLD_REG,ob=newInt16()},
newOPTable(){addr=D_BASE+2,type=MB_READ_HOLD_REG,ob=newInt16()},
newOPTable(){addr=D_BASE+3,type=MB_READ_HOLD_REG,ob=newInt16()},
newOPTable(){addr=D_RANGE,type=MB_READ_HOLD_REG,ob=newInt16()},//10
newOPTable(){addr=D_RANGE+1,type=MB_READ_HOLD_REG,ob=newInt16()},
newOPTable(){addr=D_PWM,type=MB_READ_HOLD_REG,ob=newInt16()},//12
newOPTable(){addr=D_PWM+1,type=MB_READ_HOLD_REG,ob=newInt16()},
newOPTable(){addr=D_PWM+2,type=MB_READ_HOLD_REG,ob=newInt16()},
newOPTable(){addr=D_PWM+3,type=MB_READ_HOLD_REG,ob=newInt16()},
newOPTable(){addr=D_PID,type=MB_READ_HOLD_REG,ob=newUInt16()},//16
newOPTable(){addr=D_PID+1,type=MB_READ_HOLD_REG,ob=newUInt16()},
newOPTable(){addr=D_PID+2,type=MB_READ_HOLD_REG,ob=newUInt16()},
newOPTable(){addr=D_PID+3,type=MB_READ_HOLD_REG,ob=newUInt16()},
newOPTable(){addr=D_PID+4,type=MB_READ_HOLD_REG,ob=newUInt16()},
newOPTable(){addr=D_PID+5,type=MB_READ_HOLD_REG,ob=newUInt16()},
};
publicstaticUInt16gDioX{get{returnConvert.ToUInt16(MBDataTable[0].ob);}set{MBDataTable[0].ob=value;}}
publicstaticUInt16gDioY{get{returnConvert.ToUInt16(MBDataTable[1].ob);}set{MBDataTable[1].ob=value;}}
publicstaticUInt16gDioZ{get{returnConvert.ToUInt16(MBDataTable[2].ob);}set{MBDataTable[2].ob=value;}}
publicstaticUInt16gDioD{get{returnConvert.ToUInt16(MBDataTable[3].ob);}set{MBDataTable[3].ob=value;}}
publicstaticInt16gDioXx{get{return(Int16)Convert.ToInt32(MBDataTable[4].ob);}set{MBDataTable[4].ob=value;}}
publicstaticInt16gDioXy{get{return(Int16)Convert.ToInt32(MBDataTable[5].ob);}set{MBDataTable[5].ob=value;}}
publicstaticInt16gBaseF1{get{return(Int16)Convert.ToInt32(MBDataTable[6].ob);}set{MBDataTable[6].ob=value;}}
publicstaticInt16gBaseF2{get{return(Int16)Convert.ToInt32(MBDataTable[7].ob);}set{MBDataTable[7].ob=value;}}
publicstaticInt16gBaseF3{get{return(Int16)Convert.ToInt32(MBDataTable[8].ob);}set{MBDataTable[8].ob=value;}}
publicstaticInt16gBaseF4{get{return(Int16)Convert.ToInt32(MBDataTable[9].ob);}set{MBDataTable[9].ob=value;}}
publicstaticInt16gRangeMax{get{return(Int16)Convert.ToInt32(MBDataTable[10].ob);}set{MBDataTable[10].ob=value;}}
publicstaticInt16gRangeMin{get{return(Int16)Convert.ToInt32(MBDataTable[11].ob);}set{MBDataTable[11].ob=value;}}
publicstaticInt16gPwmF1{get{return(Int16)Convert.ToInt32(MBDataTable[12].ob);}set{MBDataTable[12].ob=value;}}
publicstaticInt16gPwmF2{get{return(Int16)Convert.ToInt32(MBDataTable[13].ob);}set{MBDataTable[13].ob=value;}}
publicstaticInt16gPwmF3{get{return(Int16)Convert.ToInt32(MBDataTable[14].ob);}set{MBDataTable[14].ob=value;}}
publicstaticInt16gPwmF4{get{return(Int16)Convert.ToInt32(MBDataTable[15].ob);}set{MBDataTable[15].ob=value;}}
publicstaticfloatgP
{
get
{
inttmp=(Convert.ToInt32(MBDataTable[16].ob)&0xFFFF)|((Convert.ToInt32(MBDataTable[17].ob)&0xFFFF)<<16);
byte[]arr=BitConverter.GetBytes(tmp);
returnBitConverter.ToSingle(arr,0);
}
set
{
byte[]val=BitConverter.GetBytes(value);
MBDataTable[16].ob=BitConverter.ToUInt16(val,0);
MBDataTable[17].ob=BitConverter.ToUInt16(val,2);
}
}
publicstaticfloatgI
{
get
{
inttmp=(Convert.ToInt32(MBDataTable[18].ob)&0xFFFF)|((Convert.ToInt32(MBDataTable[19].ob)&0xFFFF)<<16);
byte[]arr=BitConverter.GetBytes(tmp);
returnBitConverter.ToSingle(arr,0);
}
set
{
byte[]val=BitConverter.GetBytes(value);
MBDataTable[18].ob=BitConverter.ToUInt16(val,0);
MBDataTable[19].ob=BitConverter.ToUInt16(val,2);
}
}
publicstaticfloatgD
{
get
{
inttmp=(Convert.ToInt32(MBDataTable[20].ob)&0xFFFF)|((Convert.ToInt32(MBDataTable[21].ob)&0xFFFF)<<16);
byte[]arr=BitConverter.GetBytes(tmp);
returnBitConverter.ToSingle(arr,0);
}
set
{
byte[]val=BitConverter.GetBytes(value);
MBDataTable[20].ob=BitConverter.ToUInt16(val,0);
MBDataTable[21].ob=BitConverter.ToUInt16(val,2);
}
}
publicstaticUInt16gNode=100;
继续阅读
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- MODBUS 协议