Ejemplo n.º 1
0
        public bool openComm()
        {
            //LogClass.GetInstance().WriteLogFile("OpenComm Called " + port.PortName);
            lock (locker)
            {
                if (port != null && !port.IsOpen)
                {
                    try
                    {
                        port.Open();
                        //LogClass.GetInstance().WriteLogFile("port opened");
                        // create modbus master
                        if (InputModbusType == "RTU")
                        {
                            master = ModbusSerialMaster.CreateRtu(port);
                        }
                        else
                        {
                            master = ModbusSerialMaster.CreateAscii(port);
                        }

                        return(true);
                    }
                    catch (Exception ex)
                    {
                        LogClass.GetInstance().WriteExceptionLog(ex);
                        //MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return(false);
                    }
                }
            }

            return(true);
        }
Ejemplo n.º 2
0
        public int readRegister(ref ModbusRegisters regs)
        {
            if (master == null)
            {
                commsts = COMMSTS_UNKONOWN;
                return(RET_INITFAILURE);
            }

            lock (locker)
            {
                if (tcpClient == null)
                {
                    commsts = COMMSTS_PORTNOTOPEN;
                    return(RET_INITFAILURE);
                }

                try
                {
                    regs.values = master.ReadHoldingRegisters(regs.slaveid, regs.startAddress, regs.numRegisters);

                    for (int i = 0; i < regs.numRegisters; i++)
                    {
                        regs.stReg[i].value = regs.values[i];
                    }
                }
                catch (TimeoutException ex)
                {
                    //LogClass.GetInstance().WriteLogFile("ReadHoldingRegisters Timeout:" + port.ReadTimeout.ToString());
                    //MessageBox.Show("Serial Port Read Timeout:" + port.ReadTimeout.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    if (rtycnt++ < MAX_RETYR_COUNT)
                    {
                        return(RET_TIMEOUT);
                    }
                    else
                    {
                        commsts = COMMSTS_FAILURE;
                        return(RET_COMMERROR);
                    }
                }
                catch (Exception ex)
                {
                    LogClass.GetInstance().WriteExceptionLog(ex);
                    //MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    commsts = COMMSTS_FAILURE;
                    return(RET_FAILURE);
                }
            }
            rtycnt  = 0;
            commsts = COMMSTS_NORMAL;
            return(RET_OK);
        }
Ejemplo n.º 3
0
        public int writeMultiRegisters(ModbusRegisters regs)
        {
            if (master == null)
            {
                commsts = COMMSTS_UNKONOWN;
                return(RET_INITFAILURE);
            }

            if (port.IsOpen == false)
            {
                commsts = COMMSTS_PORTNOTOPEN;
                return(RET_INITFAILURE);
            }

            lock (locker)
            {
                try
                {
                    for (int i = 0; i < regs.numRegisters; i++)
                    {
                        regs.values[i] = regs.stReg[i].value;
                    }

                    master.WriteMultipleRegisters(regs.slaveid, regs.startAddress, regs.values);
                }
                catch (TimeoutException ex)
                {
                    //LogClass.GetInstance().WriteLogFile("WriteMultipleRegisters Timeout:" + port.WriteTimeout.ToString());
                    //MessageBox.Show("Serial Port Write Timeout:" + port.WriteTimeout.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    if (rtycnt++ < MAX_RETYR_COUNT)
                    {
                        return(RET_TIMEOUT);
                    }
                    else
                    {
                        commsts = COMMSTS_FAILURE;
                        return(RET_COMMERROR);
                    }
                }
                catch (Exception ex)
                {
                    LogClass.GetInstance().WriteExceptionLog(ex);
                    //MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    commsts = COMMSTS_FAILURE;
                    return(RET_FAILURE);
                }
            }
            rtycnt  = 0;
            commsts = COMMSTS_NORMAL;
            return(RET_OK);
        }
Ejemplo n.º 4
0
        public int writeSingleRegister(ModbusRegisters regs, ushort value)
        {
            try
            {
                if (master == null)
                {
                    commsts = COMMSTS_UNKONOWN;
                    return(RET_INITFAILURE);
                }

                if (tcpClient == null)
                {
                    commsts = COMMSTS_PORTNOTOPEN;
                    return(RET_INITFAILURE);
                }

                lock (locker)
                {
                    master.WriteSingleRegister(regs.slaveid, regs.startAddress, value);
                }
            }
            catch (TimeoutException ex)
            {
                //LogClass.GetInstance().WriteLogFile("writeSingleRegister Timeout:" + port.WriteTimeout.ToString());
                //MessageBox.Show("Serial Port Write Timeout:" + port.WriteTimeout.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                if (rtycnt++ < MAX_RETYR_COUNT)
                {
                    return(RET_TIMEOUT);
                }
                else
                {
                    commsts = COMMSTS_FAILURE;
                    return(RET_COMMERROR);
                }
            }
            catch (Exception ex)
            {
                LogClass.GetInstance().WriteExceptionLog(ex);
                //MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                commsts = COMMSTS_FAILURE;
                return(RET_FAILURE);
            }

            rtycnt  = 0;
            commsts = COMMSTS_NORMAL;
            return(RET_OK);
        }
Ejemplo n.º 5
0
        private float getFloatValue()
        {
            float value;

            try
            {
                value = float.Parse(this.Text);
            }
            catch (Exception ex)
            {
                LogClass.GetInstance().WriteExceptionLog(ex);
                this.Text = "";
                value     = 0.0f;
            }

            return(value);
        }
Ejemplo n.º 6
0
        private int getIntValue()
        {
            int value;

            try
            {
                if (this._dataType == DataType.dtFloat)
                {
                    value = (int)(float.Parse(this.Text) * this._dtDivValue);
                }
                else
                {
                    value = int.Parse(this.Text);
                }

                getMinMax();

                if (value < this._dtMinValue)
                {
                    this.Text = this._dtMinValue.ToString();
                    value     = this._dtMinValue;
                }
                else if (value > this._dtMaxValue)
                {
                    this.Text = this._dtMaxValue.ToString();
                    value     = this._dtMaxValue;
                }
            }
            catch (Exception ex)
            {
                LogClass.GetInstance().WriteExceptionLog(ex);
                this.Text = this._dtMinValue.ToString();
                value     = this._dtMinValue;
            }

            return(value);
        }
