コード例 #1
0
        public void UpdateUIData(ModbusRegisters reg)
        {
            for (int i = 0; i < lRegTextBox.Count; i++)
            {
                lRegTextBox[i].UpdateData();
            }

            byte[] bytes = new byte[4];
            if (comboBoxFormat.Text == "AB CD")
            {
                bytes[0] = (byte)modbusRegs.stReg[1].getLowReg();
                bytes[1] = (byte)modbusRegs.stReg[1].getHighReg();
                bytes[2] = (byte)modbusRegs.stReg[0].getLowReg();
                bytes[3] = (byte)modbusRegs.stReg[0].getHighReg();
            }
            else if (comboBoxFormat.Text == "CD AB")
            {
                bytes[0] = (byte)modbusRegs.stReg[0].getLowReg();
                bytes[1] = (byte)modbusRegs.stReg[0].getHighReg();
                bytes[2] = (byte)modbusRegs.stReg[1].getLowReg();
                bytes[3] = (byte)modbusRegs.stReg[1].getHighReg();
            }
            else if (comboBoxFormat.Text == "BA DC")
            {
                bytes[0] = (byte)modbusRegs.stReg[1].getHighReg();
                bytes[1] = (byte)modbusRegs.stReg[1].getLowReg();
                bytes[2] = (byte)modbusRegs.stReg[0].getHighReg();
                bytes[3] = (byte)modbusRegs.stReg[0].getLowReg();
            }
            else if (comboBoxFormat.Text == "DC BA")
            {
                bytes[0] = (byte)modbusRegs.stReg[0].getHighReg();
                bytes[1] = (byte)modbusRegs.stReg[0].getLowReg();
                bytes[2] = (byte)modbusRegs.stReg[1].getHighReg();
                bytes[3] = (byte)modbusRegs.stReg[1].getLowReg();
            }
            else
            {
                return;
            }
            float q = BitConverter.ToSingle(bytes, 0);

            if (q > 0)
            {
                SaveFile();
            }

            regTextBoxResult.Text = "0";
            if (input_method == INPUT_METHOD_SERIAL)
            {
                inputCommPortSingleton.GetInstance().writeMultiRegisters(modbusRegs);
            }
            else
            {
                modbusNetworkSingleton.GetInstance().writeMultiRegisters(modbusRegs);
            }
        }
コード例 #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);
        }
コード例 #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);
        }
コード例 #4
0
        string[] getString(ModbusRegisters asciiModbusRegs, int blocklen)
        {
            int numstr = asciiModbusRegs.numRegisters * 2 / blocklen;

            string[] str = new string[numstr];
            for (int i = 0; i < numstr; i++)
            {
                byte[] tmpstr = new byte[blocklen];
                Buffer.BlockCopy(asciiModbusRegs.values, i * blocklen, tmpstr, 0, blocklen);
                str[i] = System.Text.Encoding.UTF8.GetString(tmpstr);
            }

            return(str);
        }
コード例 #5
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);
        }
コード例 #6
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);
         }
     }
 }
コード例 #7
0
        public void setReg(ref ModbusRegisters reg)
        {
            this.reg = reg;

            switch (dataType)
            {
            case DataType.dtString:
            case DataType.dtLString:
            case DataType.dtHString:
                for (int i = 0; i < items.Count; i++)
                {
                    this.reg.stReg[_regIndex].addStringMap(items[i].index, items[i].mapstring);
                }

                break;

            default:
                break;
            }
        }
コード例 #8
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);
            //     }
            //  }
        }
コード例 #9
0
        private void button1_Click(object sender, EventArgs e)
        {
            string[] str = new string[3];
            str[0] = "仪器名称";
            str[1] = "仪器编号";
            str[2] = "实验室名称";
            byte[] b = getAsciiData(str);

            ushort startAddress = 6898;

            ushort[] data = new ushort[b.Length / sizeof(short)];
            Buffer.BlockCopy(b, 0, data, 0, data.Length * sizeof(short));

            ModbusRegisters asciiModbusRegs     = new ModbusRegisters(1, startAddress, (ushort)data.Length);
            ModbusRegisters asciiModbusRegsRead = new ModbusRegisters(1, startAddress, (ushort)data.Length);

            for (int i = 0; i < asciiModbusRegs.numRegisters; i++)
            {
                asciiModbusRegs.stReg[i].value = data[i];
            }
            if (radioButtonSerial.Checked)
            {
                int ret = inputCommPortSingleton.GetInstance().writeMultiRegisters(asciiModbusRegs);
            }
            else
            {
                //modbusNetworkSingleton.GetInstance().writeMultiRegisters(modbusRegs);
                int ret = modbusNetworkSingleton.GetInstance().writeMultiRegisters(asciiModbusRegs);
            }

            modbusNetworkSingleton.GetInstance().readRegister(ref asciiModbusRegsRead);
            string[] stt = getString(asciiModbusRegsRead, 16);
            textBoxDevName.Text = stt[0];
            textBoxDevID.Text   = stt[1];
            textBoxLabName.Text = stt[2];
        }