// ------------------------------------------------------------------------ /// <summary>Sychnronous read digital inputs starting from a specified offset.</summary> /// <param name="module_nr">The position of the module.</param> /// <param name="offset">Starting offset for digital read command.</param> /// <param name="size">Number of bits to read.</param> /// <returns>Result of the read command as bool arrary.</returns> public bool[] ReadDigitalInputs(byte module_nr, ushort offset, ushort size) { byte[] values = { }; bool[] data = new bool[size]; if (ValidateData(module_nr, size)) { if (MDinfo[module_nr].digital_in_index != 0xFFFF) { if (size <= dig_in_buffer.Length - (MDinfo[module_nr].digital_in_index * 8 + offset)) { // ------------------------------------------------------------------------ // Request value from bus coupler if (_refresh == 0) { // Read digital inputs, convert data to bool array MBmaster.ReadDiscreteInputs(ID_value, 0, Convert.ToUInt16(MDinfo[module_nr].digital_in_index * 8 + offset), size, ref values); if ((values == null) && (OnBCexception != null)) { OnBCexception(excDataEmptyAnswer); } else { BitArray tmp1 = new System.Collections.BitArray(values); bool[] tmp2 = new bool[tmp1.Count]; tmp1.CopyTo(tmp2, 0); Array.Copy(tmp2, 0, data, 0, size); } } // ------------------------------------------------------------------------ // Request value from internal buffer else { Array.Copy(dig_in_buffer, MDinfo[module_nr].digital_in_index * 8 + offset, data, 0, size); } return(data); } else if (OnBCexception != null) { OnBCexception(excDataSize); } } else if (OnBCexception != null) { OnBCexception(excNoDigInData); } } return(null); }
// ------------------------------------------------------------------------ // Button read discrete inputs // ------------------------------------------------------------------------ private void btnReadDisInp_Click(object sender, System.EventArgs e) { int ID = 2; int StartAddress = ReadStartAdr(); byte Length = Convert.ToByte(txtSize.Text); MBmaster.ReadDiscreteInputs(ID, StartAddress, Length); }
// ------------------------------------------------------------------------ // Button read discrete inputs // ------------------------------------------------------------------------ private void btnReadDisInp_Click(object sender, System.EventArgs e) { ushort ID = 2; ushort StartAddress = ReadStartAdr(); ushort Length = Convert.ToUInt16(txtSize.Text); MBmaster.ReadDiscreteInputs(ID, StartAddress, Length); }
private void ReadDisInp() { ushort ID = 2; byte unit = 0x00; ushort StartAddress = 0; UInt16 Length = 0; this.Invoke(new MethodInvoker( delegate() { unit = Convert.ToByte(txtUnit.Text); StartAddress = ReadStartAdr(); Length = Convert.ToUInt16(txtSize.Text); if (MBmaster != null) { MBmaster.ReadDiscreteInputs(ID, unit, StartAddress, Length); } } ) ); }
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(); } }