Ejemplo n.º 7
0
        public bool createXmlFile(string filename)
        {
            try
            {
                Configure cfg     = null;
                string    cfgfile = System.IO.Path.Combine(Application.StartupPath, "cfg.json");
                if (File.Exists(cfgfile))
                {
                    cfg = JsonConvert.DeserializeObject <Configure>(File.ReadAllText(cfgfile));
                    if (cfg != null)
                    {
                        LimsDoc l;
                        l = new LimsDoc(cfg.username, cfg.userpassword, "system");

                        LimsDocEntity entity       = l.createEntity("SAMPLE", "RESULT_ENTRY");
                        LimsDocEntity entity2      = l.createEntity("TEST", null);
                        LimsDocEntity entity1_time = l.createEntity("RESULT", null);
                        LimsDocEntity entity1_res  = l.createEntity("RESULT", null);
                        LimsDocEntity entity2_time = l.createEntity("RESULT", null);
                        LimsDocEntity entity2_res  = l.createEntity("RESULT", null);
                        LimsDocEntity entity3_time = l.createEntity("RESULT", null);
                        LimsDocEntity entity3_res  = l.createEntity("RESULT", null);
                        LimsDocEntity entity4_time = l.createEntity("RESULT", null);
                        LimsDocEntity entity4_res  = l.createEntity("RESULT", null);

                        entity2.addFields("ANALYSIS", "in", textBoxAnalysis.Text);

                        entity.addFields("ID_NUMERIC", "in", textBoxOilID.Text);

                        if (lcfg == null)
                        {
                            entity1_time.addFields("NAME", "in", "一号弹测试时间");
                            entity1_time.addFields("TEXT", "in", regTextBox1.Text);
                            entity2.addChild(entity1_time.getElement());

                            entity1_res.addFields("NAME", "in", "一号弹最大压力");
                            entity1_res.addFields("TEXT", "in", regTextBox2.Text);
                            entity2.addChild(entity1_res.getElement());

                            entity2_time.addFields("NAME", "in", "二号弹测试时间");
                            entity2_time.addFields("TEXT", "in", regTextBox3.Text);
                            entity2.addChild(entity2_time.getElement());

                            entity2_res.addFields("NAME", "in", "二号弹最大压力");
                            entity2_res.addFields("TEXT", "in", regTextBox4.Text);
                            entity2.addChild(entity2_res.getElement());

                            entity3_time.addFields("NAME", "in", "三号弹测试时间");
                            entity3_time.addFields("TEXT", "in", regTextBox5.Text);
                            entity2.addChild(entity3_time.getElement());

                            entity3_res.addFields("NAME", "in", "三号弹最大压力");
                            entity3_res.addFields("TEXT", "in", regTextBox6.Text);
                            entity2.addChild(entity3_res.getElement());

                            entity4_time.addFields("NAME", "in", "四号弹测试时间");
                            entity4_time.addFields("TEXT", "in", regTextBox7.Text);
                            entity2.addChild(entity4_time.getElement());

                            entity4_res.addFields("NAME", "in", "四号弹最大压力");
                            entity4_res.addFields("TEXT", "in", regTextBox8.Text);
                            entity2.addChild(entity4_res.getElement());
                        }
                        else
                        {
                            entity1_time.addFields("NAME", "in", lcfg.label_1_out);
                            entity1_time.addFields("TEXT", "in", regTextBox1.Text);
                            entity2.addChild(entity1_time.getElement());

                            entity1_res.addFields("NAME", "in", lcfg.label_2_out);
                            entity1_res.addFields("TEXT", "in", regTextBox2.Text);
                            entity2.addChild(entity1_res.getElement());

                            entity2_time.addFields("NAME", "in", lcfg.label_3_out);
                            entity2_time.addFields("TEXT", "in", regTextBox3.Text);
                            entity2.addChild(entity2_time.getElement());

                            entity2_res.addFields("NAME", "in", lcfg.label_4_out);
                            entity2_res.addFields("TEXT", "in", regTextBox4.Text);
                            entity2.addChild(entity2_res.getElement());

                            entity3_time.addFields("NAME", "in", lcfg.label_5_out);
                            entity3_time.addFields("TEXT", "in", regTextBox5.Text);
                            entity2.addChild(entity3_time.getElement());

                            entity3_res.addFields("NAME", "in", lcfg.label_6_out);
                            entity3_res.addFields("TEXT", "in", regTextBox6.Text);
                            entity2.addChild(entity3_res.getElement());

                            entity4_time.addFields("NAME", "in", lcfg.label_7_out);
                            entity4_time.addFields("TEXT", "in", regTextBox7.Text);
                            entity2.addChild(entity4_time.getElement());

                            entity4_res.addFields("NAME", "in", lcfg.label_8_out);
                            entity4_res.addFields("TEXT", "in", regTextBox8.Text);
                            entity2.addChild(entity4_res.getElement());
                        }

                        entity.addChild(entity2.getElement());

                        l.getBody().addEntity(entity.getElement());

                        return(l.createdoc(filename));
                    }
                }
            }
            catch (Exception ex)
            {
                LogClass.GetInstance().WriteExceptionLog(ex);
                //MessageBox.Show(ex.ToString(), "Error - No Ports available", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            return(false);
        }
Ejemplo n.º 8
0
        private void CreateExcelFile(string FileName)
        {
            //create
            try
            {
                NPOI.HSSF.UserModel.HSSFWorkbook book  = new NPOI.HSSF.UserModel.HSSFWorkbook();
                NPOI.SS.UserModel.ISheet         sheet = book.CreateSheet("Result");
                NPOI.SS.UserModel.IRow           row0  = sheet.CreateRow(0);
                int i = 0;
                if (lcfg == null)
                {
                    row0.CreateCell(i++).SetCellValue("日期:");
                    row0.CreateCell(i++).SetCellValue("仪器名称:");
                    row0.CreateCell(i++).SetCellValue("仪器编号:");
                    row0.CreateCell(i++).SetCellValue("实验室名称:");
                    row0.CreateCell(i++).SetCellValue("油样名称:");
                    row0.CreateCell(i++).SetCellValue("油样号:");
                    row0.CreateCell(i++).SetCellValue("实验员:");
                    row0.CreateCell(i++).SetCellValue("分析方法:");
                    row0.CreateCell(i++).SetCellValue("一号弹:测试时间:");
                    row0.CreateCell(i++).SetCellValue("最大压力:");
                    row0.CreateCell(i++).SetCellValue("二号弹:测试时间:");
                    row0.CreateCell(i++).SetCellValue("最大压力:");
                    row0.CreateCell(i++).SetCellValue("三号弹:测试时间:");
                    row0.CreateCell(i++).SetCellValue("最大压力:");
                    row0.CreateCell(i++).SetCellValue("四号弹:测试时间:");
                    row0.CreateCell(i++).SetCellValue("最大压力:");
                }
                else
                {
                    row0.CreateCell(i++).SetCellValue("日期:");
                    row0.CreateCell(i++).SetCellValue("仪器名称:");
                    row0.CreateCell(i++).SetCellValue("仪器编号:");
                    row0.CreateCell(i++).SetCellValue("实验室名称:");
                    row0.CreateCell(i++).SetCellValue("油样名称:");
                    row0.CreateCell(i++).SetCellValue("油样号:");
                    row0.CreateCell(i++).SetCellValue("实验员:");
                    row0.CreateCell(i++).SetCellValue("分析方法:");
                    row0.CreateCell(i++).SetCellValue(lcfg.label_1_out);
                    row0.CreateCell(i++).SetCellValue(lcfg.label_2_out);
                    row0.CreateCell(i++).SetCellValue(lcfg.label_3_out);
                    row0.CreateCell(i++).SetCellValue(lcfg.label_4_out);
                    row0.CreateCell(i++).SetCellValue(lcfg.label_5_out);
                    row0.CreateCell(i++).SetCellValue(lcfg.label_6_out);
                    row0.CreateCell(i++).SetCellValue(lcfg.label_7_out);
                    row0.CreateCell(i++).SetCellValue(lcfg.label_8_out);
                }

                NPOI.SS.UserModel.IRow row1 = sheet.CreateRow(1);
                i = 0;
                row1.CreateCell(i++).SetCellValue(DateTime.Now.ToString());
                row1.CreateCell(i++).SetCellValue(textBoxDevName.Text);
                row1.CreateCell(i++).SetCellValue(textBoxDevID.Text);
                row1.CreateCell(i++).SetCellValue(textBoxLabName.Text);
                row1.CreateCell(i++).SetCellValue(textBoxOilName.Text);
                row1.CreateCell(i++).SetCellValue(textBoxOilID.Text);
                row1.CreateCell(i++).SetCellValue(textBoxUserName.Text);
                row1.CreateCell(i++).SetCellValue(textBoxAnalysis.Text);
                row1.CreateCell(i++).SetCellValue(regTextBox1.Text);
                row1.CreateCell(i++).SetCellValue(regTextBox2.Text);
                row1.CreateCell(i++).SetCellValue(regTextBox3.Text);
                row1.CreateCell(i++).SetCellValue(regTextBox4.Text);
                row1.CreateCell(i++).SetCellValue(regTextBox5.Text);
                row1.CreateCell(i++).SetCellValue(regTextBox6.Text);
                row1.CreateCell(i++).SetCellValue(regTextBox7.Text);
                row1.CreateCell(i++).SetCellValue(regTextBox8.Text);

                for (int j = 0; j < i; j++)
                {
                    sheet.AutoSizeColumn(j);
                }

                //worksheet.Cells[1, 1] = "仪器名称:";
                //worksheet.Cells[2, 1] = "仪器编号:";
                //worksheet.Cells[3, 1] = "实验室名称:";
                //worksheet.Cells[4, 1] = "油样名称:";
                //worksheet.Cells[5, 1] = "油样号:";
                //worksheet.Cells[6, 1] = "实验员:";
                //worksheet.Cells[7, 1] = "分析方法:";
                //worksheet.Cells[8, 1] = "日期:";

                //worksheet.Cells[1, 2] = textBoxDevName.Text;
                //worksheet.Cells[2, 2] = textBoxDevID.Text;
                //worksheet.Cells[3, 2] = textBoxLabName.Text;
                //worksheet.Cells[4, 2] = textBoxOilName.Text;
                //worksheet.Cells[5, 2] = textBoxOilID.Text;
                //worksheet.Cells[6, 2] = textBoxUserName.Text;
                //worksheet.Cells[7, 2] = textBoxAnalysis.Text;
                //worksheet.Cells[8, 2] = DateTime.Now.ToString();

                //worksheet.Cells[9, 1] = "一号弹:";
                //worksheet.Cells[10, 1] = "二号弹:";
                //worksheet.Cells[11, 1] = "三号弹:";
                //worksheet.Cells[12, 1] = "四号弹:";

                //worksheet.Cells[9, 2] = "测试时间:";
                //worksheet.Cells[10, 2] = "测试时间:";
                //worksheet.Cells[11, 2] = "测试时间:";
                //worksheet.Cells[12, 2] = "测试时间:";

                //worksheet.Cells[9, 3] = regTextBox1.Text;
                //worksheet.Cells[10, 3] = regTextBox3.Text;
                //worksheet.Cells[11, 3] = regTextBox5.Text;
                //worksheet.Cells[12, 3] = regTextBox7.Text;

                //worksheet.Cells[9, 4] = "最大压力:";
                //worksheet.Cells[10, 4] = "最大压力:";
                //worksheet.Cells[11, 4] = "最大压力:";
                //worksheet.Cells[12, 4] = "最大压力:";


                //worksheet.Cells[9, 5] = regTextBox2.Text;
                //worksheet.Cells[10, 5] = regTextBox4.Text;
                //worksheet.Cells[11, 5] = regTextBox6.Text;
                //worksheet.Cells[12, 5] = regTextBox8.Text;

                //((Excel.Range)worksheet.Columns["A:E", System.Type.Missing]).AutoFit();
                //worksheet.SaveAs(FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing);
                //workBook.Close(false, Type.Missing, Type.Missing);
                //app.Quit();

                using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
                {
                    book.Write(ms);
                    using (FileStream fs = new FileStream(FileName, FileMode.Create, FileAccess.Write))
                    {
                        byte[] data = ms.ToArray();
                        fs.Write(data, 0, data.Length);
                        fs.Flush();
                    }
                    book = null;
                }
            }
            catch (Exception ex)
            {
                LogClass.GetInstance().WriteExceptionLog(ex);
                //MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Ejemplo n.º 9
0
        public void DoUpdateRegs()
        {
            while (m_updateDataFlg)
            {
                try
                {
                    if (input_method == INPUT_METHOD_SERIAL)
                    {
                        int ret = inputCommPortSingleton.GetInstance().readRegister(ref modbusRegs);
                        if (ret != inputCommPortSingleton.RET_OK)
                        {
                            if (ret == inputCommPortSingleton.RET_TIMEOUT)
                            {
                                continue;
                            }
                            else
                            {
                                // set label communication error
                            }
                            break;
                        }
                    }
                    else
                    {
                        //modbusNetworkSingleton.GetInstance().writeMultiRegisters(modbusRegs);
                        int ret = modbusNetworkSingleton.GetInstance().readRegister(ref modbusRegs);
                        if (ret != modbusNetworkSingleton.RET_OK)
                        {
                            if (ret == modbusNetworkSingleton.RET_TIMEOUT)
                            {
                                continue;
                            }
                            else
                            {
                                // set label communication error
                            }
                            break;
                        }
                    }
                    UpdateMainUIInvoke umi = new UpdateMainUIInvoke(UpdateUIData);

                    while (!this.IsHandleCreated)
                    {
                        Thread.Sleep(100);
                    }
                    BeginInvoke(umi, modbusRegs);

                    Thread.Sleep(1000);
                }
                catch (Exception ex)
                {
                    LogClass.GetInstance().WriteExceptionLog(ex);
                    //MessageBox.Show(ex.ToString(), "Error - No Ports available", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    break;
                }
            }

            while (m_updateDataFlg)
            {
                try
                {
                    Thread.Sleep(1000);
                }
                catch (Exception ex)
                {
                    LogClass.GetInstance().WriteExceptionLog(ex);
                }
            }
            if (input_method == INPUT_METHOD_SERIAL)
            {
                inputCommPortSingleton.GetInstance().closeComm();
            }
            else
            {
                modbusNetworkSingleton.GetInstance().closeComm();
            }
        }
Ejemplo n.º 10
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);
            }
        }
