Exemplo n.º 1
0
        private void buttonWriteConfig_Click(object sender, EventArgs e)
        {
            if (serialPortSSC.IsOpen)
            {
                FormPrompt   writeConfPrompt;
                DialogResult result       = DialogResult.None;
                string       promptString = "";

                while (promptString.Length < 8 && result != DialogResult.Cancel)
                {
                    writeConfPrompt = new FormPrompt();
                    writeConfPrompt.ShowDialog();
                    promptString = writeConfPrompt.promptText;
                    if (promptString.Length < 8)
                    {
                        result = MessageBox.Show("Password needs to be 8 characters long", "Password length incorrect", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
                    }
                }

                string[] tempConfs = new string[5];
                tempConfs[0] = textBoxName.Text;
                tempConfs[1] = textBoxlrv.Text;
                tempConfs[2] = textBoxurv.Text;
                tempConfs[3] = textBoxAlarml.Text;
                tempConfs[4] = textBoxAlarmh.Text;

                command = "writeconf";
                string confWriteString = command + ">" + promptString + ">" + string.Join(";", tempConfs);
                serialPortSSC.WriteLine(confWriteString);
                timerReadSerial.Enabled = true;
            }
            else
            {
                MessageBox.Show("Connect to a device to use this function", "Connection needed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
Exemplo n.º 2
0
        private void timerReadSerial_Tick(object sender, EventArgs e)
        {
            if (serialPortSSC.IsOpen)
            {
                if (command == "readconf")
                {
                    config = serialPortSSC.ReadExisting();
                    if (config.Length > 0)
                    {
                        try
                        {
                            configs            = config.Split(';');
                            textBoxName.Text   = configs[0];
                            textBoxlrv.Text    = configs[1];
                            textBoxurv.Text    = configs[2];
                            textBoxAlarml.Text = configs[3];
                            textBoxAlarmh.Text = configs[4];


                            timerReadSerial.Enabled = false;
                        }
                        catch (IndexOutOfRangeException)
                        {
                            MessageBox.Show("Something went wrong when reading the configuration. Please try again. If the issue continues, make sure the device is connected and operating.", "Reading error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            timerReadSerial.Enabled = false;
                        }
                    }
                }

                else if (command == "writeconf")
                {
Write:
                    string success;
                    success = serialPortSSC.ReadExisting();
                    if (success.Length > 0)
                    {
                        if (success == "1")
                        {
                            MessageBox.Show("New configuration accepted!", "Accepted", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            configs[0] = textBoxName.Text;
                            configs[1] = textBoxlrv.Text;
                            configs[2] = textBoxurv.Text;
                            configs[3] = textBoxAlarml.Text;
                            configs[4] = textBoxAlarmh.Text;
                        }
                        else
                        {
                            DialogResult result = MessageBox.Show("New configuration denied! Make sure the password is correct. Try again?", "Write error", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);

                            if (result == DialogResult.Yes)
                            {
                                timerReadSerial.Enabled = false;
                                FormPrompt writeConfPrompt = new FormPrompt();
                                writeConfPrompt.ShowDialog();
                                string promptString = writeConfPrompt.promptText;

                                string[] tempConfs = new string[5];
                                tempConfs[0] = textBoxName.Text;
                                tempConfs[1] = textBoxlrv.Text;
                                tempConfs[2] = textBoxurv.Text;
                                tempConfs[3] = textBoxAlarml.Text;
                                tempConfs[4] = textBoxAlarmh.Text;

                                string confWriteString = command + ">" + promptString + ">" + string.Join(";", tempConfs);
                                serialPortSSC.WriteLine(confWriteString);
                                timerReadSerial.Enabled = true;
                                goto Write;
                            }
                            else
                            {
                                textBoxName.Text   = configs[0];
                                textBoxlrv.Text    = configs[1];
                                textBoxurv.Text    = configs[2];
                                textBoxAlarml.Text = configs[3];
                                textBoxAlarmh.Text = configs[4];
                            }
                        }
                        timerReadSerial.Enabled = false;
                    }
                }
                else if (command == "readraw")
                {
                    string inputString = serialPortSSC.ReadExisting();
                    if (inputString.Length > 0)
                    {
                        double input       = double.Parse(inputString);
                        string urv         = string.Join(",", configs[2].Split('.'));
                        double scaledInput = double.Parse(urv) / 1023.0 * input;
                        chartSensorData.Series[1].Points.Add(scaledInput);
                        chartSensorData.Series[0].Points.Add(input);

                        if (buttonRawScaled.Text == "Scaled")
                        {
                            listBoxValues.Items.Add(Math.Round(scaledInput, 2));
                        }
                        else if (buttonRawScaled.Text == "Raw")
                        {
                            listBoxValues.Items.Add(input);
                        }

                        timerReadSerial.Enabled = false;
                        timerChart.Enabled      = true;
                    }
                }
            }
            else
            {
                MessageBox.Show("Connect to a device to use this function", "Connection needed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }