示例#1
0
        private void btnWriteSetting_Click(object sender, EventArgs e)
        {
            lblStatus.Text = "Writing";
            lblStatus.Refresh();
            getDeviceInfo.getActiveDevice();
            if (getDeviceInfo.activeDeviceListAl.Count == 0)
            {
                MessageBox.Show("Troi oi! Chua cam logger kia thim 2!");
                return;
            }
            device35.hostport = getDeviceInfo.activeDeviceListAl[0].ToString();

            if (cbbStartDelay.Text.Length == 0)
            {
                MessageBox.Show("Please set delay time to start recording");
                return;
            }

            if (cbbDuration.Text.Length == 0)
            {
                MessageBox.Show("Please set record time");
                return;
            }

            cbbChannel_SelectedIndexChanged(null, null);
            int    dldura  = Convert.ToInt32(cbbDuration.Text);
            string StrDevs = device35.hostport;

            if (string.IsNullOrEmpty(StrDevs))
            {
                MessageBox.Show("No Device");
                return;
            }

            if (txtDescription.Text.Length > 40 && chkDes.Checked == true)
            {
                MessageBox.Show("Description can not be over 40 characters");
                return;
            }
            else if (txtDescription.Text.Length == 0 && chkDes.Checked == true)
            {
                MessageBox.Show("No Description Input");
                return;
            }

            if (txtLocation.Text.Length > 40 && chkLoc.Checked)
            {
                MessageBox.Show("Location can not be over 40 characters");
                return;
            }
            else if (txtLocation.Text.Length == 0 && chkLoc.Checked)
            {
                MessageBox.Show("No Location Input");
                return;
            }

            if (txtSerial.Text.Length == 0)
            {
                MessageBox.Show("Serial can't be empty");
                return;
            }

            for (int i = 0; i < 4; i++)
            {
                if (!device35.Channels[i].NoAlarm)
                {
                    if (device35.Channels[i].AlarmMax <= device35.Channels[i].AlarmMin)
                    {
                        MessageBox.Show("Max alarm of channel " + (i + 1) + " must higher than min alarm of channel " + (i + 1));
                        return;
                    }
                }
            }

            char[] characters = txtDescription.Text.ToCharArray();
            for (int j = 0; j < characters.Length; j++)
            {
                if (Encoding.UTF8.GetByteCount(characters[j].ToString()) == characters[j].ToString().Length)
                {
                    continue;
                }
                else
                {
                    MessageBox.Show("Description must be typed in English");
                    return;
                }
            }

            lblStatus.Visible = true;
            lblStatus.Text    = "Writing....Please wait";
            lblStatus.Update();
            //lockButton(true);

            if (device35.USBOpen(StrDevs) == false)
            {
                lblStatus.Visible = false;
                MessageBox.Show("Open USB fail");
                lockButton(false);
                device35.Close();
                return;
            }

            if (device35.readSettingDevice() == false)
            {
                device35.Close();
            }

            if (device35.byteLogging == 0x44)
            {
                MessageBox.Show("Logger is recording. Cannot write setting");
                lblStatus.Text = ".....";
                device35.Close();
                return;
            }


            //---------------write Location, Description, Serial (Serial not be updated by hardware)
            if (chkLoc.Checked)
            {
                device35.Location = txtLocation.Text;
                if (device35.writeLocation() == false)
                {
                    lblStatus.Visible = false;
                    MessageBox.Show("Write Setting fail");
                    rtxtEventLog.Text += "Write Setting fail" + Environment.NewLine;
                    lockButton(false);
                    device35.Close();
                    return;
                }
                else
                {
                    rtxtEventLog.Text += "Write Location successfully" + Environment.NewLine;
                }
            }

            //------write Description
            if (chkDes.Checked)
            {
                device35.Description = txtDescription.Text;
                Thread.Sleep(100);
                if (device35.writeDescription() == false)
                {
                    lblStatus.Visible = false;
                    MessageBox.Show("Write Setting fail");
                    rtxtEventLog.Text += "Write Setting fail" + Environment.NewLine;
                    lockButton(false);
                    device35.Close();
                    return;
                }
                else
                {
                    rtxtEventLog.Text += "Write Description successfully" + Environment.NewLine;
                }
            }

            //-----write Serial
            Thread.Sleep(100);
            device35.Serial = txtSerial.Text;
            if (!device35.writeSerial())
            {
                lblStatus.Visible = false;
                MessageBox.Show("Write Setting fail");
                lockButton(false);
                device35.Close();
                return;
            }


            //-------------------write Setting (Time, TimeZone, Delay, Duration).
            if (chkRecord.Checked)
            {
                //Thread.Sleep(500);
                string TimeZone = null;
                try
                {
                    TimeZone = mGlobal.FindSystemTimeZoneFromDisplayName(cbbTimeZone.Text).Id.ToString();
                    //get 15 first char and 3 last char
                    TimeZone = TimeZone.Substring(0, 15) + TimeZone.Substring(Math.Max(TimeZone.Length - 2, 1) - 1).Substring(TimeZone.Substring(Math.Max(TimeZone.Length - 2, 1) - 1).Length - 3, 3);
                }
                catch (Exception)
                {
                    TimeZone = "local";
                }

                device35.Duration = int.Parse(cbbDuration.Text);
                device35.Startrec = int.Parse(cbbStartDelay.Text);
                Thread.Sleep(100);
                if (device35.writeSettingDevice(LoggerTime, TimeZone) == false)
                {
                    lblStatus.Visible = false;
                    MessageBox.Show("Write setting fail");
                    rtxtEventLog.Text += "Write setting fail" + Environment.NewLine;
                    lockButton(false);
                    device35.Close();
                    return;
                }
                else
                {
                    rtxtEventLog.Text += "Write setting successfully" + Environment.NewLine;
                }
            }

            //-----------------write setting Channel (Unit, sensor, description, alarm)
            if (chkChannel.Checked)
            {
                for (int i = 0; i < 4; i++)
                {
                    if (device35.Channels[i].Sensor == 255)
                    {
                        device35.Channels[i].Sensor = 0;
                    }
                }

                Thread.Sleep(100);
                if (device35.writeSettingChannel() == false)
                {
                    lblStatus.Visible = false;
                    MessageBox.Show("Write setting Channel fail");
                    rtxtEventLog.Text += "Write setting Channel fail" + Environment.NewLine;
                    lockButton(false);
                    device35.Close();
                    return;
                }
                else
                {
                    rtxtEventLog.Text += "Write setting Channel successfully" + Environment.NewLine;
                }
            }

            saveSetting();
            lblStatus.Text     = "....";
            rtxtEventLog.Text += "Writing to " + device35.Serial + "has done!" + Environment.NewLine;

            string tampSerial = (Int32.Parse(device35.Serial.Substring(6, device35.Serial.Length - 6)) + 1).ToString();
            string seri       = "";

            for (int i = 0; i < (4 - tampSerial.Length); i++)
            {
                seri += "0";
            }
            seri           = device35.Serial.Substring(0, 6) + seri + tampSerial;
            txtSerial.Text = seri;

            device35.Close();
        }