Beispiel #1
0
        /// <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 (CmdState.Response == this.CheckState())
            {
                return(false);
            }
            Estar.Common.Tools.NameObjectList ps = new Estar.Common.Tools.NameObjectList();
            ps["tpl"] = tpl;
            ps["cmd"] = cmd;
            DataTable tab = query.getTable(dbItem, ps);

            if (null == tab || tab.Rows.Count < 1)
            {
                return(false);
            }
            //设备参数
            string pmdevice = "";
            //输入格式参数
            string pminput = "";
            //输出格式参数
            string pmout = "";

            //从数据库中取出的参数转换成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 "协议":
                    pmdevice = Convert.ToString(dr["pms"]);
                    break;

                case "Input":
                    pminput += Convert.ToString(dr["pms"]) + ";";
                    break;

                case "Output":
                    pmout += Convert.ToString(dr["pms"]) + ";";
                    break;
                }
            }

            if (pmdevice.EndsWith(";"))
            {
                pmdevice = pmdevice.Substring(0, pmdevice.Length - 1);
            }
            if (pminput.EndsWith(";"))
            {
                pminput = pminput.Substring(0, pminput.Length - 1);
            }
            if (pmout.EndsWith(";"))
            {
                pmout = pmout.Substring(0, pmout.Length - 1);
            }
            pmdevice         = pmdevice.Replace(";;", ";");
            this.inputParam  = pminput = pminput.Replace(";;", ";");
            this.outterParam = pmout = pmout.Replace(";;", ";");

            // begin 临时补充,在将来完善协议解析后去掉
            pmdevice = basefun.setvaltag(pmdevice, "devid", basefun.valtag(tagdata, "设备地址"));
            // end
            string cmdstr = comparse.CommandString(pmdevice, pminput, tagdata, ref dvParam);

            IsAscii   = dvParam.IsAsc;
            Exchanges = dvParam.Exchanges;
            //转义成字节,使用""""代替原来帧头帧尾的处理
            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));
        }
Beispiel #2
0
        /// <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));
        }