private JavaScriptObject send(HttpRequest request)
        {
            string DeviceNo         = request["DeviceNo"] ?? "";
            string k                = request["k"] ?? "";
            string content          = request["content"] ?? "";
            string DeviceTime       = request["DeviceTime"] ?? "";
            string YearExploitation = request["YearExploitation"] ?? "";
            string SerialNumber     = request["SerialNumber"] ?? "";
            string Range            = request["Range"] ?? "";
            string LineLength       = request["LineLength"] ?? "";

            JavaScriptObject result = new JavaScriptObject();

            result.Add("Result", false);
            result.Add("Message", "");

            if (DeviceNo.Length != 15)
            {
                result["Message"] = "设备编号格式不对!";
                return(result);
            }

            Device device = DeviceModule.GetDeviceByFullDeviceNo(DeviceNo);

            if (device == null)
            {
                result["Message"] = "设备编号不存在!" + DeviceNo;
                return(result);
            }

            try
            {
                bool waitRsp = false;

                TcpCommunication tcpService = new TcpCommunication();

                int timeDelay = 0;
                //待socket准备好
                while (timeDelay < tcpService.TcpWait)
                {
                    if ((tcpService.SOCKET_STATE == TcpCommunication.TCP_SocketState.SOCKET_CONNECTED) ||
                        (tcpService.SOCKET_STATE == TcpCommunication.TCP_SocketState.SOCKET_CLOSED))
                    {
                        break;
                    }

                    Thread.Sleep(100);
                    timeDelay = timeDelay + 1;
                }

                if (tcpService.SOCKET_STATE != TcpCommunication.TCP_SocketState.SOCKET_CONNECTED)
                {
                    result["Message"] = "与网关通讯失败!";
                    return(result);
                }

                byte[] cmd_send = null;

                if (k == "1")
                {
                    CmdToDtuSetDateTime cmd = new CmdToDtuSetDateTime();
                    cmd.AddressField = DeviceNo.Substring(0, 12) + Convert.ToInt32(DeviceNo.Substring(12, 3)).ToString("X").PadLeft(2, '0');
                    cmd.StationType  = (byte)device.StationType;
                    cmd.StationCode  = device.StationType == 2 ? device.StationCode : 0;
                    cmd.DateTimeNew  = DateTime.Parse(DeviceTime);
                    cmd.RawDataChar  = cmd.WriteMsg();
                    cmd.RawDataStr   = HexStringUtility.ByteArrayToHexString(cmd.RawDataChar);

                    myLogger.Info(cmd.RawDataStr);

                    cmd_send = cmd.RawDataChar;
                }
                else if (k == "2")
                {
                    CmdToDtuQueryDateTime cmd = new CmdToDtuQueryDateTime();
                    cmd.AddressField = DeviceNo.Substring(0, 12) + Convert.ToInt32(DeviceNo.Substring(12, 3)).ToString("X").PadLeft(2, '0');
                    cmd.StationType  = (byte)device.StationType;
                    cmd.StationCode  = device.StationType == 2 ? device.StationCode : 0;
                    cmd.RawDataChar  = cmd.WriteMsg();
                    cmd.RawDataStr   = HexStringUtility.ByteArrayToHexString(cmd.RawDataChar);

                    myLogger.Info(cmd.RawDataStr);

                    cmd_send = cmd.RawDataChar;
                }
                else if (k == "3")
                {
                    CmdToDtuSetYearExploitation cmd = new CmdToDtuSetYearExploitation();
                    cmd.AddressField     = DeviceNo.Substring(0, 12) + Convert.ToInt32(DeviceNo.Substring(12, 3)).ToString("X").PadLeft(2, '0');
                    cmd.StationType      = (byte)device.StationType;
                    cmd.StationCode      = device.StationType == 2 ? device.StationCode : 0;
                    cmd.YearExploitation = decimal.Parse(YearExploitation);
                    cmd.RawDataChar      = cmd.WriteMsg();
                    cmd.RawDataStr       = HexStringUtility.ByteArrayToHexString(cmd.RawDataChar);

                    myLogger.Info(cmd.RawDataStr);

                    cmd_send = cmd.RawDataChar;
                }
                else if (k == "4")
                {
                    CmdToDtuQueryYearExploitation cmd = new CmdToDtuQueryYearExploitation();
                    cmd.AddressField = DeviceNo.Substring(0, 12) + Convert.ToInt32(DeviceNo.Substring(12, 3)).ToString("X").PadLeft(2, '0');
                    cmd.StationType  = (byte)device.StationType;
                    cmd.StationCode  = device.StationType == 2 ? device.StationCode : 0;
                    cmd.RawDataChar  = cmd.WriteMsg();
                    cmd.RawDataStr   = HexStringUtility.ByteArrayToHexString(cmd.RawDataChar);

                    myLogger.Info(cmd.RawDataStr);

                    cmd_send = cmd.RawDataChar;
                }
                else if (k == "5")
                {
                    CmdToDtuOpenPump cmd = new CmdToDtuOpenPump();
                    cmd.AddressField = DeviceNo.Substring(0, 12) + Convert.ToInt32(DeviceNo.Substring(12, 3)).ToString("X").PadLeft(2, '0');
                    cmd.StationType  = (byte)device.StationType;
                    cmd.StationCode  = device.StationType == 2 ? device.StationCode : 0;
                    cmd.RawDataChar  = cmd.WriteMsg();
                    cmd.RawDataStr   = HexStringUtility.ByteArrayToHexString(cmd.RawDataChar);

                    myLogger.Info(cmd.RawDataStr);

                    cmd_send = cmd.RawDataChar;
                }
                else if (k == "6")
                {
                    CmdToDtuClosePump cmd = new CmdToDtuClosePump();
                    cmd.AddressField = DeviceNo.Substring(0, 12) + Convert.ToInt32(DeviceNo.Substring(12, 3)).ToString("X").PadLeft(2, '0');
                    cmd.StationType  = (byte)device.StationType;
                    cmd.StationCode  = device.StationType == 2 ? device.StationCode : 0;
                    cmd.RawDataChar  = cmd.WriteMsg();
                    cmd.RawDataStr   = HexStringUtility.ByteArrayToHexString(cmd.RawDataChar);

                    myLogger.Info(cmd.RawDataStr);

                    cmd_send = cmd.RawDataChar;
                }
                else if (k == "7")
                {
                    CmdToDtuSetStationCode cmd = new CmdToDtuSetStationCode();

                    if (device.StationType == 0)
                    {
                        result["Message"] = "非主从站无法设置分站射频地址!";
                        return(result);
                    }
                    else if (device.StationType == 1)
                    {
                        cmd.AddressField = DeviceNo.Substring(0, 12) + Convert.ToInt32(DeviceNo.Substring(12, 3)).ToString("X").PadLeft(2, '0');
                        cmd.StationType  = (byte)device.StationType;
                        cmd.StationCode  = device.StationType == 2 ? device.StationCode : 0;
                        List <Device> DeviceSubList = DeviceModule.GetAllDeviceSubList(device.Id);
                        List <int>    list          = new List <int>();
                        foreach (Device DeviceSub in DeviceSubList)
                        {
                            list.Add(DeviceSub.StationCode);
                        }
                        cmd.StationCodeList = list;
                    }
                    else if (device.StationType == 2)
                    {
                        Device DeviceMain   = DeviceModule.GetDeviceByID(device.MainId);
                        string DeviceMainNo = DeviceModule.GetFullDeviceNoByID(device.MainId);
                        cmd.AddressField = DeviceMainNo.Substring(0, 12) + Convert.ToInt32(DeviceMainNo.Substring(12, 3)).ToString("X").PadLeft(2, '0');
                        cmd.StationType  = (byte)DeviceMain.StationType;
                        cmd.StationCode  = DeviceMain.StationType == 2 ? DeviceMain.StationCode : 0;
                        List <Device> DeviceSubList = DeviceModule.GetAllDeviceSubList(device.MainId);
                        List <int>    list          = new List <int>();
                        foreach (Device DeviceSub in DeviceSubList)
                        {
                            list.Add(DeviceSub.StationCode);
                        }
                        cmd.StationCodeList = list;
                    }

                    cmd.RawDataChar = cmd.WriteMsg();
                    cmd.RawDataStr  = HexStringUtility.ByteArrayToHexString(cmd.RawDataChar);

                    myLogger.Info(cmd.RawDataStr);

                    cmd_send = cmd.RawDataChar;
                }
                else if (k == "8")
                {
                    if (SerialNumber.Length != 8)
                    {
                        result["Message"] = "卡号长度只能为8位!";
                        return(result);
                    }
                    if (!Regex.IsMatch(SerialNumber, "^[0-9A-Fa-f]+$"))
                    {
                        result["Message"] = "卡号只能为0-9A-Fa-f!";
                        return(result);
                    }

                    CmdToDtuShieldSerialNumber cmd = new CmdToDtuShieldSerialNumber();
                    cmd.AddressField = DeviceNo.Substring(0, 12) + Convert.ToInt32(DeviceNo.Substring(12, 3)).ToString("X").PadLeft(2, '0');
                    cmd.StationType  = (byte)device.StationType;
                    cmd.StationCode  = device.StationType == 2 ? device.StationCode : 0;
                    cmd.SerialNumber = SerialNumber;
                    cmd.RawDataChar  = cmd.WriteMsg();
                    cmd.RawDataStr   = HexStringUtility.ByteArrayToHexString(cmd.RawDataChar);

                    myLogger.Info(cmd.RawDataStr);

                    cmd_send = cmd.RawDataChar;
                }
                else if (k == "9")
                {
                    if (SerialNumber.Length != 8)
                    {
                        result["Message"] = "卡号长度只能为8位!";
                        return(result);
                    }
                    if (!Regex.IsMatch(SerialNumber, "^[0-9A-Fa-f]+$"))
                    {
                        result["Message"] = "卡号只能为0-9A-Fa-f!";
                        return(result);
                    }

                    CmdToDtuShieldSerialNumberCancel cmd = new CmdToDtuShieldSerialNumberCancel();
                    cmd.AddressField = DeviceNo.Substring(0, 12) + Convert.ToInt32(DeviceNo.Substring(12, 3)).ToString("X").PadLeft(2, '0');
                    cmd.StationType  = (byte)device.StationType;
                    cmd.StationCode  = device.StationType == 2 ? device.StationCode : 0;
                    cmd.SerialNumber = SerialNumber;
                    cmd.RawDataChar  = cmd.WriteMsg();
                    cmd.RawDataStr   = HexStringUtility.ByteArrayToHexString(cmd.RawDataChar);

                    myLogger.Info(cmd.RawDataStr);

                    cmd_send = cmd.RawDataChar;
                }
                else if (k == "10")
                {
                    CmdToDtuSetGroundWaterParam cmd = new CmdToDtuSetGroundWaterParam();
                    cmd.AddressField = DeviceNo.Substring(0, 12) + Convert.ToInt32(DeviceNo.Substring(12, 3)).ToString("X").PadLeft(2, '0');
                    cmd.StationType  = (byte)device.StationType;
                    cmd.StationCode  = device.StationType == 2 ? device.StationCode : 0;
                    cmd.Range        = byte.Parse(Range);
                    cmd.LineLength   = double.Parse(LineLength);
                    cmd.RawDataChar  = cmd.WriteMsg();
                    cmd.RawDataStr   = HexStringUtility.ByteArrayToHexString(cmd.RawDataChar);

                    myLogger.Info(cmd.RawDataStr);

                    cmd_send = cmd.RawDataChar;
                }
                else if (k == "11")
                {
                    CmdToDtuQueryGroundWaterParam cmd = new CmdToDtuQueryGroundWaterParam();
                    cmd.AddressField = DeviceNo.Substring(0, 12) + Convert.ToInt32(DeviceNo.Substring(12, 3)).ToString("X").PadLeft(2, '0');
                    cmd.StationType  = (byte)device.StationType;
                    cmd.StationCode  = device.StationType == 2 ? device.StationCode : 0;
                    cmd.RawDataChar  = cmd.WriteMsg();
                    cmd.RawDataStr   = HexStringUtility.ByteArrayToHexString(cmd.RawDataChar);

                    myLogger.Info(cmd.RawDataStr);

                    cmd_send = cmd.RawDataChar;
                }
                else if (k == "12")
                {
                    CmdToDtuQueryGroundWater cmd = new CmdToDtuQueryGroundWater();
                    cmd.AddressField = DeviceNo.Substring(0, 12) + Convert.ToInt32(DeviceNo.Substring(12, 3)).ToString("X").PadLeft(2, '0');
                    cmd.StationType  = (byte)device.StationType;
                    cmd.StationCode  = device.StationType == 2 ? device.StationCode : 0;
                    cmd.RawDataChar  = cmd.WriteMsg();
                    cmd.RawDataStr   = HexStringUtility.ByteArrayToHexString(cmd.RawDataChar);

                    myLogger.Info(cmd.RawDataStr);

                    cmd_send = cmd.RawDataChar;
                }
                else if (k == "13")
                {
                    CmdToDtuQueryState cmd = new CmdToDtuQueryState();
                    cmd.AddressField = DeviceNo.Substring(0, 12) + Convert.ToInt32(DeviceNo.Substring(12, 3)).ToString("X").PadLeft(2, '0');
                    cmd.StationType  = (byte)device.StationType;
                    cmd.StationCode  = device.StationType == 2 ? device.StationCode : 0;
                    cmd.RawDataChar  = cmd.WriteMsg();
                    cmd.RawDataStr   = HexStringUtility.ByteArrayToHexString(cmd.RawDataChar);

                    myLogger.Info(cmd.RawDataStr);

                    cmd_send = cmd.RawDataChar;
                }
                else
                {
                    result["Message"] = "参数错误!";
                    return(result);
                }
                tcpService.SendData(cmd_send, 0, cmd_send.Length);

                timeDelay = 0;
                while (timeDelay < tcpService.TcpWait)
                {
                    if (tcpService.socketData.Buffer.Length > 0)
                    {
                        myLogger.Info(HexStringUtility.ByteArrayToHexString(tcpService.socketData.Buffer.Buffer));
                    }
                    if (tcpService.socketData.Buffer.Length >= CommandCommon.CMD_MIN_LENGTH)
                    {
                        byte[] re         = tcpService.socketData.Buffer.Buffer;
                        byte[] buffer_new = new byte[tcpService.socketData.Buffer.Length];
                        Array.Copy(re, buffer_new, tcpService.socketData.Buffer.Length);
                        string receiveHex = HexStringUtility.ByteArrayToHexString(buffer_new);
                        //string str = HexStringUtility.HexStringToStr(receiveHex);
                        waitRsp          = true;
                        result["Result"] = true;
                        if (receiveHex == BaseProtocol.DeviceOffline)
                        {
                            result["Message"] = "查询终端不在线";
                        }
                        else
                        {
                            result["Message"] = "终端在线返回结果:" + receiveHex;
                        }
                    }

                    if (waitRsp == true)
                    {
                        myLogger.Info("获取响应结束");
                        break;
                    }

                    if (tcpService.SOCKET_STATE == TcpCommunication.TCP_SocketState.SOCKET_CLOSED)
                    {
                        myLogger.Info("Socket关闭结束");
                        break;
                    }

                    Thread.Sleep(100);
                    timeDelay = timeDelay + 1;
                }

                tcpService.Close();

                if (waitRsp == false)
                {
                    result["Message"] = "等待设备回复超时!";
                }
            }
            catch (Exception exception)
            {
                result["Message"] = exception.Message;
                myLogger.Error(exception.Message);
            }

            return(result);
        }
