Пример #1
0
        public void ReadFromBSC(ushort ID)
        {
            //MBmaster.ReadHoldingRegister((ushort)0, (Byte)3, (ushort)40000, (Byte)3);
            //ushort ID = 1;
            byte unit = Convert.ToByte(0);

            ushort temp_startaddr = new ushort();
            ushort temp_length    = new ushort();

            if (ID == 1)
            {
                temp_startaddr = 0;
                temp_length    = 40;
            }
            else if (ID == 2)
            {
                temp_startaddr = 40;
                temp_length    = 51;
            }
            else if (ID == 3)
            {
                temp_startaddr = 100;
                temp_length    = 51;
            }


            ushort StartAddress = ReadStartAdr(temp_startaddr);
            byte   Length       = Convert.ToByte(temp_length);

            MBmaster.ReadInputRegister(ID, unit, StartAddress, Length);
        }
Пример #2
0
        // ------------------------------------------------------------------------
        // Button read holding register
        // ------------------------------------------------------------------------
        private void btnReadInpReg_Click(object sender, System.EventArgs e)
        {
            int  ID           = 4;
            int  StartAddress = ReadStartAdr();
            byte Length       = Convert.ToByte(txtSize.Text);

            MBmaster.ReadInputRegister(ID, StartAddress, Length);
        }
Пример #3
0
        // ------------------------------------------------------------------------
        // Button read holding register
        // ------------------------------------------------------------------------
        private void btnReadInpReg_Click(object sender, System.EventArgs e)
        {
            ushort ID           = 4;
            ushort StartAddress = ReadStartAdr();
            ushort Length       = Convert.ToUInt16(txtSize.Text);

            MBmaster.ReadInputRegister(ID, StartAddress, Length);
        }
Пример #4
0
        // ------------------------------------------------------------------------
        /// <summary>Sychnronous read analog inputs starting from a specified offset.</summary>
        /// <param name="module_nr">The position of the module.</param>
        /// <param name="offset">Starting offset for analog read command.</param>
        /// <param name="size">Number of words to read.</param>
        /// <returns>Result of the read command as integer arrary. UDINT and DINT values are returned as two integers.</returns>
        public int[] ReadAnalogInputs(byte module_nr, byte offset, byte size)
        {
            byte[] values = { };
            int[]  data   = new int[size];;

            if (ValidateData(module_nr, size))
            {
                if (MDinfo[module_nr].analog_in_index != 0xFFFF)
                {
                    if (size <= ana_in_buffer.Length - (MDinfo[module_nr].analog_in_index / 2 + offset))
                    {
                        // Request values from bus coupler
                        if (_refresh == 0)
                        {
                            MBmaster.ReadInputRegister(ID_value, 0, Convert.ToUInt16(0x0000 + MDinfo[module_nr].analog_in_index / 2 + offset), size, ref values);
                            if ((values == null) && (OnBCexception != null))
                            {
                                OnBCexception(excDataEmptyAnswer);
                            }
                            else
                            {
                                return(ByteArray2WordArray(values));
                            }
                        }
                        // ------------------------------------------------------------------------
                        // Request value from internal buffer
                        else
                        {
                            Array.Copy(ana_in_buffer, MDinfo[module_nr].analog_in_index / 2 + offset, data, 0, size);
                            return(data);
                        }
                    }
                    else if (OnBCexception != null)
                    {
                        OnBCexception(excDataSize);
                    }
                }
                else if (OnBCexception != null)
                {
                    OnBCexception(excNoAnaInData);
                }
            }
            return(null);
        }
Пример #5
0
        //===================================================================
        #region THREAD LECTURE DE LA STATION RFID
        private void ReadRFID()
        {
            while (true)
            {
                ushort ID           = ushort.Parse("3");
                byte   unit         = Convert.ToByte(Properties.Settings.Default.Unit);
                ushort StartAddress = ushort.Parse(Properties.Settings.Default.startAddress);
                byte   Length       = Convert.ToByte(Properties.Settings.Default.Size);

                MBmaster.ReadInputRegister(ID, unit, StartAddress, Length);
                Thread.Sleep(50);
            }
        }
Пример #6
0
        private void timerPolling_Tick(object sender, EventArgs e)
        {
            try
            {
                ushort size         = Convert.ToUInt16(ucTextBoxEx_Size.InputText);
                ushort startAddress = Convert.ToUInt16(ucTextBoxEx_StartAddress.InputText);
                //DISCRETE INPUTS
                MBmaster.ReadDiscreteInputs(1, 0, startAddress, size, ref newdataInputs);

                if (dataInputs.SequenceEqual <byte>(newdataInputs) == false || addressChanged)
                {
                    listBox_DiscreteInputs.BeginUpdate();
                    listBox_DiscreteInputs.Items.Clear();
                    BitArray bitArray = new BitArray(newdataInputs);
                    bool[]   bits     = new bool[bitArray.Count];
                    bitArray.CopyTo(bits, 0);

                    for (int x = 0; x < size; x++)
                    {
                        listBox_DiscreteInputs.Items.Add((x + 1 + startAddress) + "\t - " + bits[x].ToString());
                    }
                    listBox_DiscreteInputs.EndUpdate();
                    dataInputs = newdataInputs;
                }

                //COILS
                MBmaster.ReadCoils(2, 0, startAddress, size, ref newdataCoils);

                if (dataCoils.SequenceEqual <byte>(newdataCoils) == false || addressChanged)
                {
                    listBox_Coils.BeginUpdate();
                    listBox_Coils.Items.Clear();
                    BitArray bitArray = new BitArray(newdataCoils);
                    bool[]   bits     = new bool[bitArray.Count];
                    bitArray.CopyTo(bits, 0);

                    for (int x = 0; x < size; x++)
                    {
                        listBox_Coils.Items.Add((x + 1 + startAddress) + "\t - " + bits[x].ToString());
                    }
                    listBox_Coils.EndUpdate();
                    dataCoils = newdataCoils;
                }

                //INPUT REGISTERS
                MBmaster.ReadInputRegister(3, 0, startAddress, size, ref newdataInputRegisters);
                if (dataInputRegisters.SequenceEqual <byte>(newdataInputRegisters) == false || addressChanged)
                {
                    listBox_InputRegisters.BeginUpdate();
                    listBox_InputRegisters.Items.Clear();
                    int i = 1;
                    for (int x = 0; x < newdataInputRegisters.Length - 1; x += 2)
                    {
                        listBox_InputRegisters.Items.Add((i + startAddress) + "\t - " + (newdataInputRegisters[x] * 256 +
                                                                                         newdataInputRegisters[x + 1]).ToString());
                        i++;
                    }
                    listBox_InputRegisters.EndUpdate();
                    dataInputRegisters = newdataInputRegisters;
                }

                //HOLDING REGISTERS
                MBmaster.ReadHoldingRegister(4, 0, startAddress, size, ref newdataHoldingRegisters);
                if (dataHoldingRegisters.SequenceEqual <byte>(newdataHoldingRegisters) == false || addressChanged)
                {
                    listBox_HoldingRegisters.BeginUpdate();
                    listBox_HoldingRegisters.Items.Clear();
                    int i = 1;
                    for (int x = 0; x < newdataHoldingRegisters.Length - 1; x += 2)
                    {
                        listBox_HoldingRegisters.Items.Add((i + startAddress) + "\t - " + (newdataHoldingRegisters[x] * 256 +
                                                                                           newdataHoldingRegisters[x + 1]).ToString());
                        i++;
                    }
                    listBox_HoldingRegisters.EndUpdate();
                    dataHoldingRegisters = newdataHoldingRegisters;
                }
                addressChanged = false;

                MBmaster.ReadHoldingRegister(4, 0, startAddress, size, ref newdataHoldingRegisters);
                if (dataHoldingRegisters.SequenceEqual <byte>(newdataHoldingRegisters) == false || addressChanged)
                {
                    listBox_HoldingRegisters.BeginUpdate();
                    listBox_HoldingRegisters.Items.Clear();
                    int i = 1;
                    for (int x = 0; x < newdataHoldingRegisters.Length - 1; x += 2)
                    {
                        listBox_HoldingRegisters.Items.Add((i + startAddress) + "\t - " + (newdataHoldingRegisters[x] * 256 +
                                                                                           newdataHoldingRegisters[x + 1]).ToString());
                        i++;
                    }
                    listBox_HoldingRegisters.EndUpdate();
                    dataHoldingRegisters = newdataHoldingRegisters;
                }
                addressChanged = false;
            }
            catch (Exception ex)
            {
                timerPolling.Stop();
                DarkMessageBox.ShowWarning(ex.Message.ToString() + "\nDesconectando Sesión", "Warning!");
                //darkButton_Disconnect.PerformClick();
                newdataCoils               = new byte[0];
                newdataInputs              = new byte[0];
                newdataInputRegisters      = new byte[0];
                newdataHoldingRegisters    = new byte[0];
                addressChanged             = false;
                ucTextBoxEx_Size.InputText = "1";
                timerPolling.Start();
            }
        }