using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace _87K_di_latch_count { public partial class Form1 : Form { int DIAMETER = 40; int LENGTH_CUBE_HL_DIVIDED_3 = 6; int GAP_CIRCLE_HL = 5; int GAP_H_L = 4; int GAP_HL_BUTTON = 5; int BUTTON_WIDTH = 40; int BUTTON_HEIGHT = 25; int HORIZONTAL_GAP_WHOLE_SET = 10; int VERTICAL_GAP_WHOLE_SET = 5; uint diValue; uint LowLatchValue; uint HighLatchValue; bool[] Hshape = new bool[] { true, false, true, true, true, true, true, false, true }; //H shape: // T F T // T T T // T F T bool[] Lshape = new bool[] { true, false, false, true, false, false, true, true, true }; //L shape: // T F F // T F F // T T T Button[] btn = new Button[32]; public Form1() { InitializeComponent(); for (int i = 0; i < 32; i++) { btn[i] = new Button(); btn[i].Click += new EventHandler(btnClearCnt_Click); this.panel1.Controls.Add(btn[i]); } } private void printButtons(int channel) //print buttons according to total channel { int x = 10; int y = 10 + DIAMETER + GAP_CIRCLE_HL + LENGTH_CUBE_HL_DIVIDED_3 * 3 + GAP_HL_BUTTON; int horizontal_shift = DIAMETER + HORIZONTAL_GAP_WHOLE_SET; int vertical_shift = DIAMETER + GAP_CIRCLE_HL + LENGTH_CUBE_HL_DIVIDED_3 * 3 + GAP_HL_BUTTON + BUTTON_HEIGHT + VERTICAL_GAP_WHOLE_SET; for (int i = 0; i < channel; i++) { btn[i].Location = new Point(x + (i % 8) * horizontal_shift, y + (i / 8) * vertical_shift); btn[i].Size = new Size(BUTTON_WIDTH, BUTTON_HEIGHT); btn[i].Visible = true; } for (int j = channel; j < 32; j++) { btn[j].Visible = false; } } //print shape of characters. (H or L) private void printShape(PaintEventArgs e, Brush b, int x, int y, int length, bool[] shape) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (shape[j + 3 * i]) { e.Graphics.FillRectangle(b, x + length * j, y + length * i, length, length); } } } } private void panel1_Paint(object sender, PaintEventArgs e) { int x = 10; int y = 10; int horizontal_shift = DIAMETER + HORIZONTAL_GAP_WHOLE_SET; int vertical_shift = DIAMETER + GAP_CIRCLE_HL + LENGTH_CUBE_HL_DIVIDED_3 * 3 + GAP_HL_BUTTON + BUTTON_HEIGHT + VERTICAL_GAP_WHOLE_SET; int vertical_shift_HL = DIAMETER + GAP_CIRCLE_HL; int horizontal_shift_L = LENGTH_CUBE_HL_DIVIDED_3 * 3 + GAP_H_L; int total_ch = int.Parse(cbbChannel.Items[cbbChannel.SelectedIndex].ToString()); Brush bs; Pen p = new Pen(Color.Black); Brush black = new SolidBrush(Color.Black); Brush red = new SolidBrush(Color.Red); Brush blue = new SolidBrush(Color.Blue); Brush green = new SolidBrush(Color.Green); bs = black; for (int i = 0; i < total_ch; i++) { //print DI status: if (PACNET.PAC_IO.GetBit(int.Parse(diValue.ToString()), i)) bs = red; else bs = black; e.Graphics.FillEllipse(bs, x + horizontal_shift * (i % 8), y + vertical_shift * (i / 8), DIAMETER, DIAMETER); //print DI Low latch status: if (PACNET.PAC_IO.GetBit(int.Parse(LowLatchValue.ToString()), i)) bs = green; else bs = black; printShape(e, bs, x + horizontal_shift_L + horizontal_shift * (i % 8), y + vertical_shift_HL + vertical_shift * (i / 8), LENGTH_CUBE_HL_DIVIDED_3, Lshape); //print DI High latch status: if (PACNET.PAC_IO.GetBit(int.Parse(HighLatchValue.ToString()), i)) bs = blue; else bs = black; printShape(e, bs, x + horizontal_shift * (i % 8), y + vertical_shift_HL + vertical_shift * (i / 8), LENGTH_CUBE_HL_DIVIDED_3, Hshape); } } private void button1_Click(object sender, EventArgs e) { int slot = cbbSlot.SelectedIndex + 1; int total_ch = int.Parse(cbbChannel.Items[cbbChannel.SelectedIndex].ToString()); IntPtr h = PACNET.UART.Open(""); //DI Value: diValue = 0; PACNET.PAC_IO.ReadDI(h, slot, total_ch, ref diValue); textBox1.Text = diValue.ToString(); //DI High Latch Value: HighLatchValue = 0; PACNET.PAC_IO.ReadDILatch(h, slot, total_ch, 1, ref HighLatchValue); textBox2.Text = HighLatchValue.ToString(); //DI Low Latch Value: LowLatchValue = 0; PACNET.PAC_IO.ReadDILatch(h, slot, total_ch, 0, ref LowLatchValue); textBox3.Text = LowLatchValue.ToString(); //DI Counts uint counterValue = 0; for (int i = 0; i < total_ch; i++) { PACNET.PAC_IO.ReadDICNT(h, slot, i, total_ch, ref counterValue); btn[i].Text = counterValue.ToString(); } PACNET.UART.Close(h); panel1.Refresh(); } private void button2_Click(object sender, EventArgs e) { int slot = cbbSlot.SelectedIndex + 1; IntPtr h = PACNET.UART.Open(""); PACNET.PAC_IO.ClearDILatch(h, slot); PACNET.UART.Close(h); textBox2.Text = "0"; textBox3.Text = "0"; panel1.Refresh(); } private void cbbChannel_SelectedIndexChanged(object sender, EventArgs e) { int total_ch = int.Parse(cbbChannel.Items[cbbChannel.SelectedIndex].ToString()); panel1.Refresh(); printButtons(total_ch); } private void Form1_Load(object sender, EventArgs e) { cbbSlot.SelectedIndex = 0; cbbChannel.SelectedIndex = 0; int total_ch = int.Parse(cbbChannel.Items[cbbChannel.SelectedIndex].ToString()); printButtons(total_ch); } private void btnClearCnt_Click(object sender, EventArgs e) { int slot = cbbSlot.SelectedIndex + 1; int total_ch = int.Parse(cbbChannel.Items[cbbChannel.SelectedIndex].ToString()); IntPtr h = PACNET.UART.Open(""); for (int i = 0; i < 32; i++) { if (sender.Equals(btn[i])) //which button clicked { PACNET.PAC_IO.ClearDICNT(h, slot, i, total_ch); btn[i].Text = "0"; } } PACNET.UART.Close(h); } } }