Beispiel #1
0
        /// <summary>
        /// 保存配置控件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SaveConfigbutton_Click(object sender, EventArgs e)
        {
            if (this.EditConfigtextBox.Text == "")
            {
                //MessageBox.Show("配置文件名称不能为空!");
                //MessageBox.Show("0x30001020 The configuration file's name cannot be empty!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                MessageBox.Show("0x30001020 " + LanguageContainer.SaveConfigFormMessagebox1Text[GlobalDataInterface.selectLanguageIndex],
                                LanguageContainer.SaveConfigFormMessageboxInformationCaption[GlobalDataInterface.selectLanguageIndex],
                                MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.EditConfigtextBox.Focus();
                return;
            }
            if (!Directory.Exists(System.Environment.CurrentDirectory + "\\config\\" + GlobalDataInterface.VERSION_SHOW + "\\"))
            {
                Directory.CreateDirectory(System.Environment.CurrentDirectory + "\\config\\" + GlobalDataInterface.VERSION_SHOW + "\\");
            }
            string FileName = System.Environment.CurrentDirectory;

            FileName += "\\config\\" + GlobalDataInterface.VERSION_SHOW + "\\";
            //if (this.EditConfigtextBox.Text=="")
            //{
            FileName += this.EditConfigtextBox.Text;
            if (m_IsProjectConfig)
            {
                FileName += ".exp";
            }
            else
            {
                FileName += ".cmc";
            }
            //}
            //else
            //{
            //    FileName += (string)this.ConfiglistBox.Items[this.ConfiglistBox.SelectedIndex];
            //}

            FileInfo configFile = new FileInfo(FileName);

            byte[]     FileData;
            FileStream configStream;

            try
            {
                if (configFile.Exists)
                {
                    //DialogResult result = MessageBox.Show("是否覆盖原来的配置信息?", "保存配置", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    //DialogResult result = MessageBox.Show("0x30001021 Whether to overwrite the original configuration information?", "Information", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    DialogResult result = MessageBox.Show("0x30001021 " + LanguageContainer.SaveConfigFormMessagebox2Text[GlobalDataInterface.selectLanguageIndex],
                                                          LanguageContainer.SaveConfigFormMessageboxInformationCaption[GlobalDataInterface.selectLanguageIndex],
                                                          MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (result == DialogResult.No)
                    {
                        return;
                    }
                }

                configStream = new FileStream(FileName, FileMode.Create, FileAccess.Write);
                if (m_IsProjectConfig)
                {
                    //寻找最长结构体长度
                    int MaxLenth = Marshal.SizeOf(typeof(stSysConfig));
                    if (MaxLenth < Marshal.SizeOf(typeof(stExitInfo)))
                    {
                        MaxLenth = Marshal.SizeOf(typeof(stExitInfo));
                    }
                    if (MaxLenth < Marshal.SizeOf(typeof(stWeightBaseInfo)))
                    {
                        MaxLenth = Marshal.SizeOf(typeof(stWeightBaseInfo));
                    }
                    if (MaxLenth < Marshal.SizeOf(typeof(stParas)))
                    {
                        MaxLenth = Marshal.SizeOf(typeof(stParas));
                    }
                    if (MaxLenth < Marshal.SizeOf(typeof(stGlobalExitInfo)))
                    {
                        MaxLenth = Marshal.SizeOf(typeof(stGlobalExitInfo));
                    }
                    if (MaxLenth < Marshal.SizeOf(typeof(stGlobalWeightBaseInfo)))
                    {
                        MaxLenth = Marshal.SizeOf(typeof(stGlobalWeightBaseInfo));
                    }
                    //if (MaxLenth < Marshal.SizeOf(typeof(stSpotDetectThresh)))
                    //    MaxLenth = Marshal.SizeOf(typeof(stSpotDetectThresh));

                    FileData = new byte[MaxLenth];
                    configStream.Seek(0, SeekOrigin.Begin);

                    byte[] version = BitConverter.GetBytes(GlobalDataInterface.Version);
                    configStream.Write(version, 0, sizeof(Int32));//2016-12-6加版本号校验

                    FileData = Commonfunction.StructToBytes(GlobalDataInterface.globalOut_SysConfig);
                    configStream.Write(FileData, 0, Marshal.SizeOf(typeof(stSysConfig)));

                    for (int i = 0; i < ConstPreDefine.MAX_SUBSYS_NUM * ConstPreDefine.MAX_CHANNEL_NUM; i++)
                    {
                        FileData = Commonfunction.StructToBytes(GlobalDataInterface.globalOut_ExitInfo[i]);
                        configStream.Write(FileData, 0, Marshal.SizeOf(typeof(stExitInfo)));
                    }
                    for (int i = 0; i < ConstPreDefine.MAX_SUBSYS_NUM * ConstPreDefine.MAX_CHANNEL_NUM; i++)
                    {
                        FileData = Commonfunction.StructToBytes(GlobalDataInterface.globalOut_WeightBaseInfo[i]);
                        configStream.Write(FileData, 0, Marshal.SizeOf(typeof(stWeightBaseInfo)));
                    }
                    for (int i = 0; i < ConstPreDefine.MAX_SUBSYS_NUM * ConstPreDefine.MAX_IPM_NUM; i++)
                    {
                        FileData = Commonfunction.StructToBytes(GlobalDataInterface.globalOut_Paras[i]);
                        configStream.Write(FileData, 0, Marshal.SizeOf(typeof(stParas)));
                    }
                    for (int i = 0; i < ConstPreDefine.MAX_SUBSYS_NUM; i++)
                    {
                        FileData = Commonfunction.StructToBytes(GlobalDataInterface.globalOut_GlobalExitInfo[i]);
                        configStream.Write(FileData, 0, Marshal.SizeOf(typeof(stGlobalExitInfo)));
                    }
                    for (int i = 0; i < ConstPreDefine.MAX_SUBSYS_NUM; i++)
                    {
                        FileData = Commonfunction.StructToBytes(GlobalDataInterface.globalOut_GlobalWeightBaseInfo[i]);
                        configStream.Write(FileData, 0, Marshal.SizeOf(typeof(stGlobalWeightBaseInfo)));
                    }
                    //for (int i = 0; i < ConstPreDefine.MAX_SUBSYS_NUM * ConstPreDefine.MAX_IPM_NUM; i++)
                    //{
                    //    FileData = Commonfunction.StructToBytes(GlobalDataInterface.globalOut_SpotDetectThresh[i]);
                    //    configStream.Write(FileData, 0, Marshal.SizeOf(typeof(stSpotDetectThresh)));
                    //}

                    configStream.Write(FileData, 0, sizeof(bool));
                    FileData = BitConverter.GetBytes(GlobalDataInterface.CIRAvailable);
                    configStream.Write(FileData, 0, sizeof(bool));
                    FileData = BitConverter.GetBytes(GlobalDataInterface.WeightAvailable);
                    configStream.Write(FileData, 0, sizeof(bool));
                    GlobalDataInterface.NetStateSum = BitConverter.ToBoolean(FileData, 0);
                    for (int i = 0; i < ConstPreDefine.MAX_SUBSYS_NUM; i++)
                    {
                        FileData = BitConverter.GetBytes(GlobalDataInterface.NetState[i]);
                        configStream.Write(FileData, 0, sizeof(int));
                    }
                    FileData = BitConverter.GetBytes(GlobalDataInterface.CupStateSum);
                    configStream.Write(FileData, 0, sizeof(bool));
                    for (int i = 0; i < ConstPreDefine.MAX_SUBSYS_NUM; i++)
                    {
                        FileData = BitConverter.GetBytes(GlobalDataInterface.CupState[i]);
                        configStream.Write(FileData, 0, sizeof(int));
                    }
                    configStream.Close();
                }
                else//保存用户配置
                {
                    FileData = new byte[Marshal.SizeOf(typeof(stGradeInfo))];
                    configStream.Seek(0, SeekOrigin.Begin);

                    byte[] version = BitConverter.GetBytes(GlobalDataInterface.Version);
                    configStream.Write(version, 0, sizeof(Int32));//2016-12-6加版本号校验

                    FileData = Commonfunction.StructToBytes(GlobalDataInterface.globalOut_GradeInfo);
                    configStream.Write(FileData, 0, Marshal.SizeOf(typeof(stGradeInfo)));

                    /*用户配置增加颜色界面-》颜色列表背景颜色保存*/
                    string      color;
                    byte[]      tempcolor = new byte[ConstPreDefine.MAX_TEXT_LENGTH];
                    stColorList colorlist = new stColorList(true);
                    color     = Commonfunction.GetAppSetting("颜色参数-颜色1");
                    tempcolor = Encoding.Default.GetBytes(color);
                    Array.Copy(tempcolor, colorlist.color1, tempcolor.Length);
                    color     = Commonfunction.GetAppSetting("颜色参数-颜色2");
                    tempcolor = Encoding.Default.GetBytes(color);
                    Array.Copy(tempcolor, colorlist.color2, tempcolor.Length);
                    color     = Commonfunction.GetAppSetting("颜色参数-颜色3");
                    tempcolor = Encoding.Default.GetBytes(color);
                    Array.Copy(tempcolor, colorlist.color3, tempcolor.Length);
                    FileData = Commonfunction.StructToBytes(colorlist);
                    configStream.Write(FileData, 0, Marshal.SizeOf(typeof(stColorList)));

                    //Add by ChengSk - 20190929
                    string clientinfo;
                    byte[] tempclientinfo    = new byte[ConstPreDefine.MAX_CLIENTINFO_LENGTH];
                    string ClientName        = "";                                                     //客户名称 Add by ChengSk - 20191115
                    string FarmName          = "";                                                     //农场名称 Add by ChengSk - 20191115
                    string FruitName         = "";                                                     //水果名称 Add by ChengSk - 20191115
                    string clientInfoContent = FileOperate.ReadFile(1, m_mainform.clientInfoFileName); //Modify by ChengSk - 20180308
                    if (clientInfoContent == null || clientInfoContent == "")
                    {
                        ClientName = "";
                        FarmName   = "";
                        FruitName  = "";
                    }
                    else
                    {
                        string[] clientInfoContentItem = clientInfoContent.Split(',');
                        ClientName = clientInfoContentItem[0].Trim();
                        FarmName   = clientInfoContentItem[1].Trim();
                        FruitName  = clientInfoContentItem[2].Trim();
                    }
                    stClientInfo ClientInfo = new stClientInfo(true);
                    clientinfo     = ClientName; //客户名称
                    tempclientinfo = Encoding.Default.GetBytes(clientinfo);
                    Array.Copy(tempclientinfo, ClientInfo.customerName, tempclientinfo.Length);
                    clientinfo     = FarmName; //农场名称
                    tempclientinfo = Encoding.Default.GetBytes(clientinfo);
                    Array.Copy(tempclientinfo, ClientInfo.farmName, tempclientinfo.Length);
                    clientinfo     = FruitName; //水果名称
                    tempclientinfo = Encoding.Default.GetBytes(clientinfo);
                    Array.Copy(tempclientinfo, ClientInfo.fruitName, tempclientinfo.Length);
                    FileData = Commonfunction.StructToBytes(ClientInfo);
                    configStream.Write(FileData, 0, Marshal.SizeOf(typeof(stClientInfo)));

                    configStream.Close();
                    m_mainform.SetSeparationProgrameChangelabel(true, this.EditConfigtextBox.Text);//ivycc 2013.11.26
                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show("保存配置文件失败:" + ex);
                //MessageBox.Show("0x1000100C Save configuration failed:" + ex, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                MessageBox.Show("0x1000100C " + LanguageContainer.SaveConfigFormMessagebox3Text[GlobalDataInterface.selectLanguageIndex] + ex,
                                LanguageContainer.SaveConfigFormMessageboxErrorCaption[GlobalDataInterface.selectLanguageIndex],
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Beispiel #2
0
        /// <summary>
        /// 一对一发送命令/数据
        /// </summary>
        /// <param name="nDestId"></param>命令/数据目的ID
        /// <param name="nCmd"></param>命令
        /// <param name="rData"></param>数据
        /// <returns></returns>
        public bool SyncRequest(int nDestId, int nCmd, byte[] Data)
        {
            bool   boRC = false;//运行正常与否标志位
            Socket socket;

            if (nCmd != (int)HC_FSM_COMMAND_TYPE.HC_CMD_DISPLAY_OFF &&
                nCmd != (int)HC_FSM_COMMAND_TYPE.HC_CMD_DISPLAY_ON &&
                !SubsysIsConnected(Commonfunction.GetSubsysIndex(nDestId)))
            {
                Trace.WriteLine(string.Format("当前子系统不可用,函数SyncRequest nDestId:{0},nCmd={1}", nDestId, nCmd));
#if REALEASE
                GlobalDataInterface.WriteErrorInfo(string.Format("当前子系统不可用,函数SyncRequest nDestId:{0},nCmd={1}", nDestId, nCmd));
#endif
                return(false);
            }
            //C++: AfxGetApp()->BeginWaitCursor();

            string strIP     = "";
            int    nSubsysId = Commonfunction.GetSubSysID(nDestId);
            int    nPortNum  = ConstPreDefine.FSM_PORT_NUM;

            if (nCmd >= 0x2000 && nCmd < 0x3000) //判断为HC->IPM的发送命令
            {
                nPortNum  = ConstPreDefine.IPM_PORT_NUM;
                nSubsysId = Commonfunction.GetIPMID(nDestId);
            }
            string strTemp = ConstPreDefine.LC_IP_ADDR_TEMPLATE;
            strIP = strTemp + nSubsysId;          //得到发送的IP地址
            //if (nCmd >= 0x7100 && nCmd <= 0x7101) //判断为HC->SIM的发送命令
            if (nCmd >= 0x7000 && nCmd <= 0x7102) //判断为HC->SIM的发送命令
            {
                nPortNum = ConstPreDefine.SIM_PORT_NUM;
                strIP    = ConstPreDefine.SIM_IP_ADDR;//得到发送的IP地址
            }

            try
            {
                Lock(1000);//资源加锁


                socket = CreateSocket();              //创建socket
                socket.SendBufferSize = 0;            //网络发送缓冲区为0
                socket.SendTimeout    = nTrySendTime; //设置发送超时时间
                socket.NoDelay        = false;

                //if (nCmd == (int)HC_FSM_COMMAND_TYPE.HC_CMD_DISPLAY_ON)
                //    boRC = ConnectoServer(socket, strIP, nPortNum, true);//初始连接
                //else
                //boRC = ConnectoServer(socket, strIP, nPortNum, false);//运行状态下连接
                boRC = ConnectoServer(socket, strIP, nPortNum, true);//2015-5-14 运行过程中会存在网线断了的情况


                if (!boRC)
                {
                    Trace.WriteLine("网络连接失败!");
#if REALEASE
                    GlobalDataInterface.WriteErrorInfo(string.Format("网络连接失败!"));
#endif
                    UnLock();

                    socket.Close();
                    socket = null;
                    Trace.WriteLine("Socket关闭");
#if REALEASE
                    GlobalDataInterface.WriteErrorInfo("Socket关闭");
#endif
                    return(boRC);
                }
                //byte[] ABC = new byte[1];
                //ABC[0] = 100;
                //if (nCmd == (int)HC_IPM_COMMAND_TYPE.HC_CMD_CONTINUOUS_SAMPLE_ON)
                //{
                //    QueryPerformanceFrequency(ref freq);
                //    QueryPerformanceCounter(ref count);
                //    Trace.WriteLine("发送连续采集前同步信息开始");
                //    boRC = Send(socket, ABC);//发送同步信息
                //    Trace.WriteLine("发送连续采集前同步信息结束");
                //}
                SendCMD cmd = new SendCMD();

                cmd.SYNC    = 0x434e5953;
                cmd.nSrcId  = ConstPreDefine.HC_ID; //发送源ID
                cmd.nDestId = nDestId;              //发送目的ID
                cmd.nCmd    = nCmd;

                int    nStructLen = 4 * sizeof(int);
                byte[] bytes      = new byte[nStructLen];
                bytes = Commonfunction.StructToBytes(cmd);//将结构体转化为byte数组
                //if (nCmd == (int)HC_IPM_COMMAND_TYPE.HC_CMD_CONTINUOUS_SAMPLE_ON)
                //    Trace.WriteLine("发送连续采集命令头开始");
                boRC = Send(socket, bytes);//发送命令头
                //if (nCmd == (int)HC_IPM_COMMAND_TYPE.HC_CMD_CONTINUOUS_SAMPLE_ON)
                //{
                //    //socket.RE
                //    Trace.WriteLine("发送连续采集命令头结束");
                //}
                ////测试获取图像命令发送时间
                //QueryPerformanceCounter(ref count1);
                //count = count1 - count;
                //result = (double)(count) / (double)freq;
                //Trace.WriteLine("发送连续采集同步信息+命令头时间:" + result);
                if (!boRC)
                {
                    Trace.WriteLine("网络发送命令头错误!");
#if REALEASE
                    GlobalDataInterface.WriteErrorInfo(string.Format("网络发送命令头错误!"));
#endif
                    goto leave;
                }

                int A = BitConverter.ToInt32(bytes, 12);

                if (Data != null)
                {
                    boRC = Send(socket, Data);//发送数据

                    if (!boRC)
                    {
                        Trace.WriteLine("网络发送数据错误!");
#if REALEASE
                        GlobalDataInterface.WriteErrorInfo(string.Format("网络发送数据错误!"));
#endif
                        goto leave;
                    }
                }


leave:
                DestroySocket(socket, boRC);
                UnLock();
                //AfxGetApp()->EndWaitCursor();
                if (boRC)
                {
                    if (Data != null)
                    {
                        Trace.WriteLine(string.Format("成功:函数SyncRequest nDestId:{0},nCmd={1},数据长度={2}", nDestId, nCmd, Data.Length));
//#if REALEASE
//                        GlobalDataInterface.WriteErrorInfo(string.Format("成功:函数SyncRequest nDestId:{0},nCmd={1},数据长度={2}", nDestId, nCmd, Data.Length));
//#endif
                    }
                    else
                    {
                        Trace.WriteLine(string.Format("成功:函数SyncRequest nDestId:{0},nCmd={1}", nDestId, nCmd));
//#if REALEASE
//                        GlobalDataInterface.WriteErrorInfo(string.Format("成功:函数SyncRequest nDestId:{0},nCmd={1}", nDestId, nCmd));
//#endif   //delete by xcw 20201207
                    }
                }
                else
                {
                    if (nCmd == (int)HC_FSM_COMMAND_TYPE.HC_CMD_DISPLAY_ON)
                    {
                        m_IsConnected[Commonfunction.GetSubsysIndex(nDestId)] = false;
                    }
                    if (Data != null)
                    {
                        Trace.WriteLine(string.Format("失败:函数SyncRequest nDestId:{0},nCmd={1},数据长度={2},当前时间" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), nDestId, nCmd, Data.Length));
#if REALEASE
                        GlobalDataInterface.WriteErrorInfo(string.Format("失败:函数SyncRequest nDestId:{0},nCmd={1},数据长度={2},当前时间" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), nDestId, nCmd, Data.Length));
#endif
                    }
                    else
                    {
                        Trace.WriteLine(string.Format("失败:函数SyncRequest nDestId:{0},nCmd={1},当前时间" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), nDestId, nCmd));
#if REALEASE
                        GlobalDataInterface.WriteErrorInfo(string.Format("失败:函数SyncRequest nDestId:{0},nCmd={1},当前时间" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), nDestId, nCmd));
#endif   //add by xcw 20201207
                    }
                }
            }
            catch (Exception e)
            {
                Trace.WriteLine("失败:函数SyncRequest" + e);
#if REALEASE
                GlobalDataInterface.WriteErrorInfo("失败:函数SyncRequest" + e);
#endif
                return(false);
            }

            return(boRC);
        }
        private void SendDevParaThread(object arg)
        {
            try
            {
                int channelNumber = (int)arg;
                int index         = 0;
                while (index < channelNumber)
                {
                    try
                    {
                        if (this.InvokeRequired)
                        {
                            this.BeginInvoke(new MethodInvoker(delegate
                            {
                                this.LabelWaitCount.Text = "通道" + (index + 1).ToString() + "基础信息发送中...";
                            }));
                        }
                        else
                        {
                            this.LabelWaitCount.Text = "通道" + (index + 1).ToString() + "基础信息发送中...";
                        }

                        //首先关闭当前的连接
                        if (tcpClient.IsConnected)
                        {
                            tcpClient.Close();
                            GlobalDataInterface.CommportConnectFlag = false;
                        }
                        else
                        {
                            GlobalDataInterface.CommportConnectFlag = false;
                        }

                        int SelId = m_ChanelIDList[index];
                        int m_InnerQualitySubsysindex  = Commonfunction.GetSubsysIndex(SelId);  //子系统索引
                        int m_InnerQualityChannelIndex = Commonfunction.GetChannelIndex(SelId); //子系统通道
                        int dataIndex = m_InnerQualitySubsysindex * ConstPreDefine.MAX_CHANNEL_NUM + m_InnerQualityChannelIndex;
                        m_TcpChannelIndex = dataIndex;                                          //当前选择的TCP通道索引
                        string ip = ConstPreDefine.LC_IP_ADDR_TEMPLATE + (dataIndex + 101);

                        #region 网络Ping验证
                        Ping      m_ping    = new Ping();
                        PingReply pingReply = m_ping.Send(ip, 500);
                        if (pingReply.Status != IPStatus.Success) //网络不通
                        {
                            Thread.Sleep(1000);
                            if (this.InvokeRequired)
                            {
                                this.BeginInvoke(new MethodInvoker(delegate
                                {
                                    this.LabelWaitCount.Text = "通道" + (index + 1).ToString() + "基础信息发送失败";
                                }));
                            }
                            else
                            {
                                this.LabelWaitCount.Text = "通道" + (index + 1).ToString() + "基础信息发送失败";
                            }
                            Thread.Sleep(1000);
                            index++;
                            continue;
                        }
                        #endregion

                        tcpClient.Connect(new IPEndPoint(IPAddress.Parse(ip), ConstPreDefine.LC_PORT_NUM)); //建立index+1的通道连接

                        Thread.Sleep(1000);
                        if (GlobalDataInterface.CommportConnectFlag)
                        {
                            GlobalDataInterface.globalOut_SysDevParaData.ToCopy(temp_SysDevParaData);
                            byte[] bytes = Commonfunction.StructToBytes(GlobalDataInterface.globalOut_SysDevParaData);
                            MessageDataSend(ConstPreDefine.SBC_PARA_SET, bytes);  //设置基础信息

                            if (this.InvokeRequired)
                            {
                                this.BeginInvoke(new MethodInvoker(delegate
                                {
                                    this.LabelWaitCount.Text = "通道" + (index + 1).ToString() + "基础信息发送成功";
                                }));
                            }
                            else
                            {
                                this.LabelWaitCount.Text = "通道" + (index + 1).ToString() + "基础信息发送成功";
                            }
                        }
                        else
                        {
                            if (this.InvokeRequired)
                            {
                                this.BeginInvoke(new MethodInvoker(delegate
                                {
                                    this.LabelWaitCount.Text = "通道" + (index + 1).ToString() + "基础信息发送失败";
                                }));
                            }
                            else
                            {
                                this.LabelWaitCount.Text = "通道" + (index + 1).ToString() + "基础信息发送失败";
                            }
                        }

                        Thread.Sleep(500);
                        if (tcpClient.IsConnected)  //最后关闭当前的连接(仅在有需要时才打开连接)
                        {
                            tcpClient.Close();
                            GlobalDataInterface.CommportConnectFlag = false;
                        }
                        else
                        {
                            GlobalDataInterface.CommportConnectFlag = false;
                        }
                    }
                    catch (Exception ex)
                    {
                        Trace.WriteLine("ProjectSetForm中函数OpenTcpThread的while循环出错,index=" + index.ToString() + ex);
#if REALEASE
                        GlobalDataInterface.WriteErrorInfo("ProjectSetForm中函数OpenTcpThread的while循环出错,index=" + index.ToString() + ex);
#endif
                    }
                    index++;
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine("ProjectSetForm中函数SendDevParaThread出错" + ex);
#if REALEASE
                GlobalDataInterface.WriteErrorInfo("ProjectSetForm中函数SendDevParaThread出错" + ex);
#endif
            }
        }
        //参数信息 - 保存
        private void ButtonSParaSave_Click(object sender, EventArgs e)
        {
            try
            {
                bool resFlag = false;
                resFlag = SetupParameterCheckProcess1();
                if (!resFlag)
                {
                    return;
                }
                resFlag = SetupParameterCheckProcess2();
                if (!resFlag)
                {
                    return;
                }
                SaveFileDialog SaveDialogFile = new SaveFileDialog();
                SaveDialogFile.DefaultExt = "*." + ConstPreDefine.FILE_NAME_PARA;
                SaveDialogFile.Filter     = ConstPreDefine.FILE_NAME_PARA + " Files (*." + ConstPreDefine.FILE_NAME_PARA + ")|*." + ConstPreDefine.FILE_NAME_PARA + "|All Files (*.*)|*.*";
                SaveDialogFile.Title      = "绿萌参数文件保存";
                if (SaveDialogFile.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                {
                    return;
                }
                string SaveFileName = SaveDialogFile.FileName;
                if (System.IO.File.Exists(SaveFileName))
                {
                    if (MessageBox.Show("文件已存在. 是否覆盖?"
                                        , "警告信息", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != System.Windows.Forms.DialogResult.Yes)
                    {
                        return;
                    }
                    System.IO.File.Delete(SaveFileName);
                }
                using (System.IO.FileStream fileStream = new System.IO.FileStream(SaveFileName, System.IO.FileMode.Create, System.IO.FileAccess.Write))
                {
                    int MaxLenth = Marshal.SizeOf(typeof(TSYS_DEV_PARAMETER));
                    if (MaxLenth < Marshal.SizeOf(typeof(TSYS_DEV_INFORMATION)))
                    {
                        MaxLenth = Marshal.SizeOf(typeof(TSYS_DEV_INFORMATION));
                    }
                    byte[] FileData = new byte[MaxLenth];
                    FileData = Commonfunction.StructToBytes(temp_SysDevParaData);
                    fileStream.Write(FileData, 0, Marshal.SizeOf(typeof(TSYS_DEV_PARAMETER)));  //写参数信息

                    for (int i = 0; i < ConstPreDefine.MAX_SUBSYS_NUM * ConstPreDefine.MAX_CHANNEL_NUM; i++)
                    {
                        FileData = Commonfunction.StructToBytes(temp_SysDevInfoData[i]);
                        fileStream.Write(FileData, 0, Marshal.SizeOf(typeof(TSYS_DEV_INFORMATION)));//写光谱信息
                    }
                }
                MessageBox.Show("保存成功: " + System.IO.Path.GetFileNameWithoutExtension(SaveFileName), "提示",
                                MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                Trace.WriteLine("ProjectSetForm-InnerQuality中函数ButtonSParaSave_Click出错" + ex);
#if REALEASE
                GlobalDataInterface.WriteErrorInfo("ProjectSetForm-InnerQuality中函数ButtonSParaSave_Click出错" + ex);
#endif
            }
        }
        /// <summary>
        /// 一对一发送命令/数据
        /// </summary>
        /// <param name="nDestId"></param>命令/数据目的ID
        /// <param name="nCmd"></param>命令
        /// <param name="rData"></param>数据
        /// <returns></returns>
        public bool SyncRequest(int nDestId, int nCmd, byte[] Data)
        {
            bool   boRC = false;//运行正常与否标志位
            Socket socket;

            if (nCmd != (int)HC_FSM_COMMAND_TYPE.HC_CMD_DISPLAY_OFF &&
                nCmd != (int)HC_FSM_COMMAND_TYPE.HC_CMD_DISPLAY_ON &&
                !SubsysIsConnected(Commonfunction.GetSubsysIndex(nDestId)))
            {
                Trace.WriteLine(string.Format("当前子系统不可用,函数SyncRequest nDestId:{0},nCmd={1}", nDestId, nCmd));
#if REALEASE
                GlobalDataInterface.WriteErrorInfo(string.Format("当前子系统不可用,函数SyncRequest nDestId:{0},nCmd={1}", nDestId, nCmd));
#endif
                return(false);
            }
            //C++: AfxGetApp()->BeginWaitCursor();

            string strIP     = "";
            int    nSubsysId = Commonfunction.GetSubSysID(nDestId);
            int    nPortNum  = ConstPreDefine.FSM_PORT_NUM;

            if (nCmd >= 0x2000 && nCmd < 0x3000) //判断为HC->IPM的发送命令
            {
                nPortNum  = ConstPreDefine.IPM_PORT_NUM;
                nSubsysId = Commonfunction.GetIPMID(nDestId);
            }

            string strTemp = ConstPreDefine.LC_IP_ADDR_TEMPLATE;
            strIP = strTemp + nSubsysId;//得到发送的IP地址


            try
            {
                Lock(1000);                //资源加锁

                socket = CreateSocket();   //创建socket
                socket.SendBufferSize = 0; //网络发送缓冲区为0
                if (nCmd == (int)HC_FSM_COMMAND_TYPE.HC_CMD_DISPLAY_ON)
                {
                    boRC = ConnectoServer(socket, strIP, nPortNum, true, true);                                                                                                                                                                       //初始连接
                }
                else if ((nCmd >= (int)HC_FSM_COMMAND_TYPE.HC_CMD_DISPLAY_OFF && nCmd <= (int)HC_FSM_COMMAND_TYPE.HC_CMD_CUPSTATERESET) || (nCmd > (int)HC_FSM_COMMAND_TYPE.HC_CMD_DISPLAY_ON && nCmd <= (int)HC_FSM_COMMAND_TYPE.HC_CMD_GRADE_INFO)) //所有子系统发送
                {
                    boRC = ConnectoServer(socket, strIP, nPortNum, false, true);                                                                                                                                                                      //运行状态下连接
                }
                else
                {
                    boRC = ConnectoServer(socket, strIP, nPortNum, false, false);//运行状态下连接
                }
                if (!boRC)
                {
                    Trace.WriteLine("网络连接失败!");
#if REALEASE
                    GlobalDataInterface.WriteErrorInfo(string.Format("网络连接失败!"));
#endif
                    UnLock();
                    socket.Close();
                    return(boRC);
                }

                SendCMD cmd = new SendCMD();

                cmd.SYNC    = 0x434e5953;
                cmd.nSrcId  = ConstPreDefine.HC_ID; //发送源ID
                cmd.nDestId = nDestId;              //发送目的ID
                cmd.nCmd    = nCmd;

                int    nStructLen = 4 * sizeof(int);
                byte[] bytes      = new byte[nStructLen];
                bytes = Commonfunction.StructToBytes(cmd); //将结构体转化为byte数组

                boRC = Send(socket, bytes);                //发送命令头
                if (!boRC)
                {
                    Trace.WriteLine("网络发送命令头错误!");
#if REALEASE
                    GlobalDataInterface.WriteErrorInfo(string.Format("网络发送命令头错误!"));
#endif
                    goto leave;
                }

                int A = BitConverter.ToInt32(bytes, 12);

                if (Data != null)
                {
                    boRC = Send(socket, Data);//发送数据
                    if (!boRC)
                    {
                        Trace.WriteLine("网络发送数据错误!");
#if REALEASE
                        GlobalDataInterface.WriteErrorInfo(string.Format("网络发送数据错误!"));
#endif
                        goto leave;
                    }
                }


leave:
                DestroySocket(socket, boRC);
                UnLock();
                //AfxGetApp()->EndWaitCursor();
                if (boRC)
                {
                    Trace.WriteLine(string.Format("成功:函数SyncRequest nDestId:{0},nCmd={1},数据长度={2}", nDestId, nCmd, Data.Length));
#if REALEASE
                    GlobalDataInterface.WriteErrorInfo(string.Format("成功:函数SyncRequest nDestId:{0},nCmd={1},数据长度={2}", nDestId, nCmd, Data.Length));
#endif
                }
                else
                {
                    if (nCmd == (int)HC_FSM_COMMAND_TYPE.HC_CMD_DISPLAY_ON)
                    {
                        m_IsConnected[Commonfunction.GetSubsysIndex(nDestId)] = false;
                    }
                    Trace.WriteLine(string.Format("失败:函数SyncRequest nDestId:{0},nCmd={1},数据长度={2}", nDestId, nCmd, Data.Length));
#if REALEASE
                    GlobalDataInterface.WriteErrorInfo(string.Format("失败:函数SyncRequest nDestId:{0},nCmd={1},数据长度={2}", nDestId, nCmd, Data.Length));
#endif
                }
            }
            catch (Exception e)
            {
                Trace.WriteLine("失败:函数SyncRequest" + e);
#if REALEASE
                GlobalDataInterface.WriteErrorInfo("失败:函数SyncRequest" + e);
#endif
                return(false);
            }

            return(boRC);
        }