protected IProtocol protocol = null; // реализует работу с протоколом обмена Dsn #endregion Fields #region Methods /// <summary> /// Создать пакет для отправки в последовательный порт /// </summary> /// <param name="packet">Пакет из которого сделать пакет для блока отображения</param> /// <param name="result">Данные конвертера</param> /// <returns>Пакет для отправки на блок отображения</returns> private byte[] GetComPacket(DisplayPacket packet, Float[] result) { Parameter[] parames = packet.Parameters; if (parames != null) { byte[] data = GetData(parames, result); if (data != null) { int size = 8 + data.Length; byte[] com_packet = new byte[size]; com_packet[0] = 0x7e; com_packet[1] = (byte)(0x80 | packet.Device); com_packet[2] = (byte)(7 + data.Length); com_packet[3] = 0x02; com_packet[4] = 0x00; com_packet[5] = (byte)data.Length; Array.Copy(data, 0, com_packet, 6, data.Length); SetCRC(com_packet); return com_packet; } } return null; }
/// <summary> /// Создать пакет для отправки в последовательный порт /// </summary> /// <param name="packet">Пакет из которого сделать пакет для блока отображения</param> /// <param name="result">Данные конвертера</param> /// <returns>Пакет для отправки на блок отображения</returns> private byte[] GetComPacket(DisplayPacket packet, Float[] result) { Parameter[] parames = packet.Parameters; if (parames != null) { byte[] data = GetData(parames, result); if (data != null) { int size = 8 + data.Length; byte[] com_packet = new byte[size]; com_packet[0] = 0x7e; com_packet[1] = (byte)(0x80 | packet.Device); com_packet[2] = (byte)(7 + data.Length); com_packet[3] = 0x02; com_packet[4] = 0x00; com_packet[5] = (byte)data.Length; Array.Copy(data, 0, com_packet, 6, data.Length); SetCRC(com_packet); return(com_packet); } } return(null); }
/// <summary> /// чекнули строку /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void listViewBos_ItemChecked(object sender, ItemCheckedEventArgs e) { if (e.Item != null) { if (e.Item.Tag is DisplayPacket) { DisplayPacket packet = e.Item.Tag as DisplayPacket; if (packet != null) { packet.IsActived = e.Item.Checked; } } } }
/// <summary> /// Добавит команду на БО /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void insert_Click(object sender, EventArgs e) { InsertBOForm frm = new InsertBOForm(app); if (frm.ShowDialog(this) == DialogResult.OK) { DisplayPacket packet = frm.DisplayPacket; if (packet != null) { app.Display.Insert(packet); InsertToList(packet); } } }
/// <summary> /// Загружаемся /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void InsertBOForm_Load(object sender, EventArgs e) { try { if ((int)packet.Period.TotalMilliseconds >= numericUpDownPeriod.Minimum && (int)packet.Period.TotalMilliseconds <= numericUpDownPeriod.Maximum) { numericUpDownPeriod.Value = (int)packet.Period.TotalMilliseconds; } else { numericUpDownPeriod.Value = numericUpDownPeriod.Minimum; packet.Period = new TimeSpan(0, 0, 0, 0, (int)numericUpDownPeriod.Minimum); } } catch { } if (packet != null) { textBoxComment.Text = packet.Description; numericUpDownDevice.Value = packet.Device; foreach (Parameter parameter in packet.Parameters) { InsertParameter(parameter); } checkBoxToPort.Checked = packet.ToPort; if (packet.TypePort == TypePort.Primary) { comboBox1.SelectedIndex = 0; } else if (packet.TypePort == TypePort.Secondary) { comboBox1.SelectedIndex = 1; } else { comboBox1.SelectedIndex = 2; } } else { packet = new DisplayPacket(); checkBoxToPort.Checked = packet.ToPort; } }
/// <summary> /// Редактировать пакет для БО /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void edit_Click(object sender, EventArgs e) { if (listViewBos.SelectedItems != null) { if (listViewBos.SelectedItems.Count > 0) { if (listViewBos.SelectedItems[0].Tag != null) { if (listViewBos.SelectedItems[0].Tag is DisplayPacket) { DisplayPacket pack = listViewBos.SelectedItems[0].Tag as DisplayPacket; if (pack != null) { InsertBOForm frm = new InsertBOForm(app); frm.DisplayPacket = pack; if (frm.ShowDialog(this) == DialogResult.OK) { DisplayPacket packet = frm.DisplayPacket; if (packet != null) { //app.Display.Insert(packet); //InsertToList(packet); listViewBos.SelectedItems[0].SubItems[1].Text = packet.Device.ToString(); listViewBos.SelectedItems[0].SubItems[2].Text = packet.Description; if (packet.TypePort == TypePort.Primary) { listViewBos.SelectedItems[0].SubItems[3].Text = "Основной"; } else if (packet.TypePort == TypePort.Secondary) { listViewBos.SelectedItems[0].SubItems[3].Text = "Вспомогательный"; } else { listViewBos.SelectedItems[0].SubItems[3].Text = "Не определен"; } } } } } } } } }
/// <summary> /// Удалить пакет /// </summary> /// <param name="packet">Удаляемый пакет</param> public void Remove(DisplayPacket packet) { try { locker.AcquireWriterLock(300); try { repository.RemovePlace(packet.Place); packets.Remove(packet); } finally { locker.ReleaseWriterLock(); } } catch { } }
/// <summary> /// Добавить пакет /// </summary> /// <param name="packet">Добавляемый пакет</param> public void Insert(DisplayPacket packet) { try { locker.AcquireWriterLock(300); try { packet.Place = repository.InstancePlace(); packets.Add(packet); } finally { locker.ReleaseWriterLock(); } } catch { } }
/// <summary> /// Удалить пакет для БО /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void remove_Click(object sender, EventArgs e) { if (listViewBos.SelectedItems != null) { if (listViewBos.SelectedItems.Count > 0) { if (listViewBos.SelectedItems[0].Tag is DisplayPacket) { DisplayPacket removed = listViewBos.SelectedItems[0].Tag as DisplayPacket; if (removed != null) { app.Display.Remove(removed); listViewBos.Items.Remove(listViewBos.SelectedItems[0]); } } } } }
/// <summary> /// формируем пакет для блока отображения /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void accept_Click(object sender, EventArgs e) { if (listView1.Items.Count > 0) { packet.Description = textBoxComment.Text; packet.Device = (int)numericUpDownDevice.Value; packet.Period = new TimeSpan(0, 0, 0, 0, (int)numericUpDownPeriod.Value); if (comboBox1.SelectedIndex == 0) { packet.TypePort = TypePort.Primary; } else if (comboBox1.SelectedIndex == 1) { packet.TypePort = TypePort.Secondary; } else { packet.TypePort = TypePort.Default; } packet.Clear(); foreach (ListViewItem item in listView1.Items) { if (item != null) { if (item.Tag != null) { if (item.Tag is Parameter) { packet.Insert(item.Tag as Parameter); } } } } } else { packet = null; } }
/// <summary> /// Загрузить настройки /// </summary> /// <param name="node">Узел в котором находятся настройки</param> public void Load(XmlNode root) { try { XmlNode node = FindNode(root); if (node != null) { if (node.HasChildNodes) { DisplayPacket packet = new DisplayPacket(); foreach (XmlNode child in node.ChildNodes) { if (child.Name == packetName) { InsertPacket(child); } } } } } catch { } }
/// <summary> /// Добавить пакет в список /// </summary> /// <param name="packet"></param> private void InsertToList(DisplayPacket packet) { ListViewItem item = new ListViewItem(); ListViewItem.ListViewSubItem num = new ListViewItem.ListViewSubItem(item, packet.Device.ToString()); ListViewItem.ListViewSubItem desc = new ListViewItem.ListViewSubItem(item, packet.Description); string port_name = string.Empty; if (packet.TypePort == TypePort.Primary) { port_name = "Основной"; } else if (packet.TypePort == TypePort.Secondary) { port_name = "Вспомогательный"; } else { port_name = "Не определен"; } ListViewItem.ListViewSubItem port = new ListViewItem.ListViewSubItem(item, port_name); item.Checked = packet.IsActived; item.SubItems.Add(num); item.SubItems.Add(desc); item.SubItems.Add(port); item.Tag = packet; listViewBos.Items.Add(item); }
/// <summary> /// Добавить параметр /// </summary> /// <param name="node">Узел в котором сохранены параметры пакета</param> protected void InsertPacket(XmlNode node) { try { if (node.HasChildNodes) { DisplayPacket packet = new DisplayPacket(); foreach (XmlNode child in node.ChildNodes) { switch (child.Name) { case parameterName: try { Parameter parameter = new Parameter(); parameter.InstanceFromXml(child); packet.Insert(parameter); } catch { } break; case commentName: try { packet.Description = child.InnerText; } catch { } break; case deviceName: try { packet.Device = int.Parse(child.InnerText); } catch { } break; case activedName: try { packet.IsActived = bool.Parse(child.InnerText); } catch { } break; case toPortName: try { packet.ToPort = bool.Parse(child.InnerText); } catch { } break; case "port_type": try { packet.TypePort = (TypePort)Enum.Parse(typeof(TypePort), child.InnerText); } catch { } break; case "period": try { packet.Period = TimeSpan.Parse(child.InnerText); } catch { } break; default: break; } } Insert(packet); } } catch { } }
/// <summary> /// формируем пакет для блока отображения /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void accept_Click(object sender, EventArgs e) { if (listView1.Items.Count > 0) { packet.Description = textBoxComment.Text; packet.Device = (int)numericUpDownDevice.Value; packet.Period = new TimeSpan(0, 0, 0, 0, (int)numericUpDownPeriod.Value); if (comboBox1.SelectedIndex == 0) { packet.TypePort = TypePort.Primary; } else if (comboBox1.SelectedIndex == 1) { packet.TypePort = TypePort.Secondary; } else { packet.TypePort = TypePort.Default; } packet.Clear(); foreach (ListViewItem item in listView1.Items) { if (item != null) { if (item.Tag != null) { if (item.Tag is Parameter) { packet.Insert(item.Tag as Parameter); } } } } } else packet = null; }