Ejemplo n.º 11
0
        private void InitializeSystemSetting()
        {
            try
            {
                load_labcfg();
                Configure cfg     = null;
                string    cfgfile = System.IO.Path.Combine(Application.StartupPath, "cfg.json");
                if (File.Exists(cfgfile))
                {
                    cfg = JsonConvert.DeserializeObject <Configure>(File.ReadAllText(cfgfile));
                }

                string[] portList = System.IO.Ports.SerialPort.GetPortNames();

                for (int i = 0; i < portList.Length; ++i)
                {
                    string name = portList[i];
                    comboBoxSerialPort.Items.Add(name);
                    if (cfg != null && name.ToLower() == cfg.InputSerialPortName.ToLower())
                    {
                        comboBoxSerialPort.SelectedIndex = i;
                    }
                }

                if (cfg == null)
                {
                    return;
                }

                if (cfg.bSerial_sel)
                {
                    radioButtonSerial.Checked = true;
                    input_method = INPUT_METHOD_SERIAL;
                }
                else if (cfg.bNetwork_sel)
                {
                    radioButtonNetwork.Checked = true;
                    input_method = INPUT_METHOD_IP;
                }
                else
                {
                    radioButtonSerial.Checked = true;
                    input_method = INPUT_METHOD_SERIAL;
                }

                if (radioButtonSerial.Checked)
                {
                    comboBoxSerialPort.Enabled = true;
                    comboBoxBaud.Enabled       = true;
                    textBoxIPAddr.Enabled      = false;
                    textBoxPort.Enabled        = false;
                }
                else if (radioButtonNetwork.Checked)
                {
                    comboBoxSerialPort.Enabled = false;
                    comboBoxBaud.Enabled       = false;
                    textBoxIPAddr.Enabled      = true;
                    textBoxPort.Enabled        = true;
                }


                if (inputCommPortSingleton.GetInstance().checkSerialPort(cfg.InputSerialPortName))
                {
                    comboBoxSerialPort.Text = cfg.InputSerialPortName;
                }
                else
                {
                    comboBoxSerialPort.Text = "";
                }

                if (cfg.bOutputExcel)
                {
                    checkBoxExcel.Checked = true;
                }
                else
                {
                    checkBoxExcel.Checked = false;
                }

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

                if (comboBoxBaud.Items.Contains(cfg.InputSerialPortBaud))
                {
                    comboBoxBaud.Text = cfg.InputSerialPortBaud;
                }

                textBoxUserName.Text = cfg.username;
                textBoxPassword.Text = cfg.userpassword;
                textBoxAnalysis.Text = cfg.analysis;

                textBoxDevID.Text           = cfg.dev_id;
                textBoxDevName.Text         = cfg.dev_name;
                textBoxLabName.Text         = cfg.lab_name;
                textBoxOilID.Text           = cfg.oil_id;
                textBoxOilName.Text         = cfg.oil_name;
                maskedTextBoxStartAddr.Text = cfg.startAddr.ToString();
                comboBoxFormat.Text         = cfg.floatFormat;
                last_startaddr   = cfg.startAddr;
                last_floatformat = cfg.floatFormat;

                textBoxIPAddr.Text = cfg.ipaddr;
                textBoxPort.Text   = cfg.port;

                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);
            }
        }