Ejemplo n.º 1
0
        /// <summary>
        /// 设置通讯ZNetCom模块端口好,及串口波特率
        /// </summary>
        /// <param name="macaddr">物理地址,为空则忽略操作</param>
        /// <param name="netport">网络端口</param>
        /// <param name="baudRate">波特率</param>
        /// <param name="parity">奇偶校验</param>
        /// <param name="dataBits">数据位</param>
        /// <param name="stopBits">停止位</param>
        /// <returns>修改是否成功</returns>
        public static bool SetSerialZnet(string macaddr, int netport, int baudRate, Parity parity, int dataBits, StopBits stopBits)
        {
            if (string.IsNullOrEmpty(macaddr) || netport < 0 || netport > 0xFFFF || baudRate < 0 || baudRate > 0xFFFFFF || dataBits < 5 || dataBits > 8)
                return false;
            //读取串口通讯
            string strcmd = string.Format(infocmd, macaddr.Replace("-", " "), "A114010202");

            IPEndPoint srv = new IPEndPoint(IPAddress.Broadcast, infoport);
            CommiTarget target = new CommiTarget(srv, CommiType.UDP);
            target.setProtocol(ptlznet);
            CommandBase cmd = new CommandBase(false);
            cmd.TimeOut = new TimeSpan(0, 0, 0, 0, 300);
            cmd.TimeFailLimit = new TimeSpan(0, 0, 1);
            cmd.TimeLimit = new TimeSpan(0, 0, 3);
            cmd.IsResposeHandle = isResponseCmd;
            cmd.setCommand(strcmd, true);
            CommiManager.GlobalManager.SendCommand(target, cmd);
            if (!cmd.EventWh.WaitOne(cmd.TimeFailLimit, false))
                return false;
            //修改设备信息
            string response = CommandBase.Parse(cmd.ResponseData, true);
            string funcode = "A1FE01030200";
            string strport = Convert.ToString(netport, 16).PadLeft(4, '0');
            string strbaud = Convert.ToString(baudRate, 16).PadLeft(6, '0');
            string strdata = Convert.ToString(dataBits, 16).PadLeft(2, '0');
            string strstop = StopBits.None == stopBits ? "00" : StopBits.One == stopBits ? "01" : "02";
            string strpartity = Parity.None == parity ? "00" : Parity.Even == parity ? "01" : Parity.Odd == parity ? "02" : Parity.Mark == parity ? "03" : "04";
            string[] cmdfun ={ funcode, strport, strbaud, strdata, strstop, strpartity };
            int[,] cmdpos ={ { 10, 5 }, { 16, 2 }, { 26, 3 }, { 29, 1 }, { 30, 1 }, { 31, 1 } };
            int leninv = 0;
            for (int i = 0; i < cmdfun.Length; i++)
            {
                if (string.IsNullOrEmpty(cmdfun[i]) || string.IsNullOrEmpty(cmdfun[i].Replace("0", "")))
                    continue;
                response = response.Substring(0, cmdpos[i, 0] * 2 + leninv) + cmdfun[i]
                           + response.Substring(cmdpos[i, 0] * 2 + leninv + cmdpos[i, 1] * 2, response.Length);
                leninv += cmdfun[i].Length - cmdpos[i, 1] * 2;
            }
            //发送修改指令,再发送确认指令
            cmd.setCommand(response, true);
            cmd.ResetState();
            CommiManager.GlobalManager.SendCommand(target, cmd);
            cmd.EventWh.WaitOne(cmd.TimeFailLimit, false);
            strcmd = string.Format(infomdi, macaddr.Replace("-", ""));
            cmd.setCommand(strcmd, true);
            cmd.ResetState();
            CommiManager.GlobalManager.SendCommand(target, cmd);
            cmd.EventWh.WaitOne(cmd.TimeFailLimit, false);
            return true;
        }
Ejemplo n.º 2
0
 /// <summary>
 /// 设置通讯ZNetCom模块IP地址端口号及网关
 /// </summary>
 /// <param name="macaddr">物理地址,为空则忽略操作</param>
 /// <param name="ipaddr">IP地址,为空则忽略操作</param>
 /// <param name="maskcode">子网掩码,为空保持原值</param>
 /// <param name="gateway">网关,为空保持原值</param>
 /// <returns>修改是否成功</returns>
 public static bool SetTCPIPZnet(string macaddr, string ipaddr, string maskcode, string gateway)
 {
     if (string.IsNullOrEmpty(macaddr) || string.IsNullOrEmpty(ipaddr) || !regMacaddr.IsMatch(macaddr) || !regIPaddr.IsMatch(ipaddr))
         return false;
     //读取IP地址
     string strcmd = string.Format(infocmd, macaddr.Replace("-", " "), "A114000200");
     
     IPEndPoint srv = new IPEndPoint(IPAddress.Broadcast, infoport);
     CommiTarget target = new CommiTarget(srv, CommiType.UDP);
     target.setProtocol(ptlznet);
     CommandBase cmd = new CommandBase(false);
     cmd.TimeOut = new TimeSpan(0, 0, 0, 0, 300);
     cmd.TimeFailLimit = new TimeSpan(0, 0, 1);
     cmd.TimeLimit = new TimeSpan(0, 0, 3);
     cmd.IsResposeHandle = isResponseCmd;
     cmd.setCommand(strcmd, true);
     CommiManager.GlobalManager.SendCommand(target, cmd);
     if (!cmd.EventWh.WaitOne(cmd.TimeFailLimit, false))
         return false;
     //修改设备信息
     string response = CommandBase.Parse(cmd.ResponseData, true);
     if (ipaddr.Contains(":"))
         ipaddr = ipaddr.Substring(0, ipaddr.IndexOf(":"));
     ipaddr = convertHEX(ipaddr, ".", false);
     maskcode = convertHEX(maskcode, ".", false);
     gateway = convertHEX(gateway, ".", false);
     string funcode = "A1FE01030000";
     string[] cmdfun ={ funcode, ipaddr, maskcode, gateway };
     int[,] cmdpos ={ { 10, 5 }, { 47, 4 }, { 51, 4 }, { 55, 4 } };
     int leninv = 0;
     for (int i = 0; i < cmdfun.Length; i++)
     {
         if (string.IsNullOrEmpty(cmdfun[i]))
             continue;
         response = response.Substring(0, cmdpos[i, 0] * 2 + leninv) + cmdfun[i] 
                    + response.Substring(cmdpos[i, 0] * 2 + leninv + cmdpos[i, 1] * 2, response.Length);
         leninv += cmdfun[i].Length - cmdpos[i, 1] * 2;
     }
     //发送修改指令,再发送确认指令
     cmd.setCommand(response, true);
     cmd.ResetState();
     CommiManager.GlobalManager.SendCommand(target, cmd);
     cmd.EventWh.WaitOne(cmd.TimeFailLimit, false);
     strcmd = string.Format(infomdi, macaddr.Replace("-", ""));
     cmd.setCommand(strcmd, true);
     cmd.ResetState();
     CommiManager.GlobalManager.SendCommand(target, cmd);
     cmd.EventWh.WaitOne(cmd.TimeFailLimit, false);
     return true;
 }