Login
升级VIP 登录 注册 安全退出
当前位置: 首页 > word文档 > 合同模板 > HART协议通用命令用法实例(C#)

HART协议通用命令用法实例(C#)

收藏

本作品内容为HART协议通用命令用法实例(C#),格式为 doc ,大小 170024 KB ,页数为 28页

HART协议通用命令用法实例(C#)


('usingSystem;usingSystem.Collections.Generic;usingSystem.Data;usingSystem.Data.SqlClient;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;//HART协议说明//////HART命令查询帧格式:0xFF+0xFF+定界符+地址+数据字节数+数据+校验和//1、前导符:2-20字节,用于与接收器之间的同步//首次开始通信或数据帧重发时,使用20字节前导码,前导码都是0xFF。//2、定界符:1字节,短帧:01,02,06对应成组、主从、从主三种模式,长帧时将短帧最高位置1即可//3、长帧和短帧区别在于地址的字节数不同,短帧地址为1字节,用于初始化时命令0查询设备标志、ID等信息//长帧地址:5字节(40位),建立在设备唯一标识符的基础上//唯一标识符分为三个部分://第一字节最高位,1表示基本主设备,0表示副主设备//第一字节第六位(次高位),1表示处于成组模式,0表示没有成组模式//长地址的其它部分是设备的唯一标识符,共38位,均为0表示广播地址//二.二短帧地址:1字节//最高位1表示主设备,0表示副主设备//第6位1表示处于成组模式,0表示没有处于成组模式//第4、5位必须置0//最低四位表示巡检地址,对应设备巡检地址0-15//4、命令字节:1字节,表明该帧所封装的HART命令,命令字节值在从设备响应中原值返回//5、字节奇数:1字节,表明此字节与最后帧校验字节之间的数据字节个数,用于识别帧的结束,范围0-27//6、数据域:由整数字节的用户数据组成,长度为0-25字节//在主-从帧中,数据域存放用户对设备的请求数据//在从-主帧中,数据用于存放设备对用户的响应数据//7、校验字节:1字节,对不包括前导符和该字节的帧中所有字节进行水平校验(异或)的结果//垂直校验时通信过程中硬件自动对每个字节所有位进行奇偶校验后产生的结果//水平校验时所有字节依次按位进行异或运算后的结果//8、响应码:只在从帧中出现//响应码第一字节指明通信状态,第二字节表示现场设备工作状态//该帧数据只有在响应码第一字节最高位为0时有效,1表示通信错误namespaceHart{publicpartialclassfrom1:Form{//HART对应串口初始化PrivateSerialPortserial_hart=newSerialPort();PrivatevoidportInit(){serial_hart.PortName=hart[0]["port"].ToString();serial_hart.BaudRate=Convert.ToInt32(hart[0]["baudrate"]);serial_hart.ReceivedBytesThreshold=1;serial_hart.Parity=Parity.Odd;try{serial_hart.Open();intn=serial_hart.BytesToRead;//串口缓存区待读取数据个数byte[]buffirstclear=newbyte[n];serial_hart.Read(buffirstclear,0,n);//将串口缓存区之前的数据清理掉,防止过量非预期数据进入解析环节buffirstclear=newbyte[1];//释放变量空间}catch(Exceptionex){if(ex.ToString()!=ex1){ex1=ex.ToString();MessageBox.Show(ex.Message);}}}#region设备信息privateintmanufacID=18;//制造商IDprivateintdevType=8;//制造商设备类型privateintsfVersion=1;//软件版本号privateinthwVersion=0;//硬件版本号privateint[]devID=newint[3];//设备IDprivateintcmdVersion=5;//通用命令文档版本号privateintdevIDResult=0;//设备ID计算结果#endregion#region主变量(PV)privateintPVUnitCd=0;//主变量代码privatefloatPVValue=0;//主变量privatefloatPVCurrent=0;//主变量电流privatefloatPVPercentRange=0;//主变量量程百分比privateintPVSensNum=0;//主变量传感器编号privateintPVSensUnitCd=0;//传感器极限、最小精度单位代码privatefloatPVUpSensLmt=0;//传感器上限privatefloatPVLowSensLmt=0;//传感器下限privatefloatPVMiniSpan=0;//主变量最小精度privateintPVAlarmSelCd=0;//主变量报警选择代码privateintPVTransFuncCd=0;//主变量传递功能代码privateintPVRangeUnitCd=0;//主变量上下量程单位代码privatefloatPVUpRangeValue=0;//主变量上限值privatefloatPVLowRangValue=0;//主变量下限值privatefloatPVDampValue=0;//主变量阻尼值,单位秒privateintWriteProtectCd=251;//写保护代码privateintPLDCd=18;//商标发行商代码,18-ABB公司#endregion#region第二...第四变量privateintSecVUnitCd=0;//第二变量代码privatefloatSecVValue=0;//第二变量值privateintThirdVUnitCd=0;//第三变量代码privatefloatThirdVValue=0;//第三变量值privateintForthVUnitCd=0;//第四变量代码privatefloatForthVValue=0;//第四变量值#endregion#region标签、描述符、日期(日月年)privatestringhartTag="";//标签privatestringhartDspt="";//描述privatestringhartDate="";//日期#endregion#region通用命令帧头privatebyte[]cmdHead=newbyte[14];//帧头包含导引符、定界符、地址privateintcmdHeadInitFlag=0;//帧头初始化标志#endregion#region常量表、动态变量表publicDataTablehartConstant=newDataTable();publicDataTablehartDynamic=newDataTable();#endregionprivateintIDHartConstant=0;//常量表IDprivateintIDHartDynamic=0;//动态变量表IDprivatestringerrorstr="";//错误信息字符串privatestringhartMsg="";//读设备消息返回结果publicstring[]HartParaShow=newstring[6];privateintFinalAssembleNum=0;//最终装配号privateint[]hartGetCmdOk=newint[9];//9个通用查询命令,查询成功后对应单元数置1#region常量表初始化privatevoidHartConstantInit(){hartConstant.Columns.Add("ID",typeof(int));hartConstant.Columns.Add("Time",typeof(string));hartConstant.Columns.Add("制造商ID",typeof(int));hartConstant.Columns.Add("制造商设备类型",typeof(int));hartConstant.Columns.Add("通用命令文档版本号",typeof(int));hartConstant.Columns.Add("设备软件版本号",typeof(int));hartConstant.Columns.Add("设备硬件版本号",typeof(int));hartConstant.Columns.Add("设备ID号",typeof(int));hartConstant.Columns.Add("设备消息",typeof(string));hartConstant.Columns.Add("标签Tag",typeof(string));hartConstant.Columns.Add("描述符",typeof(string));hartConstant.Columns.Add("日期",typeof(string));hartConstant.Columns.Add("传感器序列号",typeof(int));hartConstant.Columns.Add("传感器上下限单位代码",typeof(int));hartConstant.Columns.Add("传感器上限",typeof(float));hartConstant.Columns.Add("传感器下限",typeof(float));hartConstant.Columns.Add("主变量最小精度",typeof(float));hartConstant.Columns.Add("报警选择代码",typeof(int));hartConstant.Columns.Add("传递功能代码",typeof(int));hartConstant.Columns.Add("上下量程值代码",typeof(int));hartConstant.Columns.Add("主变量上限值",typeof(float));hartConstant.Columns.Add("主变量下限值",typeof(float));hartConstant.Columns.Add("主变量阻尼值",typeof(float));hartConstant.Columns.Add("写保护代码",typeof(int));hartConstant.Columns.Add("最终装配号",typeof(int));}#endregion#region动态变量表初始化privatevoidHartDynamicInit(){hartDynamic.Columns.Add("ID",typeof(int));hartDynamic.Columns.Add("Time",typeof(string));hartDynamic.Columns.Add("设备ID号",typeof(int));hartDynamic.Columns.Add("主变量单位代码",typeof(int));hartDynamic.Columns.Add("主变量值",typeof(float));hartDynamic.Columns.Add("主变量电流",typeof(float));hartDynamic.Columns.Add("主变量量程百分比",typeof(float));hartDynamic.Columns.Add("第二变量单位代码",typeof(int));hartDynamic.Columns.Add("第二变量值",typeof(int));hartDynamic.Columns.Add("第三变量单位代码",typeof(int));hartDynamic.Columns.Add("第三变量值",typeof(int));hartDynamic.Columns.Add("第四变量单位代码",typeof(int));hartDynamic.Columns.Add("第四变量值",typeof(int));}#endregion#region发送命令0(读标识码)查询请求privatevoidhartSendCmd0()//发送命令0(读标识码)查询请求{if(hartopenflag==1){byte[]cmd=newbyte[13];for(inti=0;i<8;i++){cmd[i]=0xff;//第一次通信前导符为20个0xFF}cmd[8]=0x02;//短帧,主从模式cmd[9]=0x80;//主设备cmd[10]=0x00;//命令0cmd[11]=0x00;//数据长度0cmd[12]=Convert.ToByte(cmd[8]^cmd[9]^cmd[10]^cmd[11]);//水平校验serial_hart.Write(cmd,0,13);}}#endregion#region发送无请求数据的长帧通用方法privatevoidhartLongFrameCmdsend(bytecmdn)//发送无请求数据的长帧通用方法{byte[]cmd=newbyte[17];if(cmdHeadInitFlag==1)//获取到设备信息后{for(inti=0;i<14;i++){cmd[i]=cmdHead[i];//帧头赋值}cmd[14]=1;//命令1cmd[15]=cmdn;//无请求数据cmd[16]=cmd[8];for(inti=9;i<16;i++){cmd[16]=Convert.ToByte(cmd[16]^cmd[i]);//校验和}serial_hart.Write(cmd,0,17);//发送请求}}#endregion#region无请求数据的长地址查询privatevoidhartSendCmd1()//发送通用命令1(读主变量)查询请求{hartLongFrameCmdsend(1);}privatevoidhartSendCmd2()//发送通用命令2(读主变量电流值和百分比)查询请求{hartLongFrameCmdsend(2);}privatevoidhartSendCmd3()//发送通用命令3(读动态变量和主变量电流)查询请求{hartLongFrameCmdsend(3);}privatevoidhartSendCmd12()//发送通用命令12(读消息)查询请求{hartLongFrameCmdsend(12);}privatevoidhartSendCmd13()//发送通用命令13(读标签Tag)查询请求{hartLongFrameCmdsend(13);}privatevoidhartSendCmd14()//发送通用命令14(读主变量传感器信息)查询请求{hartLongFrameCmdsend(14);}privatevoidhartSendCmd15()//发送通用命令15(读主变量输出信息)查询请求{hartLongFrameCmdsend(15);}privatevoidhartSendCmd16()//发送通用命令16(读最终装配号)查询请求{hartLongFrameCmdsend(16);}privatevoidhartSendCmd48()//发送常用命令48(读附加器件状态)查询请求{hartLongFrameCmdsend(48);}#endregion#region命令0返回值解析(标识码)privatevoidhartGetCmd0()//获取命令0设备返回的数据并解析{//预期接收到设备返回的数据为://FFFFFFFFFFFFFFFF0680000E0000FE12080805000100或010000设备ID高字节设备ID低字节intn=serial_hart.BytesToRead;intbegin=0;byte[]buf;if(n>24){buf=newbyte[n];serial_hart.Read(buf,0,n);for(inti=0;i<9;i++){if((buf[i]==0x06)&(buf[i+1]==0x80)&(buf[i+2]==0x0)&(buf[i+3]==12)){begin=i;if((buf[i+4]==0)&(buf[i+5]==0)){manufacID=buf[i+7];devType=buf[i+8];cmdVersion=buf[i+10];sfVersion=buf[i+12];hwVersion=buf[i+13];devID[0]=buf[i+15];devID[1]=buf[i+16];devID[2]=buf[i+17];devIDResult=devID[1]256+devID[2];hartGetCmdOk[0]=1;//返回值接收成功标志置1cmdHeadInit();//初始化命令帧头}else{errorstr="数据长度有误!";pictureBox_Normal.Image=Properties.Resources.异常;label_Right.Text="异常";label_Hint.Text="Hart通用命令0"+errorstr;hartSendCmd0();//未能获取到设备信息则重新发送请求}}}}}#endregion#region数据解析查找帧头相关变量privateintheadbegin=0;//分界符位置privateintheadFindFlag=0;//找到分解符标志#endregion#region判断返回值是否正确,并找到帧头(分界符位置)//判断帧头(定界符)位置privatevoidfindFrameHead(byte[]buf1,bytecmdn,bytedatanum){for(inti=0;i<10;i++){if((buf1[i]==0x86)&(buf1[i+1]==cmdHead[9])&(buf1[i+2]==cmdHead[10])&(buf1[i+3]==cmdHead[11])&(buf1[i+4]==cmdHead[12])&(buf1[i+5]==cmdHead[13])&(buf1[i+6]==cmdn)&(buf1[i+7]==datanum)){if((buf1[i+8]==0)&(buf1[i+9]==0)){headbegin=i;headFindFlag=1;}if((buf1[i+8]==0)&(buf1[i+9]==5)){errorstr="命令"+cmdn.ToString()+"返回结果异常,字节数不对!!";pictureBox_Normal.Image=Properties.Resources.异常;label_Right.Text="异常";label_Hint.Text=errorstr;}}}}#endregion#region查询命令通用帧头设置privatevoidcmdHeadInit()//初始化命令帧头,只有在获取到设备信息后才会有此标志为1{for(inti=0;i<8;i++){cmdHead[i]=0xff;}cmdHead[8]=0x82;cmdHead[9]=Convert.ToByte(manufacID);cmdHead[9]=0x80;cmdHead[10]=Convert.ToByte(devType);for(inti=0;i<3;i++){cmdHead[i+11]=Convert.ToByte(devID[i]);}cmdHeadInitFlag=1;}#endregion#region命令1返回值解析(主变量PV)//命令1返回值解析//预期返回值为:FFFFFFFFFFFFFFFF86#add1#add2#add3#add4#add50x010x050000#0#1...#4/////////前导符///定界符//地址//命令//数据长度//状态//主变量单位代码//主变量privatevoidhartGetCmd1(){intn=serial_hart.BytesToRead;byte[]buf;bytecmdn=1;bytedatanum=5;if(n>18){buf=newbyte[n];serial_hart.Read(buf,0,n);findFrameHead(buf,cmdn,datanum);if(headFindFlag==1){PVUnitCd=buf[headbegin+10];PVValue=BitConverter.ToSingle(buf,headbegin+11);hartGetCmdOk[1]=1;headFindFlag=0;headbegin=0;}else{hartSendCmd1();}}}#endregion#region命令2返回值解析(主变量电流值和百分比)//命令2返回值解析//预期返回值为:FFFFFFFFFFFFFFFF86#add1#add2#add3#add4#add50x020x180000#0..#3#4...#7/////////8个前导符///定界符//5字节地址//命令//数据长度//状态//主变量电流//主变量量程百分比privatevoidhartGetCmd2(){intn=serial_hart.BytesToRead;byte[]buf;bytecmdn=2;bytedatanum=8;if(n>18){buf=newbyte[n];serial_hart.Read(buf,0,n);findFrameHead(buf,cmdn,datanum);if(headFindFlag==1){PVCurrent=BitConverter.ToSingle(buf,headbegin+10);PVPercentRange=BitConverter.ToSingle(buf,headbegin+14);hartGetCmdOk[2]=1;headFindFlag=0;headbegin=0;}else{hartSendCmd2();}}}#endregion#region命令3返回值解析(主变量电流和动态变量)//命令3返回值解析//预期返回值为:FFFFFFFFFFFFFFFF86#add1#add2#add3#add4#add50x030x180000#0..#3#4/////////8个前导符///定界符//5字节地址//命令//数据长度//状态//主变量电流//主变量单位代码//#5...#8#9#10...#13#14#15...#18#19#20...#23//主变量值第二变量代码第二变量值第三变量代码第三变量值第四变量代码第四变量值privatevoidhartGetCmd3(){intn=serial_hart.BytesToRead;byte[]buf;bytecmdn=3;bytedatanum=24;if(n>34){buf=newbyte[n];serial_hart.Read(buf,0,n);findFrameHead(buf,cmdn,datanum);if(headFindFlag==1){PVCurrent=BitConverter.ToSingle(buf,headbegin+10);PVUnitCd=buf[headbegin+14];PVValue=BitConverter.ToSingle(buf,headbegin+15);SecVUnitCd=buf[headbegin+19];SecVValue=BitConverter.ToSingle(buf,headbegin+20);ThirdVUnitCd=buf[headbegin+24];ThirdVValue=BitConverter.ToSingle(buf,headbegin+25);ForthVUnitCd=buf[headbegin+29];ForthVValue=BitConverter.ToSingle(buf,headbegin+30);hartGetCmdOk[3]=1;headFindFlag=0;headbegin=0;}else{hartSendCmd3();}}}#endregion#region命令12返回值解析(设备消息)//该命令用于读取设备消息////预期返回值为:FFFFFFFFFFFFFFFF86#add1#add2#add3#add4#add50x0C0x180000#0..#23//////8个前导符///定界符//5字节地址//命令//数据长度//状态//设备消息(ASCII)privatevoidhartGetCmd12(){intn=serial_hart.BytesToRead;byte[]buf;bytecmdn=12;bytedatanum;if(n>18){buf=newbyte[n];serial_hart.Read(buf,0,n);datanum=Convert.ToByte(n-18);//是24还是实际字符串长度不确定findFrameHead(buf,cmdn,datanum);if(headFindFlag==1){StringBuilderstrb=newStringBuilder();for(inti=0;i31){buf=newbyte[n];serial_hart.Read(buf,0,n);findFrameHead(buf,cmdn,datanum);if(headFindFlag==1){StringBuilderstrb=newStringBuilder();for(inti=0;i<6;i++){strb.Append((char)buf[headbegin+10+i]);}hartTag=strb.ToString();strb.Clear();for(inti=6;i<18;i++){strb.Append((char)buf[headbegin+10+i]);}hartDspt=strb.ToString();hartDate=buf[headbegin+20].ToString()+"年"+buf[headbegin+19].ToString()+"月"+buf[headbegin+18].ToString()+"日";hartGetCmdOk[5]=1;headFindFlag=0;headbegin=0;}else{hartSendCmd13();}}}#endregion#region命令14返回值解析(主变量传感器信息)//命令14返回值解析//预期返回值为:FFFFFFFFFFFFFFFF86#add1#add2#add3#add4#add50x0E0x100000#0..#2#3/////////8个前导符///定界符//5字节地址//命令//数据长度//状态//传感器序列号//单位代码//#4...#7#8...#11#12...#15//传感器上限传感器下限主变量最小精度privatevoidhartGetCmd14(){intn=serial_hart.BytesToRead;byte[]buf;bytecmdn=14;bytedatanum=16;if(n>26){buf=newbyte[n];serial_hart.Read(buf,0,n);findFrameHead(buf,cmdn,datanum);if(headFindFlag==1){PVSensNum=(buf[headbegin+10]<<16)&0xff0000+(buf[headbegin+11]<<8)&0xff00+buf[headbegin+12];PVSensUnitCd=buf[headbegin+13];PVUpSensLmt=BitConverter.ToSingle(buf,headbegin+14);PVLowSensLmt=BitConverter.ToSingle(buf,headbegin+18);PVMiniSpan=BitConverter.ToSingle(buf,headbegin+22);hartGetCmdOk[6]=1;headFindFlag=0;headbegin=0;}else{hartSendCmd14();}}}#endregion#region命令15返回值解析(主变量输出信息)//命令15返回值解析//预期返回值为:FFFFFFFFFFFFFFFF86#add1#add2#add3#add4#add50x0F0x100000#0#1/////////8个前导符///定界符//5字节地址//命令//数据长度//状态//报警选择代码//传递功能代码//#2#3...#6#7...#10#11..#14#15#16//上下量程单位代码主变量上限主变量下限阻尼值写保护代码发行商代码/privateintPVAlarmSelCd=0;//主变量报警选择代码privateintPVTransFuncCd=0;//主变量传递功能代码privateintPVRangeUnitCd=0;//主变量上下量程单位代码privatefloatPVUpRangeValue=0;//主变量上限值privatefloatPVLowRangValue=0;//主变量下限值privatefloatPVDampValue=0;//主变量阻尼值,单位秒privateintWriteProtectCd=251;//写保护代码privateintPLDCd=18;//商标发行商代码,18-ABB公司privatevoidhartGetCmd15(){intn=serial_hart.BytesToRead;byte[]buf;bytecmdn=15;bytedatanum=17;if(n>27){buf=newbyte[n];serial_hart.Read(buf,0,n);findFrameHead(buf,cmdn,datanum);if(headFindFlag==1){PVAlarmSelCd=buf[headbegin+10];PVTransFuncCd=buf[headbegin+11];PVRangeUnitCd=buf[headbegin+12];PVUpRangeValue=BitConverter.ToSingle(buf,headbegin+13);PVLowRangValue=BitConverter.ToSingle(buf,headbegin+17);PVDampValue=BitConverter.ToSingle(buf,headbegin+21);WriteProtectCd=buf[headbegin+25];PLDCd=buf[headbegin+26];hartGetCmdOk[7]=1;headFindFlag=0;headbegin=0;}else{hartSendCmd15();}}}#endregion#region命令16返回值解析(最终装配号)//命令15返回值解析//预期返回值为:FFFFFFFFFFFFFFFF86#add1#add2#add3#add4#add50x0F0x100000#0#1/////////8个前导符///定界符//5字节地址//命令//数据长度//状态//报警选择代码//传递功能代码//#2#3...#6#7...#10#11..#14#15#16//上下量程单位代码主变量上限主变量下限阻尼值写保护代码发行商代码privatevoidhartGetCmd16(){intn=serial_hart.BytesToRead;byte[]buf;bytecmdn=16;bytedatanum=3;if(n>27){buf=newbyte[n];serial_hart.Read(buf,0,n);findFrameHead(buf,cmdn,datanum);if(headFindFlag==1){FinalAssembleNum=buf[headbegin+10]65536+buf[headbegin+11]256+buf[headbegin+12];hartGetCmdOk[8]=1;headFindFlag=0;headbegin=0;}else{hartSendCmd16();}}}#endregionprivatevoidhartSaveConstantTable()//常量存入常量表{DataRowhartc=hartConstant.NewRow();hartc["ID"]=IDHartConstant;hartc["Time"]=DateTime.Now.ToString("yyyy-MM-ddHH:mm:ss.fff");hartc["制造商ID"]=manufacID;hartc["制造商设备类型"]=devType;hartc["通用命令文档版本号"]=cmdVersion;hartc["设备软件版本号"]=sfVersion;hartc["设备硬件版本号"]=hwVersion;hartc["设备ID号"]=devID;hartc["设备消息"]=hartMsg;hartc["标签Tag"]=hartTag;hartc["描述符"]=hartDspt;hartc["日期"]=hartDate;hartc["传感器序列号"]=PVSensNum;hartc["传感器上下限单位代码"]=PVSensUnitCd;hartc["传感器上限"]=PVUpSensLmt;hartc["传感器下限"]=PVLowSensLmt;hartc["主变量最小精度"]=PVMiniSpan;hartc["报警选择代码"]=PVAlarmSelCd;hartc["传递功能代码"]=PVTransFuncCd;hartc["上下量程值代码"]=PVRangeUnitCd;hartc["主变量上限值"]=PVUpRangeValue;hartc["主变量下限值"]=PVLowRangValue;hartc["主变量阻尼值"]=PVDampValue;hartc["写保护代码"]=WriteProtectCd;hartc["最终装配号"]=PLDCd;hartConstant.Rows.Add(hartc);}privatevoidstrHartCSet(StringBuilderstr)//字符串赋值{str.Append("ID,");str.Append("Time,");str.Append("制造商ID,");str.Append("制造商设备类型,");str.Append("通用命令文档版本号,");str.Append("设备软件版本号,");str.Append("设备硬件版本号,");str.Append("设备ID号,");str.Append("设备消息,");str.Append("标签Tag,");str.Append("描述符,");str.Append("日期,");str.Append("传感器序列号,");str.Append("传感器上下限单位代码,");str.Append("传感器上限,");str.Append("传感器下限,");str.Append("主变量最小精度,");str.Append("报警选择代码,");str.Append("传递功能代码,");str.Append("上下量程值代码,");str.Append("主变量上限值,");str.Append("主变量下限值,");str.Append("主变量阻尼值,");str.Append("写保护代码,");str.Append("最终装配号");}publicint[]hartViewItemSct=newint[6];/privatestring[]hartConstItem={"ID","Time","制造商ID","制造商设备类型","通用命令文档版本号","设备软件版本号","","","","","","","","","","","","","","","","","","","",};/privatevoidstrHartDSet(StringBuilderstr){str.Append("ID,");str.Append("Time,");str.Append("设备ID号,");str.Append("主变量单位代码,");str.Append("主变量值,");str.Append("主变量电流,");str.Append("主变量量程百分比,");str.Append("第二变量单位代码,");str.Append("第二变量值,");str.Append("第三变量单位代码,");str.Append("第三变量值,");str.Append("第四变量单位代码,");str.Append("第四变量值");}privatevoidhartSaveConstantDb()//常量存入数据库{if(hartConstant.Rows.Count>0){StringBuilderstrHartC=newStringBuilder();strHartCSet(strHartC);//字符串赋值stringsaveStr=strHartC.ToString();intm=hartConstant.Rows.Count-1;SqlCommandcom=newSqlCommand(String.Format("insertintoHartConstant(\'{0}\')Values({1},\'{2}\',{3},{4},{5},{6},{7},{8},\'{9}\',\'{10}\',\'{11}\',\'{12}\',{13},{14},{15},{16},{17},{18},{19},{20},{21},{22},{23},{24},{25})",saveStr,hartConstant.Rows[m]["ID"],hartConstant.Rows[m]["Time"],hartConstant.Rows[m]["制造商ID"],hartConstant.Rows[m]["制造商设备类型"],hartConstant.Rows[m]["通用命令文档版本号"],hartConstant.Rows[m]["设备软件版本号"],hartConstant.Rows[m]["设备硬件版本号"],hartConstant.Rows[m]["设备ID号"],hartConstant.Rows[m]["设备消息"],hartConstant.Rows[m]["标签Tag"],hartConstant.Rows[m]["描述符"],hartConstant.Rows[m]["日期"],hartConstant.Rows[m]["传感器序列号"],hartConstant.Rows[m]["传感器上下限单位代码"],hartConstant.Rows[m]["传感器上限"],hartConstant.Rows[m]["传感器下限"],hartConstant.Rows[m]["主变量最小精度"],hartConstant.Rows[m]["报警选择代码"],hartConstant.Rows[m]["传递功能代码"],hartConstant.Rows[m]["上下量程值代码"],hartConstant.Rows[m]["主变量上限值"],hartConstant.Rows[m]["主变量下限值"],hartConstant.Rows[m]["主变量阻尼值"],hartConstant.Rows[m]["写保护代码"],hartConstant.Rows[m]["最终装配号"]),dbcon);try{com.ExecuteNonQuery();}catch(Exceptionex){MessageBox.Show(ex.Message);}}}privatevoidhartSaveDynamicTable()//动态变量存入动态变量表{DataRowhartd=hartDynamic.NewRow();hartd["ID"]=IDHartDynamic;hartd["Time"]=DateTime.Now.ToString("yyyy-MM-ddHH:mm:ss.fff");hartd["设备ID号"]=devID;hartd["主变量单位代码"]=PVUnitCd;hartd["主变量值"]=PVValue;hartd["主变量电流"]=PVCurrent;hartd["主变量量程百分比"]=PVPercentRange;hartd["第二变量单位代码"]=SecVUnitCd;hartd["第二变量值"]=SecVValue;hartd["第三变量单位代码"]=ThirdVUnitCd;hartd["第三变量值"]=ThirdVValue;hartd["第四变量单位代码"]=ForthVUnitCd;hartd["第四变量值"]=ForthVValue;hartDynamic.Rows.Add(hartd);}privatevoidhartSaveDynamicDb()//动态变量存入数据看{if(hartDynamic.Rows.Count>0){StringBuilderstrHartD=newStringBuilder();strHartDSet(strHartD);//字符串赋值stringsaveStr=strHartD.ToString();intm=hartDynamic.Rows.Count-1;SqlCommandcom=newSqlCommand(String.Format("insertintoHartDynamic(\'{0}\')Values({1},\'{2}\',{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13})",saveStr,hartDynamic.Rows[m]["ID"],hartDynamic.Rows[m]["Time"],hartDynamic.Rows[m]["设备ID号"],hartDynamic.Rows[m]["主变单位代码"],hartDynamic.Rows[m]["主变量值"],hartDynamic.Rows[m]["主变量电流"],hartDynamic.Rows[m]["主变量量程百分比"],hartDynamic.Rows[m]["第二变量单位代码"],hartDynamic.Rows[m]["第二变量值"],hartDynamic.Rows[m]["第三变量单位代码"],hartDynamic.Rows[m]["第三变量值"],hartDynamic.Rows[m]["第四变量单位代码"],hartDynamic.Rows[m]["第四变量值"]),dbcon);try{com.ExecuteNonQuery();}catch(Exceptionex){MessageBox.Show(ex.Message);}}}#region存储常量(数据库、数据表)privatevoidhartSaveConstant(){if(dbcon_flag==1){if(dbconOpenNow==0){dbconOpen();}stringsqlquery="selecttop1IDfromHartConstantorderbyIDdesc";try{SqlDataAdaptersda=newSqlDataAdapter(sqlquery,dbcon);DataSetds=newDataSet();sda.Fill(ds);//把查询结果填充在ds里if(ds.Tables[0].Rows.Count==1){IDHartConstant=Convert.ToInt32(ds.Tables[0].Rows[0][0])+1;}elseIDHartConstant=0;}catch(Exceptionex){MessageBox.Show(ex.Message);}hartSaveConstantTable();hartSaveConstantDb();if(dbconOpenNow==1){dbconClose();}}else{if(hartConstant.Rows.Count>0){IDHartConstant=Convert.ToInt32(hartConstant.Rows[hartConstant.Rows.Count-1]["ID"])+1;}else{IDHartConstant=0;}hartSaveConstantTable();}}#endregion#region存储动态变量(数据库、数据表)privatevoidhartSaveDynamic(){if(dbcon_flag==1){if(dbconOpenNow==0){dbconOpen();}stringsqlquery="selecttop1IDfromHartDynamicorderbyIDdesc";try{SqlDataAdaptersda=newSqlDataAdapter(sqlquery,dbcon);DataSetds=newDataSet();sda.Fill(ds);//把查询结果填充在ds里if(ds.Tables[0].Rows.Count==1){IDHartDynamic=Convert.ToInt32(ds.Tables[0].Rows[0][0])+1;}elseIDHartDynamic=0;}catch(Exceptionex){MessageBox.Show(ex.Message);}hartSaveDynamicTable();hartSaveDynamicDb();if(dbconOpenNow==1){dbconClose();}}else{if(hartDynamic.Rows.Count>0){IDHartDynamic=Convert.ToInt32(hartDynamic.Rows[hartDynamic.Rows.Count-1]["ID"])+1;}else{IDHartDynamic=0;}hartSaveDynamicTable();}}#endregionprivateinthartNum=0;//HART收发计数器,偶数发查询命令,奇数解析设备返回数据privateinthartCmdNum=0;//控制HART发送解析具体命令privateinthartInitFlag=0;//Hart设备初始化标识privateinthartWorkFlag=0;//Hart设备工作标识privateinthartSleepFlag=0;//Hart设备暂停查询标识(两分钟查询一次变量)privateinthartSleepNum=0;//Hart设备休眠时间privateinthartInitOkFlag=0;//初始化完成标识,用于更新主界面常量相关的参数显示#regionHART设备初始化,主要是查询设备标识及常量信息//HART初始化privatevoidhartInit(){if((hartNum%2)==0){#region发送初始化需要的查询指令switch(hartCmdNum){case0:hartSendCmd0();hartNum++;hartInitOkFlag=0;break;case12:hartSendCmd12();hartNum++;break;case13:hartSendCmd13();hartNum++;break;case14:hartSendCmd14();hartNum++;break;case15:hartSendCmd15();hartNum++;break;case16:hartSendCmd16();hartNum++;break;default:break;}#endregion}else{#region解析设备返回数据switch(hartCmdNum){case0:hartGetCmd0();if(hartGetCmdOk[0]==1){hartNum++;hartCmdNum=12;hartGetCmdOk[0]=0;}break;case12:hartGetCmd12();if(hartGetCmdOk[4]==1){hartNum++;hartCmdNum=13;hartGetCmdOk[4]=0;}break;case13:hartGetCmd13();if(hartGetCmdOk[5]==1){hartNum++;hartCmdNum=14;hartGetCmdOk[5]=0;}break;case14:hartGetCmd14();if(hartGetCmdOk[6]==1){hartNum++;hartCmdNum=15;hartGetCmdOk[6]=0;}break;case15:hartGetCmd13();if(hartGetCmdOk[7]==1){hartNum++;hartCmdNum=16;hartGetCmdOk[7]=0;}break;case16:hartGetCmd16();if(hartGetCmdOk[8]==1){hartSaveConstant();hartNum=0;hartCmdNum=1;hartGetCmdOk[8]=0;hartInitOkFlag=1;//初始化完毕hartConstantShow();//界面显示hartInitFlag=0;hartWorkFlag=1;}break;default:break;}#endregion}}#endregion#regionHART设备工作常态,时时采集动态变量信息privatevoidhartWork(){if((hartNum%2)==0){switch(hartCmdNum){case1:hartSendCmd1();hartNum++;break;case2:hartSendCmd2();hartNum++;break;case3:hartSendCmd3();hartNum++;break;default:break;}}else{switch(hartCmdNum){case1:hartGetCmd1();if(hartGetCmdOk[1]==1){hartNum++;hartCmdNum=2;hartGetCmdOk[1]=0;}break;case2:hartGetCmd2();if(hartGetCmdOk[2]==1){hartNum++;hartCmdNum=3;hartGetCmdOk[2]=0;}break;case3:hartGetCmd3();if(hartGetCmdOk[3]==1){hartNum=0;hartCmdNum=1;hartGetCmdOk[3]=0;hartWorkFlag=0;hartSleepFlag=1;//停止采集两分钟hartDynamicShow();//界面显示数据}break;default:break;}}}#endregion#region动态变量显示privatevoidhartDynamicShow(){if(hartViewItemSct[0]==1){stringstr=label_HartPara_1.Text.Trim();intnum=hartDynamic.Rows.Count-1;if(num>=0){textBox_HartPara_1.Text=hartDynamic.Rows[num][str].ToString();}}if(hartViewItemSct[1]==1){stringstr=label_HartPara_2.Text.Trim();intnum=hartDynamic.Rows.Count-1;if(num>=0){textBox_HartPara_2.Text=hartDynamic.Rows[num][str].ToString();}}if(hartViewItemSct[2]==1){stringstr=label_HartPara_3.Text.Trim();intnum=hartDynamic.Rows.Count-1;if(num>=0){textBox_HartPara_3.Text=hartDynamic.Rows[num][str].ToString();}}if(hartViewItemSct[3]==1){stringstr=label_HartPara_4.Text.Trim();intnum=hartDynamic.Rows.Count-1;if(num>=0){textBox_HartPara_4.Text=hartDynamic.Rows[num][str].ToString();}}if(hartViewItemSct[4]==1){stringstr=label_HartPara_5.Text.Trim();intnum=hartDynamic.Rows.Count-1;if(num>=0){textBox_HartPara_5.Text=hartDynamic.Rows[num][str].ToString();}}if(hartViewItemSct[5]==1){stringstr=label_HartPara_6.Text.Trim();intnum=hartDynamic.Rows.Count-1;if(num>=0){textBox_HartPara_6.Text=hartDynamic.Rows[num][str].ToString();}}}#endregion#region常量显示privatevoidhartConstantShow(){if(hartViewItemSct[0]==2){stringstr=label_HartPara_1.Text.Trim();textBox_HartPara_1.Text=hartConstant.Rows[0][str].ToString();}if(hartViewItemSct[1]==2){stringstr=label_HartPara_2.Text.Trim();textBox_HartPara_2.Text=hartConstant.Rows[0][str].ToString();}if(hartViewItemSct[2]==2){stringstr=label_HartPara_3.Text.Trim();textBox_HartPara_3.Text=hartConstant.Rows[0][str].ToString();}if(hartViewItemSct[3]==2){stringstr=label_HartPara_4.Text.Trim();textBox_HartPara_4.Text=hartConstant.Rows[0][str].ToString();}if(hartViewItemSct[4]==2){stringstr=label_HartPara_5.Text.Trim();textBox_HartPara_5.Text=hartConstant.Rows[0][str].ToString();}if(hartViewItemSct[5]==2){stringstr=label_HartPara_6.Text.Trim();textBox_HartPara_6.Text=hartConstant.Rows[0][str].ToString();}}#endregion}}',)


  • 编号:1700635719
  • 分类:合同模板
  • 软件: wps,office word
  • 大小:28页
  • 格式:docx
  • 风格:商务
  • PPT页数:170024 KB
  • 标签:

广告位推荐

相关合同模板更多>