Beispiel #2
0
        private void send(object o)
        {
            SafeSetLabel(this.label7, "", false);

            setAllButtonEnable(false);

            string OperationType = "";
            int    k             = 0;

            try
            {
                k = Convert.ToInt32(o);
                switch (k)
                {
                case 1: OperationType = "设置时间"; break;

                case 2: OperationType = "设置开采量"; break;

                case 3: OperationType = "查询时间"; break;

                case 4: OperationType = "查询开采量"; break;

                case 5: OperationType = "远程开泵"; break;

                case 6: OperationType = "远程关泵"; break;

                case 7: OperationType = "设置主站射频地址"; break;

                case 8: OperationType = "屏蔽卡号"; break;

                case 9: OperationType = "解除屏蔽卡号"; break;

                case 10: OperationType = "水位计参数设置"; break;

                case 11: OperationType = "水位计参数查询"; break;

                case 12: OperationType = "水位查询"; break;

                case 13: OperationType = "状态查询"; break;

                default: OperationType = ""; break;
                }
                if (OperationType.Trim() == "")
                {
                    SafeSetText(textBox2, "发送命令非法!命令字:" + k, false);
                    setAllButtonEnable(true);
                    return;
                }
            }
            catch (Exception ex)
            {
                SafeSetText(textBox2, "发送命令非法!" + ex.Message, false);
                setAllButtonEnable(true);
                return;
            }

            try
            {
                if (this.comboBox1.SelectedIndex < 1)
                {
                    SafeSetText(textBox2, "请选择一个终端", false);
                    setAllButtonEnable(true);
                    return;
                }
                string deviceNo    = this.comboBox1.Text.ToString().Trim();
                Device device      = DeviceModule.GetDeviceByFullDeviceNo(deviceNo);
                string resDeviceNo = DeviceModule.GetDeviceNoMain(deviceNo);
                Device resDevice   = DeviceModule.GetDeviceByFullDeviceNo(resDeviceNo);
                if (resDeviceNo.Length != 15)
                {
                    SafeSetText(textBox2, "终端编号错误!" + resDeviceNo, false);
                    setAllButtonEnable(true);
                    return;
                }

                BaseMessage cmd = new BaseMessage();
                switch (k)
                {
                case 1:
                    if (dateTimePicker1.Value < DateTime.Now.AddDays(-1) || dateTimePicker1.Value > DateTime.Now.AddDays(1))
                    {
                        SafeSetText(textBox2, "终端时钟取值范围是当前时间前后一天", false);
                        setAllButtonEnable(true);
                        return;
                    }

                    CmdToDtuSetDateTime cmd1 = new CmdToDtuSetDateTime();
                    cmd1.AddressField = DeviceModule.DeviceNo_Normal2Hex(deviceNo);
                    cmd1.StationType  = (byte)device.StationType;
                    cmd1.StationCode  = device.StationType == 2 ? device.StationCode : 0;
                    cmd1.DateTimeNew  = dateTimePicker1.Value;
                    cmd1.RawDataChar  = cmd1.WriteMsg();
                    cmd1.RawDataStr   = HexStringUtility.ByteArrayToHexString(cmd1.RawDataChar);
                    ToDtuCommand.AddBaseMessageToDtu(cmd1.AddressField + "-" + cmd1.AFN, cmd1);

                    cmd.AddressField = cmd1.AddressField;
                    cmd.RawDataStr   = cmd1.RawDataStr;
                    cmd.RawDataChar  = cmd1.RawDataChar;
                    cmd.AFN          = cmd1.AFN;
                    break;

                case 2:
                    decimal YearExploitation = 0;
                    try
                    {
                        YearExploitation = decimal.Parse(this.textBox4.Text.Trim());
                    }
                    catch { }
                    if (YearExploitation < 0 || YearExploitation > 9999999.9M)
                    {
                        SafeSetText(textBox2, "开采量取值范围是0-9999999.9", false);
                        setAllButtonEnable(true);
                        return;
                    }

                    CmdToDtuSetYearExploitation cmd2 = new CmdToDtuSetYearExploitation();
                    cmd2.AddressField     = DeviceModule.DeviceNo_Normal2Hex(deviceNo);
                    cmd2.StationType      = (byte)device.StationType;
                    cmd2.StationCode      = device.StationType == 2 ? device.StationCode : 0;
                    cmd2.YearExploitation = YearExploitation;
                    cmd2.RawDataChar      = cmd2.WriteMsg();
                    cmd2.RawDataStr       = HexStringUtility.ByteArrayToHexString(cmd2.RawDataChar);
                    ToDtuCommand.AddBaseMessageToDtu(cmd2.AddressField + "-" + cmd2.AFN, cmd2);

                    cmd.AddressField = cmd2.AddressField;
                    cmd.RawDataStr   = cmd2.RawDataStr;
                    cmd.RawDataChar  = cmd2.RawDataChar;
                    cmd.AFN          = cmd2.AFN;
                    break;

                case 3:
                    CmdToDtuQueryDateTime cmd3 = new CmdToDtuQueryDateTime();
                    cmd3.AddressField = DeviceModule.DeviceNo_Normal2Hex(deviceNo);
                    cmd3.StationType  = (byte)device.StationType;
                    cmd3.StationCode  = device.StationType == 2 ? device.StationCode : 0;
                    cmd3.RawDataChar  = cmd3.WriteMsg();
                    cmd3.RawDataStr   = HexStringUtility.ByteArrayToHexString(cmd3.RawDataChar);
                    ToDtuCommand.AddBaseMessageToDtu(cmd3.AddressField + "-" + cmd3.AFN, cmd3);

                    cmd.AddressField = cmd3.AddressField;
                    cmd.RawDataStr   = cmd3.RawDataStr;
                    cmd.RawDataChar  = cmd3.RawDataChar;
                    cmd.AFN          = cmd3.AFN;
                    break;

                case 4:
                    CmdToDtuQueryYearExploitation cmd4 = new CmdToDtuQueryYearExploitation();
                    cmd4.AddressField = DeviceModule.DeviceNo_Normal2Hex(deviceNo);
                    cmd4.StationType  = (byte)device.StationType;
                    cmd4.StationCode  = device.StationType == 2 ? device.StationCode : 0;
                    cmd4.RawDataChar  = cmd4.WriteMsg();
                    cmd4.RawDataStr   = HexStringUtility.ByteArrayToHexString(cmd4.RawDataChar);
                    ToDtuCommand.AddBaseMessageToDtu(cmd4.AddressField + "-" + cmd4.AFN, cmd4);

                    cmd.AddressField = cmd4.AddressField;
                    cmd.RawDataStr   = cmd4.RawDataStr;
                    cmd.RawDataChar  = cmd4.RawDataChar;
                    cmd.AFN          = cmd4.AFN;
                    break;

                case 5:
                    CmdToDtuOpenPump cmd5 = new CmdToDtuOpenPump();
                    cmd5.AddressField = DeviceModule.DeviceNo_Normal2Hex(deviceNo);
                    cmd5.StationType  = (byte)device.StationType;
                    cmd5.StationCode  = device.StationType == 2 ? device.StationCode : 0;
                    cmd5.RawDataChar  = cmd5.WriteMsg();
                    cmd5.RawDataStr   = HexStringUtility.ByteArrayToHexString(cmd5.RawDataChar);
                    ToDtuCommand.AddBaseMessageToDtu(cmd5.AddressField + "-" + cmd5.AFN, cmd5);

                    cmd.AddressField = cmd5.AddressField;
                    cmd.RawDataStr   = cmd5.RawDataStr;
                    cmd.RawDataChar  = cmd5.RawDataChar;
                    cmd.AFN          = cmd5.AFN;
                    break;

                case 6:
                    CmdToDtuClosePump cmd6 = new CmdToDtuClosePump();
                    cmd6.AddressField = DeviceModule.DeviceNo_Normal2Hex(deviceNo);
                    cmd6.StationType  = (byte)device.StationType;
                    cmd6.StationCode  = device.StationType == 2 ? device.StationCode : 0;
                    cmd6.RawDataChar  = cmd6.WriteMsg();
                    cmd6.RawDataStr   = HexStringUtility.ByteArrayToHexString(cmd6.RawDataChar);
                    ToDtuCommand.AddBaseMessageToDtu(cmd6.AddressField + "-" + cmd6.AFN, cmd6);

                    cmd.AddressField = cmd6.AddressField;
                    cmd.RawDataStr   = cmd6.RawDataStr;
                    cmd.RawDataChar  = cmd6.RawDataChar;
                    cmd.AFN          = cmd6.AFN;
                    break;

                case 7:
                    CmdToDtuSetStationCode cmd7 = new CmdToDtuSetStationCode();
                    if (device.StationType == 0)
                    {
                        SafeSetText(textBox2, "非主从站无法设置分站射频地址", false);
                        setAllButtonEnable(true);
                        return;
                    }
                    else if (device.StationType == 1)
                    {
                        cmd7.AddressField = deviceNo.Substring(0, 12) + Convert.ToInt32(deviceNo.Substring(12, 3)).ToString("X").PadLeft(2, '0');
                        cmd7.StationType  = (byte)device.StationType;
                        cmd7.StationCode  = device.StationType == 2 ? device.StationCode : 0;
                        List <Device> DeviceSubList = DeviceModule.GetAllDeviceSubList(device.Id);
                        List <int>    list          = new List <int>();
                        foreach (Device DeviceSub in DeviceSubList)
                        {
                            list.Add(DeviceSub.StationCode);
                        }
                        cmd7.StationCodeList = list;
                    }
                    else if (device.StationType == 2)
                    {
                        cmd7.AddressField = resDeviceNo.Substring(0, 12) + Convert.ToInt32(resDeviceNo.Substring(12, 3)).ToString("X").PadLeft(2, '0');
                        cmd7.StationType  = (byte)resDevice.StationType;
                        cmd7.StationCode  = resDevice.StationType == 2 ? resDevice.StationCode : 0;
                        List <Device> DeviceSubList = DeviceModule.GetAllDeviceSubList(resDevice.Id);
                        List <int>    list          = new List <int>();
                        foreach (Device DeviceSub in DeviceSubList)
                        {
                            list.Add(DeviceSub.StationCode);
                        }
                        cmd7.StationCodeList = list;
                    }
                    cmd7.RawDataChar = cmd7.WriteMsg();
                    cmd7.RawDataStr  = HexStringUtility.ByteArrayToHexString(cmd7.RawDataChar);

                    ToDtuCommand.AddBaseMessageToDtu(cmd7.AddressField + "-" + cmd7.AFN, cmd7);

                    cmd.AddressField = cmd7.AddressField;
                    cmd.RawDataStr   = cmd7.RawDataStr;
                    cmd.RawDataChar  = cmd7.RawDataChar;
                    cmd.AFN          = cmd7.AFN;

                    break;

                case 8:
                    string SerialNumber1 = this.textBox3.Text.Trim();
                    if (SerialNumber1.Length != 8)
                    {
                        SafeSetText(textBox2, "卡号长度只能为8位", false);
                        setAllButtonEnable(true);
                        return;
                    }
                    if (!Regex.IsMatch(SerialNumber1, "^[0-9A-Fa-f]+$"))
                    {
                        SafeSetText(textBox2, "卡号只能为0-9A-Fa-f", false);
                        setAllButtonEnable(true);
                        return;
                    }

                    CmdToDtuShieldSerialNumber cmd8 = new CmdToDtuShieldSerialNumber();
                    cmd8.AddressField = DeviceModule.DeviceNo_Normal2Hex(deviceNo);
                    cmd8.StationType  = (byte)device.StationType;
                    cmd8.StationCode  = device.StationType == 2 ? device.StationCode : 0;
                    cmd8.SerialNumber = SerialNumber1;
                    cmd8.RawDataChar  = cmd8.WriteMsg();
                    cmd8.RawDataStr   = HexStringUtility.ByteArrayToHexString(cmd8.RawDataChar);
                    ToDtuCommand.AddBaseMessageToDtu(cmd8.AddressField + "-" + cmd8.AFN, cmd8);

                    cmd.AddressField = cmd8.AddressField;
                    cmd.RawDataStr   = cmd8.RawDataStr;
                    cmd.RawDataChar  = cmd8.RawDataChar;
                    cmd.AFN          = cmd8.AFN;
                    break;

                case 9:
                    string SerialNumber2 = this.textBox3.Text.Trim();
                    if (SerialNumber2.Length != 8)
                    {
                        SafeSetText(textBox2, "卡号长度只能为8位", false);
                        setAllButtonEnable(true);
                        return;
                    }
                    if (!Regex.IsMatch(SerialNumber2, "^[0-9A-Fa-f]+$"))
                    {
                        SafeSetText(textBox2, "卡号只能为0-9A-Fa-f", false);
                        setAllButtonEnable(true);
                        return;
                    }

                    CmdToDtuShieldSerialNumberCancel cmd9 = new CmdToDtuShieldSerialNumberCancel();
                    cmd9.AddressField = DeviceModule.DeviceNo_Normal2Hex(deviceNo);
                    cmd9.StationType  = (byte)device.StationType;
                    cmd9.StationCode  = device.StationType == 2 ? device.StationCode : 0;
                    cmd9.SerialNumber = SerialNumber2;
                    cmd9.RawDataChar  = cmd9.WriteMsg();
                    cmd9.RawDataStr   = HexStringUtility.ByteArrayToHexString(cmd9.RawDataChar);
                    ToDtuCommand.AddBaseMessageToDtu(cmd9.AddressField + "-" + cmd9.AFN, cmd9);

                    cmd.AddressField = cmd9.AddressField;
                    cmd.RawDataStr   = cmd9.RawDataStr;
                    cmd.RawDataChar  = cmd9.RawDataChar;
                    cmd.AFN          = cmd9.AFN;
                    break;

                case 10:
                    CmdToDtuSetGroundWaterParam cmd10 = new CmdToDtuSetGroundWaterParam();
                    cmd10.AddressField = DeviceModule.DeviceNo_Normal2Hex(deviceNo);
                    cmd10.StationType  = (byte)device.StationType;
                    cmd10.StationCode  = device.StationType == 2 ? device.StationCode : 0;
                    cmd10.Range        = byte.Parse(this.txtRange.Text.Trim());
                    cmd10.LineLength   = double.Parse(this.txtLineLength.Text.Trim());
                    cmd10.RawDataChar  = cmd10.WriteMsg();
                    cmd10.RawDataStr   = HexStringUtility.ByteArrayToHexString(cmd10.RawDataChar);
                    ToDtuCommand.AddBaseMessageToDtu(cmd10.AddressField + "-" + cmd10.AFN, cmd10);

                    cmd.AddressField = cmd10.AddressField;
                    cmd.RawDataStr   = cmd10.RawDataStr;
                    cmd.RawDataChar  = cmd10.RawDataChar;
                    cmd.AFN          = cmd10.AFN;
                    break;

                case 11:
                    CmdToDtuQueryGroundWaterParam cmd11 = new CmdToDtuQueryGroundWaterParam();
                    cmd11.AddressField = DeviceModule.DeviceNo_Normal2Hex(deviceNo);
                    cmd11.StationType  = (byte)device.StationType;
                    cmd11.StationCode  = device.StationType == 2 ? device.StationCode : 0;
                    cmd11.RawDataChar  = cmd11.WriteMsg();
                    cmd11.RawDataStr   = HexStringUtility.ByteArrayToHexString(cmd11.RawDataChar);
                    ToDtuCommand.AddBaseMessageToDtu(cmd11.AddressField + "-" + cmd11.AFN, cmd11);

                    cmd.AddressField = cmd11.AddressField;
                    cmd.RawDataStr   = cmd11.RawDataStr;
                    cmd.RawDataChar  = cmd11.RawDataChar;
                    cmd.AFN          = cmd11.AFN;
                    break;

                case 12:
                    CmdToDtuQueryGroundWater cmd12 = new CmdToDtuQueryGroundWater();
                    cmd12.AddressField = DeviceModule.DeviceNo_Normal2Hex(deviceNo);
                    cmd12.StationType  = (byte)device.StationType;
                    cmd12.StationCode  = device.StationType == 2 ? device.StationCode : 0;
                    cmd12.RawDataChar  = cmd12.WriteMsg();
                    cmd12.RawDataStr   = HexStringUtility.ByteArrayToHexString(cmd12.RawDataChar);
                    ToDtuCommand.AddBaseMessageToDtu(cmd12.AddressField + "-" + cmd12.AFN, cmd12);

                    cmd.AddressField = cmd12.AddressField;
                    cmd.RawDataStr   = cmd12.RawDataStr;
                    cmd.RawDataChar  = cmd12.RawDataChar;
                    cmd.AFN          = cmd12.AFN;
                    break;

                case 13:
                    CmdToDtuQueryState cmd13 = new CmdToDtuQueryState();
                    cmd13.AddressField = DeviceModule.DeviceNo_Normal2Hex(deviceNo);
                    cmd13.StationType  = (byte)device.StationType;
                    cmd13.StationCode  = device.StationType == 2 ? device.StationCode : 0;
                    cmd13.RawDataChar  = cmd13.WriteMsg();
                    cmd13.RawDataStr   = HexStringUtility.ByteArrayToHexString(cmd13.RawDataChar);
                    ToDtuCommand.AddBaseMessageToDtu(cmd13.AddressField + "-" + cmd13.AFN, cmd13);

                    cmd.AddressField = cmd13.AddressField;
                    cmd.RawDataStr   = cmd13.RawDataStr;
                    cmd.RawDataChar  = cmd13.RawDataChar;
                    cmd.AFN          = cmd13.AFN;
                    break;
                }

                SafeSetText(textBox2, cmd.RawDataStr, false);
                SafeSetText(textBox2, Environment.NewLine + "添加发送命令保存!" + cmd.AddressField + "-" + cmd.AFN, true);

                DeviceOperation op = new DeviceOperation();
                op.DeviceNo      = deviceNo;
                op.DeviceName    = DeviceModule.GetDeviceByFullDeviceNo(deviceNo).DeviceName;
                op.OperationTime = DateTime.Now;
                op.OperationType = OperationType;
                op.RawData       = cmd.RawDataStr;
                op.Remark        = "";
                op.UserId        = -1;
                op.UserName      = "******";
                op.State         = "等待发送";

                if (OnlineDeviceService.GetOnline(deviceNo) != null || OnlineDeviceService.GetOnline(resDeviceNo) != null)
                {
                    if (bridge != null)
                    {
                        if (bridge.sendToDtu(deviceNo, resDeviceNo, cmd.RawDataChar, 0, cmd.RawDataChar.Length))
                        {
                            op.State = "发送成功";
                        }
                        else
                        {
                            op.State = "发送失败";
                        }
                    }

                    DeviceOperationModule.AddDeviceOperation(op);

                    if (op.State == "发送失败")
                    {
                        ToDtuCommand.RemoveBaseMessageFromDtu(cmd.AddressField + "-" + cmd.AFN);
                    }

                    if (ToDtuCommand.GetBaseMessageToDtuByKey(cmd.AddressField + "-" + cmd.AFN) != null)
                    {
                        int  timeDelay = 0;
                        bool flag      = false;
                        while (timeDelay < 300)
                        {
                            BaseMessage res = null;
                            switch (k)
                            {
                            case 1:
                                res = (CmdResponseToDtuSetDateTime)ToDtuCommand.GetBaseMessageFromDtuByKey(cmd.AddressField + "-" + cmd.AFN);
                                if (res != null)
                                {
                                    SafeSetText(textBox2, Environment.NewLine + OperationType + "成功!" + timeDelay + ",返回结果:" + ((CmdResponseToDtuSetDateTime)res).DateTimeNew.ToString("yyyy-MM-dd HH:mm:ss"), true);
                                    ToDtuCommand.RemoveBaseMessageFromDtu(cmd.AddressField + "-" + cmd.AFN);
                                    flag = true;
                                }
                                break;

                            case 2:
                                res = (CmdResponseToDtuSetYearExploitation)ToDtuCommand.GetBaseMessageFromDtuByKey(cmd.AddressField + "-" + cmd.AFN);
                                if (res != null)
                                {
                                    SafeSetText(textBox2, Environment.NewLine + OperationType + "成功!" + timeDelay + ",返回结果:" + ((CmdResponseToDtuSetYearExploitation)res).YearExploitation, true);
                                    ToDtuCommand.RemoveBaseMessageFromDtu(cmd.AddressField + "-" + cmd.AFN);
                                    flag = true;
                                }
                                break;

                            case 3:
                                res = (CmdResponseToDtuQueryDateTime)ToDtuCommand.GetBaseMessageFromDtuByKey(cmd.AddressField + "-" + cmd.AFN);
                                if (res != null)
                                {
                                    SafeSetText(textBox2, Environment.NewLine + OperationType + "成功!" + timeDelay + ",返回结果:" + ((CmdResponseToDtuQueryDateTime)res).DateTimeNew.ToString("yyyy-MM-dd HH:mm:ss"), true);
                                    ToDtuCommand.RemoveBaseMessageFromDtu(cmd.AddressField + "-" + cmd.AFN);
                                    flag = true;
                                }
                                break;

                            case 4:
                                res = (CmdResponseToDtuQueryYearExploitation)ToDtuCommand.GetBaseMessageFromDtuByKey(cmd.AddressField + "-" + cmd.AFN);
                                if (res != null)
                                {
                                    SafeSetText(textBox2, Environment.NewLine + OperationType + "成功!" + timeDelay + ",返回结果:" + ((CmdResponseToDtuQueryYearExploitation)res).YearExploitation, true);
                                    ToDtuCommand.RemoveBaseMessageFromDtu(cmd.AddressField + "-" + cmd.AFN);
                                    flag = true;
                                }
                                break;

                            case 5:
                                res = (CmdResponseToDtuOpenPump)ToDtuCommand.GetBaseMessageFromDtuByKey(cmd.AddressField + "-" + cmd.AFN);
                                if (res != null)
                                {
                                    SafeSetText(textBox2, Environment.NewLine + OperationType + "成功!" + timeDelay + ",返回结果:" + ((CmdResponseToDtuOpenPump)res).Result, true);
                                    ToDtuCommand.RemoveBaseMessageFromDtu(cmd.AddressField + "-" + cmd.AFN);
                                    flag = true;
                                }
                                break;

                            case 6:
                                res = (CmdResponseToDtuClosePump)ToDtuCommand.GetBaseMessageFromDtuByKey(cmd.AddressField + "-" + cmd.AFN);
                                if (res != null)
                                {
                                    SafeSetText(textBox2, Environment.NewLine + OperationType + "成功!" + timeDelay + ",返回结果:" + ((CmdResponseToDtuClosePump)res).Result, true);
                                    ToDtuCommand.RemoveBaseMessageFromDtu(cmd.AddressField + "-" + cmd.AFN);
                                    flag = true;
                                }
                                break;

                            case 7:
                                res = (CmdResponseToDtuSetStationCode)ToDtuCommand.GetBaseMessageFromDtuByKey(cmd.AddressField + "-" + cmd.AFN);
                                if (res != null)
                                {
                                    SafeSetText(textBox2, Environment.NewLine + OperationType + "成功!" + timeDelay + ",返回结果:" + ((CmdResponseToDtuSetStationCode)res).Result, true);
                                    ToDtuCommand.RemoveBaseMessageFromDtu(cmd.AddressField + "-" + cmd.AFN);
                                    flag = true;
                                }
                                break;

                            case 8:
                                res = (CmdResponseToDtuShieldSerialNumber)ToDtuCommand.GetBaseMessageFromDtuByKey(cmd.AddressField + "-" + cmd.AFN);
                                if (res != null)
                                {
                                    SafeSetText(textBox2, Environment.NewLine + OperationType + "成功!" + timeDelay + ",返回结果:" + ((CmdResponseToDtuShieldSerialNumber)res).Result, true);
                                    ToDtuCommand.RemoveBaseMessageFromDtu(cmd.AddressField + "-" + cmd.AFN);
                                    flag = true;
                                }
                                break;

                            case 9:
                                res = (CmdResponseToDtuShieldSerialNumberCancel)ToDtuCommand.GetBaseMessageFromDtuByKey(cmd.AddressField + "-" + cmd.AFN);
                                if (res != null)
                                {
                                    SafeSetText(textBox2, Environment.NewLine + OperationType + "成功!" + timeDelay + ",返回结果:" + ((CmdResponseToDtuShieldSerialNumberCancel)res).Result, true);
                                    ToDtuCommand.RemoveBaseMessageFromDtu(cmd.AddressField + "-" + cmd.AFN);
                                    flag = true;
                                }
                                break;

                            case 10:
                                res = (CmdResponseToDtuSetGroundWaterParam)ToDtuCommand.GetBaseMessageFromDtuByKey(cmd.AddressField + "-" + cmd.AFN);
                                if (res != null)
                                {
                                    SafeSetText(textBox2, Environment.NewLine + OperationType + "成功!" + timeDelay + ",返回结果:" + ((CmdResponseToDtuSetGroundWaterParam)res).Result, true);
                                    ToDtuCommand.RemoveBaseMessageFromDtu(cmd.AddressField + "-" + cmd.AFN);
                                    flag = true;
                                }
                                break;

                            case 11:
                                res = (CmdResponseToDtuQueryGroundWaterParam)ToDtuCommand.GetBaseMessageFromDtuByKey(cmd.AddressField + "-" + cmd.AFN);
                                if (res != null)
                                {
                                    SafeSetText(textBox2, Environment.NewLine + OperationType + "成功!" + timeDelay + ",返回结果:" + ((CmdResponseToDtuQueryGroundWaterParam)res).Range + " | " + ((CmdResponseToDtuQueryGroundWaterParam)res).LineLength, true);
                                    SafeSetText(txtRange, ((CmdResponseToDtuQueryGroundWaterParam)res).Range.ToString(), false);
                                    SafeSetText(txtLineLength, ((CmdResponseToDtuQueryGroundWaterParam)res).LineLength.ToString(), false);
                                    ToDtuCommand.RemoveBaseMessageFromDtu(cmd.AddressField + "-" + cmd.AFN);
                                    flag = true;
                                }
                                break;

                            case 12:
                                res = (CmdResponseToDtuQueryGroundWater)ToDtuCommand.GetBaseMessageFromDtuByKey(cmd.AddressField + "-" + cmd.AFN);
                                if (res != null)
                                {
                                    SafeSetText(textBox2, Environment.NewLine + OperationType + "成功!" + timeDelay + ",返回结果:" + ((CmdResponseToDtuQueryGroundWater)res).Acq_Time.ToString("yyyy-MM-dd HH:mm:ss") + " | " + ((CmdResponseToDtuQueryGroundWater)res).GroundWaterLevel + " | " + ((CmdResponseToDtuQueryGroundWater)res).LineLength, true);
                                    SafeSetText(txtAcq_Time, ((CmdResponseToDtuQueryGroundWater)res).Acq_Time.ToString("yyyy-MM-dd HH:mm:ss"), false);
                                    SafeSetText(txtGroundWaterLevel, ((CmdResponseToDtuQueryGroundWater)res).GroundWaterLevel.ToString(), false);
                                    SafeSetText(txtLineLength, ((CmdResponseToDtuQueryGroundWater)res).LineLength.ToString(), false);
                                    ToDtuCommand.RemoveBaseMessageFromDtu(cmd.AddressField + "-" + cmd.AFN);
                                    flag = true;
                                }
                                break;

                            case 13:
                                res = (CmdResponseToDtuQueryState)ToDtuCommand.GetBaseMessageFromDtuByKey(cmd.AddressField + "-" + cmd.AFN);
                                if (res != null)
                                {
                                    SafeSetText(textBox2, Environment.NewLine + OperationType + "成功!" + timeDelay + ",返回结果:" + ((CmdResponseToDtuQueryState)res).State +
                                                ",上报时间:" + ((CmdResponseToDtuQueryState)res).DateTimeNew.ToString("yyyy-MM-dd HH:mm:ss") +
                                                //",累计用电量:" + ((CmdResponseToDtuQueryState)res).ElectricUsed +
                                                ",累计用水量:" + ((CmdResponseToDtuQueryState)res).WaterUsed, true);
                                    ToDtuCommand.RemoveBaseMessageFromDtu(cmd.AddressField + "-" + cmd.AFN);
                                    flag = true;
                                }
                                break;
                            }
                            if (flag)
                            {
                                if (res != null)
                                {
                                    SafeSetText(textBox2, Environment.NewLine + "是否带密码:" + (res.IsPW ? "1|" + res.PW : "0") + ",是否带时间戳:" + (res.IsTP ? "1|" + res.TP : "0"), true);
                                }
                                break;
                            }
                            Thread.Sleep(100);
                            timeDelay = timeDelay + 1;
                        }
                        if (!flag)
                        {
                            SafeSetText(textBox2, Environment.NewLine + OperationType + "出错!" + "响应超时", true);
                        }
                    }
                    else
                    {
                        SafeSetText(textBox2, Environment.NewLine + OperationType + "出错!" + "无命令保存", true);
                    }
                }
                else
                {
                    op.State = "终端离线";
                    SafeSetText(textBox2, Environment.NewLine + OperationType + "出错!" + "终端离线", true);
                }
                DeviceOperationModule.AddDeviceOperation(op);
            }
            catch (Exception ex)
            {
                SafeSetText(textBox2, OperationType + "出错!" + Environment.NewLine + ex.Message, false);
            }

            setAllButtonEnable(true);
        }