Exemple #1
0
        void InitializeRegs()
        {
            lRegTextBox = new List <RegTextBox>();
            modbusRegs  = new ModbusRegisters(SLAVEID, last_startaddr, REGNUM);
            RegTextBox.FloatFMT fmt = RegTextBox.FloatFMT.FMT_DCBA;

            if (last_floatformat == "AB CD")
            {
                fmt = RegTextBox.FloatFMT.FMT_ABCD;
            }
            else if (last_floatformat == "BA DC")
            {
                fmt = RegTextBox.FloatFMT.FMT_BADC;
            }
            else if (last_floatformat == "CD AB")
            {
                fmt = RegTextBox.FloatFMT.FMT_CDAB;
            }
            else
            {
                fmt = fmt = RegTextBox.FloatFMT.FMT_DCBA;
            }
            addRegTextBoxControl(ref lRegTextBox, ref modbusRegs, this.Controls, fmt);

            updateRegAddrLabel(last_startaddr);
            //foreach (Control control in this.Controls)//遍历本窗体中所有的ComboBox控件
            //{
            //    if (control.GetType().ToString() == "System.Windows.Forms.GroupBox")
            //    {

            //     }
            //     string tmpstr = control.GetType().ToString();
            //     if (tmpstr == "PLCModbusSystem.RegTextBox")
            //     {
            //         (control as PLCModbusSystem.RegTextBox).setReg(ref modbusRegs);
            //(control as CommCtrlSystem.RegTextBox).KeyPress += new KeyPressEventHandler(new CheckUserInput().CheckIsNumber);
            //         lRegTextBox.Add(control as PLCModbusSystem.RegTextBox);
            //     }
            //  }
        }
Exemple #2
0
 void addRegTextBoxControl(ref List <RegTextBox> list, ref ModbusRegisters modbusRegs, Control.ControlCollection controls, RegTextBox.FloatFMT fmt)
 {
     foreach (Control control in controls)//遍历本窗体中所有的ComboBox控件
     {
         if (control.GetType().ToString() == "System.Windows.Forms.GroupBox")
         {
             addRegTextBoxControl(ref list, ref modbusRegs, (control as GroupBox).Controls, fmt);
         }
         else if (control.GetType().ToString() == "PLCModbusSystem.RegTextBox")
         {
             (control as PLCModbusSystem.RegTextBox).setReg(ref modbusRegs);
             (control as PLCModbusSystem.RegTextBox).floatFmt = fmt;
             //(control as CommCtrlSystem.RegTextBox).KeyPress += new KeyPressEventHandler(new CheckUserInput().CheckIsNumber);
             lRegTextBox.Add(control as PLCModbusSystem.RegTextBox);
         }
     }
 }
Exemple #3
0
        private void buttonSaveSettings_Click(object sender, EventArgs e)
        {
            try
            {
                Configure cfg = new Configure();
                cfg.InputSerialPortName = comboBoxSerialPort.Text;
                if (checkBoxExcel.Checked)
                {
                    cfg.bOutputExcel = true;
                }
                else
                {
                    cfg.bOutputExcel = false;
                }

                if (checkBoxLims.Checked)
                {
                    cfg.bOutputLims = true;
                }
                else
                {
                    cfg.bOutputLims = false;
                }

                cfg.InputSerialPortBaud    = comboBoxBaud.Text.ToString();
                cfg.InputSerialPortDataBit = "8 Data Bits";
                cfg.InputSerialPortParity  = "None Parity";
                cfg.InputSerialPortStopBit = "1 Stop Bit";;

                cfg.username     = textBoxUserName.Text.Trim();
                cfg.userpassword = textBoxPassword.Text.Trim();
                cfg.analysis     = textBoxAnalysis.Text.Trim();
                cfg.dev_id       = textBoxDevID.Text.Trim();
                cfg.dev_name     = textBoxDevName.Text.Trim();
                cfg.lab_name     = textBoxLabName.Text.Trim();
                cfg.oil_id       = textBoxOilID.Text.Trim();
                cfg.oil_name     = textBoxOilName.Text.Trim();

                cfg.startAddr   = ushort.Parse(maskedTextBoxStartAddr.Text.Trim());
                cfg.floatFormat = comboBoxFormat.Text.Trim();

                if (radioButtonSerial.Checked)
                {
                    cfg.bSerial_sel = true;
                }
                else
                {
                    cfg.bSerial_sel = false;
                }

                if (radioButtonNetwork.Checked)
                {
                    cfg.bNetwork_sel = true;
                }
                else
                {
                    cfg.bNetwork_sel = false;
                }

                cfg.protocol = comboBoxProtocol.Text.Trim();
                cfg.ipaddr   = textBoxIPAddr.Text.Trim();
                cfg.port     = textBoxPort.Text.Trim();
                string cfgfile = System.IO.Path.Combine(Application.StartupPath, "cfg.json");
                File.WriteAllText(cfgfile, JsonConvert.SerializeObject(cfg));

                //if (false == inputCommPortSingleton.GetInstance().initComm() || false == inputCommPortSingleton.GetInstance().openComm())
                //{
                //    labelWarning.Text = "串口初始化失败";
                //}
                //else
                //{
                stopUpdateRegs();
                if (last_startaddr != cfg.startAddr || last_floatformat != cfg.floatFormat)
                {
                    last_startaddr   = cfg.startAddr;
                    last_floatformat = cfg.floatFormat;

                    RegTextBox.FloatFMT fmt = RegTextBox.FloatFMT.FMT_DCBA;

                    if (last_floatformat == "AB CD")
                    {
                        fmt = RegTextBox.FloatFMT.FMT_ABCD;
                    }
                    else if (last_floatformat == "BA DC")
                    {
                        fmt = RegTextBox.FloatFMT.FMT_BADC;
                    }
                    else if (last_floatformat == "CD AB")
                    {
                        fmt = RegTextBox.FloatFMT.FMT_CDAB;
                    }
                    else
                    {
                        fmt = fmt = RegTextBox.FloatFMT.FMT_DCBA;
                    }

                    lRegTextBox.Clear();

                    modbusRegs.startAddress = last_startaddr;
                    addRegTextBoxControl(ref lRegTextBox, ref modbusRegs, this.Controls, fmt);
                    updateRegAddrLabel(last_startaddr);
                }

                if (input_method == INPUT_METHOD_SERIAL)
                {
                    if (false == inputCommPortSingleton.GetInstance().initComm() || false == inputCommPortSingleton.GetInstance().openComm())
                    {
                        labelWarning.Text = "串口初始化失败";
                    }
                    else
                    {
                        startUpdateRegs();
                    }
                }
                else
                {
                    if (false == modbusNetworkSingleton.GetInstance().initModbusPoll(cfg.ipaddr, int.Parse(cfg.port), cfg.protocol))
                    {
                        labelWarning.Text = "以太网初始化失败";
                    }
                    else
                    {
                        startUpdateRegs();
                    }
                }
                //}
            }
            catch (Exception ex)
            {
                LogClass.GetInstance().WriteExceptionLog(ex);
                //MessageBox.Show(ex.ToString(), "Error - No Ports available", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }