/// <summary> /// 获取协议通用信息 /// </summary> /// <param name="pmdevice">协议配置tag值</param> /// <returns>返回设备通讯通用信息实例</returns> private devObj setDevObj(string pmdevice) { devObj devobj = new devObj(); devobj.ID = basefun.valtag(pmdevice, "devid"); devobj.ControlNo = basefun.toIntval(basefun.valtag(pmdevice, "controlno")); devobj.Buss = basefun.valtag(pmdevice, "buss"); devobj.IP = basefun.valtag(pmdevice, "ip"); devobj.Mode = basefun.toIntval(basefun.valtag(pmdevice, "mode")) - 1; devobj.Port = basefun.valtag(pmdevice, "port"); if (devobj.Mode == 0 && !devobj.Port.ToUpper().StartsWith("COM")) { devobj.Port = "COM" + devobj.Port; } devobj.Paritymode = basefun.valtag(pmdevice, "parity"); devobj.LH = basefun.valtag(pmdevice, "LH") == "1" ? true : false; devobj.Exchanges = basefun.valtag(pmdevice, "exchange"); devobj.Command = basefun.valtag(pmdevice, "cmd"); devobj.WeekRule = basefun.valtag(pmdevice, "weekrule"); devobj.IsAsc = basefun.valtag(pmdevice, "isasc") == "1" ? true : false; devobj.PreCommand = basefun.valtag(pmdevice, "precmd"); devobj.Return = basefun.valtag(pmdevice, "return"); devobj.parity = Parity.None; switch (basefun.valtag(pmdevice, "paritycom").ToLower()) { case "none": devobj.parity = Parity.None; break; case "even": devobj.parity = Parity.Even; break; case "mark": devobj.parity = Parity.Mark; break; case "odd": devobj.parity = Parity.Odd; break; case "space": devobj.parity = Parity.Space; break; } devobj.baudRate = basefun.toIntval((basefun.valtag(pmdevice, "baudrate"))); int dbit = basefun.toIntval(basefun.valtag(pmdevice, "databits")); devobj.DataBits = (dbit == 0) ? 8 : dbit; devobj.stopBits = StopBits.One; switch (basefun.valtag(pmdevice, "stopbits").ToLower()) { case "none": devobj.stopBits = StopBits.None; break; case "one": devobj.stopBits = StopBits.One; break; case "onepoint": devobj.stopBits = StopBits.OnePointFive; break; case "two": devobj.stopBits = StopBits.Two; break; } //串口参数 return(devobj); }
/// <summary> /// 设置设备指令,使用dvID做指令ID /// </summary> /// <param name="tpl">协议类型</param> /// <param name="cmd">指令</param> /// <param name="tagdata">数据参数,使用tag标记格式</param> public bool setCommand(string tpl, string cmd, string tagdata) { //if (string.IsNullOrEmpty(tpl) || string.IsNullOrEmpty(cmd) || CmdState.Response == this.CheckState()) // return false; PtlParam ptlParam = new PtlParam(); if (PTLParam.ContainsKey(tpl + ":" + cmd)) { ptlParam = PTLParam[tpl + ":" + cmd]; } else { Estar.Common.Tools.NameObjectList ps = new Estar.Common.Tools.NameObjectList(); ps["tpl"] = tpl; ps["cmd"] = cmd; if (null == this.query) { return(false); } DataTable tab = this.query.getTable(dbItem, ps); if (null == tab || tab.Rows.Count < 1) { return(false); } //从数据库中取出的参数转换成tag格式参数 DataColumnCollection dbcols = tab.Columns; if (!dbcols.Contains("type") || !dbcols.Contains("pms")) { return(false); } foreach (DataRow dr in tab.Rows) { if (DBNull.Value == dr["type"]) { continue; } string pmtype = Convert.ToString(dr["type"]); switch (pmtype) { case "协议": ptlParam.pmdevice = Convert.ToString(dr["pms"]); break; case "Input": ptlParam.pminput += Convert.ToString(dr["pms"]) + ";"; break; case "Output": ptlParam.pmout += Convert.ToString(dr["pms"]) + ";"; break; } } if (ptlParam.pmdevice.EndsWith(";")) { ptlParam.pmdevice = ptlParam.pmdevice.Substring(0, ptlParam.pmdevice.Length - 1); } if (string.IsNullOrEmpty(ptlParam.pmdevice)) { return(false); } if (ptlParam.pminput.EndsWith(";")) { ptlParam.pminput = ptlParam.pminput.Substring(0, ptlParam.pminput.Length - 1); } if (ptlParam.pmout.EndsWith(";")) { ptlParam.pmout = ptlParam.pmout.Substring(0, ptlParam.pmout.Length - 1); } ptlParam.pmdevice = ptlParam.pmdevice.Replace(";;", ";"); ptlParam.pminput = ptlParam.pminput.Replace(";;", ";"); ptlParam.pmout = ptlParam.pmout.Replace(";;", ";"); if (!PTLParam.ContainsKey(tpl + ":" + cmd)) { try { PTLParam.Add(tpl + ":" + cmd, ptlParam); } catch { } } } this.inputParam = ptlParam.pminput; this.outterParam = ptlParam.pmout; this.responseFormat = ""; //根据协议组成通讯指令 string pmdevice = basefun.setvaltag(ptlParam.pmdevice, "devid", basefun.valtag(tagdata, "设备地址")); dvParam.Command = ""; string cmdstr = ""; if (string.IsNullOrEmpty(tagdata)) { dvParam = this.setDevObj(pmdevice); } else { cmdstr = comparse.CommandString(pmdevice, ptlParam.pminput, tagdata, ref dvParam); } IsAscii = dvParam.IsAsc; Exchanges = dvParam.Exchanges; if (string.IsNullOrEmpty(dvParam.Buss)) { dvParam.Buss = tpl; } if (string.IsNullOrEmpty(dvParam.Command)) { dvParam.Command = cmd; } //没有数据参数,则只获取格式参数,便于解析结果 if (string.IsNullOrEmpty(tagdata)) { return(true); } //转义成字节,使用""""代替原来帧头帧尾的处理 if (dvParam.IsAsc) { if (cmdstr.StartsWith("02") && cmdstr.EndsWith("03")) { cmdstr = "" + cmdstr.Substring(2, cmdstr.Length - 4) + ""; } return(this.setCommand(cmdstr)); } return(this.setCommand(cmdstr, true)); }