Esempio n. 1
0
        private void LoadData(object sender, EventArgs e)
        {
            try
            {
                string command = "LoadAllData";

                paraDict = new Dictionary <string, string>
                {
                    { "data", command }
                };

                dapProtocol = new DAPProtocol(command, paraDict);

                string json = JsonConvert.SerializeObject(dapProtocol, Formatting.Indented);

                byte[] commBuffer = new byte[2048];
                commBuffer = Encoding.Default.GetBytes(json);
                int len = commSocket.Send(commBuffer);

                string tempMsg = "[" + DateTime.Now.ToString() + "] to " + commSocket.RemoteEndPoint + ":" + command;
                clientLog.Invoke(receiveCallBack, tempMsg);
            }
            catch (Exception ex)
            {
                MessageBox.Show("ReadDAP exception:\n" + ex.Message);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// send data to DAPEmulator
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SendMsg(object sender, EventArgs e)
        {
            try
            {
                string command = "TST";

                string tempText = this.msgText.Text;
                if (!tempText.Equals(""))
                {
                    command = tempText;
                }

                paraDict = new Dictionary <string, string>
                {
                    { "data", command },
                };

                dapProtocol = new DAPProtocol(command, paraDict);

                string json = JsonConvert.SerializeObject(dapProtocol, Formatting.Indented);

                byte[] buffer = new byte[2048];
                buffer = Encoding.Default.GetBytes(json);
                int receive = commSocket.Send(buffer);

                string tempMsg = "[" + DateTime.Now.ToString() + "] to " + commSocket.RemoteEndPoint + ":" + paraDict["data"];
                clientLog.Invoke(receiveCallBack, tempMsg);
            }
            catch (Exception ex)
            {
                MessageBox.Show("data send exception:\n" + ex.Message);
            }
        }
Esempio n. 3
0
        private void SaveCorrectionFactor(object sender, EventArgs e)
        {
            if (this.factor.Text.Equals(""))
            {
                MessageBox.Show("the value of CorrectionFactor is empty");
            }
            else
            {
                try
                {
                    if (CorrectionFactor == -1.00)
                    {
                        MessageBox.Show("please click 'Read Data' button firstly !");
                    }
                    else
                    {
                        string tempCorrectioFactor = this.factor.Text;

                        //string command = "%SFD" + CorrectionFactor.ToString();
                        string command = tempCorrectioFactor;

                        paraDict = new Dictionary <string, string>
                        {
                            { "data", command }
                        };

                        dapProtocol = new DAPProtocol("%SFD", paraDict);
                        string json = JsonConvert.SerializeObject(dapProtocol, Formatting.Indented);

                        string tempMsg = "[" + DateTime.Now.ToString() + "] to " + commSocket.RemoteEndPoint + ":" + dapProtocol.Head + command;
                        clientLog.Invoke(receiveCallBack, tempMsg);

                        byte[] buffer = new byte[2048];
                        buffer = Encoding.Default.GetBytes(json);
                        int receive = commSocket.Send(buffer);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("data send exception:\n" + ex.Message);
                }
            }
        }
Esempio n. 4
0
        public void SendJsonData(string dataContent)
        {
            try
            {
                if (dataContent.Equals("reset"))
                {
                    paraDict = new Dictionary <string, string>
                    {
                        { "data", dataContent },
                    };

                    dapProtocol = new DAPProtocol(dataContent, paraDict);
                }
                else
                {
                    string[] commandStr = dataContent.Split('?');
                    string[] dataStr    = commandStr[1].Split('&');

                    paraDict = new Dictionary <string, string>
                    {
                        { "AirPressure", dataStr[0] },
                        { "Temperature", dataStr[1] }
                    };

                    dapProtocol = new DAPProtocol(commandStr[0], paraDict);
                }


                string json = JsonConvert.SerializeObject(dapProtocol, Formatting.Indented);

                byte[] commBuffer = new byte[2048];
                commBuffer = Encoding.Default.GetBytes(json);
                int len = commSocket.Send(commBuffer);

                string tempMsg = "[" + DateTime.Now.ToString() + "] to " + commSocket.RemoteEndPoint + ":" + command;
                clientLog.Invoke(receiveCallBack, tempMsg);
            }
            catch (Exception ex)
            {
                MessageBox.Show("ReadDAP exception:\n" + ex.Message);
            }
        }
Esempio n. 5
0
        /// <summary>
        /// receive data from DAPEmulator
        /// </summary>
        public void Receive(object socketSend)
        {
            commSocket = socketSend as Socket;

            try
            {
                while (true)
                {
                    byte[] buffer = new byte[2048];

                    int count = commSocket.Receive(buffer);


                    if (count == 0)
                    {
                        break;
                    }
                    else
                    {
                        string jsonStr = Encoding.Default.GetString(buffer, 0, count);
                        dapProtocol = JsonConvert.DeserializeObject <DAPProtocol>(jsonStr);
                        command     = dapProtocol.Head;
                        paraDict    = dapProtocol.Body;


                        if (command.Equals(""))
                        {
                            MessageBox.Show("do not get the value of 'DAP_corr_fctor'");
                        }
                        else
                        {
                            switch (command)
                            {
                            case "LoadAllData":

                                sw_version       = paraDict["sw_version"];
                                InternalSN       = paraDict["InternalSN"];
                                CorrectionFactor = double.Parse(paraDict["CorrectionFactor"]);
                                AirPressure      = int.Parse(paraDict["AirPressure"]);
                                Temperature      = int.Parse(paraDict["Temperature"]);

                                version.Text  = sw_version.Trim();
                                sn.Text       = InternalSN;
                                factor.Text   = CorrectionFactor.ToString();
                                pressure.Text = AirPressure.ToString();
                                T.Text        = Temperature.ToString();
                                Application.DoEvents();    //refresh textbox

                                break;

                            case "reset":
                                sw_version       = paraDict["sw_version"];
                                InternalSN       = paraDict["InternalSN"];
                                CorrectionFactor = double.Parse(paraDict["CorrectionFactor"]);
                                AirPressure      = int.Parse(paraDict["AirPressure"]);
                                Temperature      = int.Parse(paraDict["Temperature"]);

                                version.Text  = sw_version.Trim();
                                sn.Text       = InternalSN;
                                factor.Text   = CorrectionFactor.ToString();
                                pressure.Text = AirPressure.ToString();
                                T.Text        = Temperature.ToString();

                                Application.DoEvents();

                                break;

                            case "savePT":
                                MessageBox.Show(paraDict["data"]);
                                break;

                            case "%RFD":
                                double tempCorrectionFactor = double.Parse(paraDict["data"]);
                                tempCorrectionFactor = 1 / (1 + 0.0451) * tempCorrectionFactor;
                                Application.DoEvents();
                                break;

                            case "%SFD":
                                MessageBox.Show("now Correction Factor is: " + paraDict["data"]);
                                break;

                            case "TST":
                                MessageBox.Show("stability test successfully");
                                break;

                            default:
                                MessageBox.Show("invalid command");
                                break;
                            }

                            string log = "[" + DateTime.Now.ToString() + "] from " + commSocket.RemoteEndPoint + ":" + command;
                            clientLog.Invoke(receiveCallBack, log);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error From DAP_emulator:\n" + ex.ToString());
            }
        }
Esempio n. 6
0
        /// <summary>
        ///  method for receiving and processing packets from client in a loop;
        ///  there are different prefixes in data string:
        ///  "%SFD", it is used to write Correction Factor of DAP to DAP Emulator;
        ///
        /// </summary>
        /// <param name="obj"></param>
        public void RunThread(object UpdataUIDelegate)
        {
            threadDelegate = UpdataUIDelegate as DelegateCollection.updateUIControlDelegate;
            Dictionary <string, string> factDict;

            string jsonStr     = string.Empty;
            string command     = string.Empty;
            string dataContent = string.Empty;
            string logStr      = string.Empty;

            while (true)
            {
                byte[] buffer = new byte[2048];

                int count = commSocket.Receive(buffer);

                if (count == 0)
                {
                    break;
                }
                else
                {
                    jsonStr     = Encoding.Default.GetString(buffer, 0, count);
                    dapProtocol = JsonConvert.DeserializeObject <DAPProtocol>(jsonStr);
                    command     = dapProtocol.Head;
                    switch (command)
                    {
                    case "TST":
                        if (threadDelegate != null)
                        {
                            threadDelegate(command, true, commSocket.RemoteEndPoint.ToString());
                        }

                        factDict = new Dictionary <string, string>
                        {
                            { "data", command }
                        };

                        dapProtocol = new DAPProtocol("TST", factDict);
                        buffer      = utils.JsonDataOperationProtocol(dapProtocol);
                        commSocket.Send(buffer);

                        if (threadDelegate != null)
                        {
                            threadDelegate(command, false, commSocket.RemoteEndPoint.ToString());
                        }
                        break;

                    case "%RFD":
                        if (threadDelegate != null)
                        {
                            threadDelegate(command, true, commSocket.RemoteEndPoint.ToString());
                        }

                        dataContent = dapParameters.CorrectionFactor.ToString();
                        factDict    = new Dictionary <string, string>
                        {
                            { "data", dataContent }
                        };
                        dapProtocol = new DAPProtocol("%RFD", factDict);
                        buffer      = utils.JsonDataOperationProtocol(dapProtocol);
                        commSocket.Send(buffer);

                        if (threadDelegate != null)
                        {
                            threadDelegate(command, false, commSocket.RemoteEndPoint.ToString());
                        }
                        break;

                    case "%SFD":
                        if (threadDelegate != null)
                        {
                            threadDelegate(command, true, commSocket.RemoteEndPoint.ToString());
                        }

                        double tempCorrectionFactor = double.Parse(dapProtocol.Body["data"]);
                        if ((int)(tempCorrectionFactor * 100) != (int)(dapParameters.CorrectionFactor * 100))
                        {
                            dapParameters.CorrectionFactor = tempCorrectionFactor;
                            emulatorUI.factor.Text         = dapParameters.CorrectionFactor.ToString();
                            Application.DoEvents();

                            dataContent = "Correction factor save successfully";
                        }
                        else
                        {
                            dataContent = "Correction factor does not change";
                        }

                        factDict = new Dictionary <string, string>
                        {
                            { "data", dataContent }
                        };

                        dapProtocol = new DAPProtocol("%SFD", factDict);
                        buffer      = utils.JsonDataOperationProtocol(dapProtocol);
                        commSocket.Send(buffer);

                        if (threadDelegate != null)
                        {
                            threadDelegate(command, false, commSocket.RemoteEndPoint.ToString());
                        }


                        MessageBox.Show(dapProtocol.Body["data"]);
                        break;

                    case "LoadAllData":
                        if (threadDelegate != null)
                        {
                            threadDelegate(command, true, commSocket.RemoteEndPoint.ToString());
                        }

                        factDict = new Dictionary <string, string>
                        {
                            { "sw_version", dapParameters.sw_version },
                            { "InternalSN", dapParameters.InternalSN },
                            { "CorrectionFactor", dapParameters.CorrectionFactor.ToString() },
                            { "AirPressure", dapParameters.AirPressure.ToString() },
                            { "Temperature", dapParameters.Temperature.ToString() },
                        };


                        if (threadDelegate != null)
                        {
                            threadDelegate(command, false, commSocket.RemoteEndPoint.ToString());
                        }

                        dapProtocol = new DAPProtocol("LoadAllData", factDict);
                        buffer      = utils.JsonDataOperationProtocol(dapProtocol);
                        commSocket.Send(buffer);


                        break;

                    case "reset":
                        if (threadDelegate != null)
                        {
                            threadDelegate(command, true, commSocket.RemoteEndPoint.ToString());
                        }

                        factDict = new Dictionary <string, string>
                        {
                            { "sw_version", dapParameters.sw_version_factory },
                            { "InternalSN", dapParameters.InternalSN_factory },
                            { "CorrectionFactor", dapParameters.CorrectionFactor_factory.ToString() },
                            { "AirPressure", dapParameters.AirPressure_factory.ToString() },
                            { "Temperature", dapParameters.Temperature_factory.ToString() },
                        };
                        dapProtocol = new DAPProtocol("reset", factDict);
                        buffer      = utils.JsonDataOperationProtocol(dapProtocol);
                        commSocket.Send(buffer);

                        if (threadDelegate != null)
                        {
                            threadDelegate(command, false, commSocket.RemoteEndPoint.ToString());
                        }

                        break;

                    case "savePT":
                        if (threadDelegate != null)
                        {
                            threadDelegate(command, true, commSocket.RemoteEndPoint.ToString());
                        }

                        int tempAirPressure = int.Parse(dapProtocol.Body["AirPressure"]);
                        int tempTemperature = int.Parse(dapProtocol.Body["Temperature"]);

                        if ((tempAirPressure != dapParameters.AirPressure) || (tempTemperature != dapParameters.Temperature))
                        {
                            dapParameters.AirPressure = tempAirPressure;
                            dapParameters.Temperature = tempTemperature;

                            dataContent = "AirPressure and Temperature saved successfully";
                        }
                        else
                        {
                            dataContent = "the value of pressure or temperature does not change";
                        }

                        factDict = new Dictionary <string, string>
                        {
                            { "data", dataContent }
                        };
                        dapProtocol = new DAPProtocol("savePT", factDict);
                        buffer      = utils.JsonDataOperationProtocol(dapProtocol);
                        commSocket.Send(buffer);

                        if (threadDelegate != null)
                        {
                            threadDelegate(command, false, commSocket.RemoteEndPoint.ToString());
                        }
                        break;

                    case "Validate":
                        if (threadDelegate != null)
                        {
                            threadDelegate(command, true, commSocket.RemoteEndPoint.ToString());
                        }

                        double tempFactor = double.Parse(dapProtocol.Body["CorrectionFactor"]);
                        dataContent = "success,Validate Successfully";
                        if ((tempFactor < 2.50) && (tempFactor > 0.25))
                        {
                            dataContent = "failure,sorry, inputed correction factor is not within tolerance";
                        }

                        factDict = new Dictionary <string, string>
                        {
                            { "data", dataContent }
                        };

                        dapProtocol = new DAPProtocol("Validate", factDict);
                        buffer      = utils.JsonDataOperationProtocol(dapProtocol);
                        commSocket.Send(buffer);

                        if (threadDelegate != null)
                        {
                            threadDelegate(command, false, commSocket.RemoteEndPoint.ToString());
                        }
                        break;

                    default:
                        break;
                    }
                }
            }
        }