using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace _87K_dio_latch { public partial class Form1 : Form { const int DI_MAX_TOTAL_CH = 8; const int DO_MAX_TOTAL_CH = 8; const int DIAMETER = 60; const int GAP_CIRCLE_HL = 5; const int LENGTH_CUBE_HL_DIVIDED_3 = 9; const int GAP_H_L = DIAMETER - LENGTH_CUBE_HL_DIVIDED_3 * 3 * 2; const int GAP_HL_LABEL = 5; const int LABEL_WIDTH = DIAMETER; const int LABEL_HEIGHT = 20; const int GAP_DI_DO = 10; const int CHECKBOX_WIDTH = DIAMETER; const int CHECKBOX_HEIGHT = 20; const int GAP_CHECKBOX_HL = 5; const int GAP_HORIZONTAL = 10; uint diValue; uint doValue; uint DILowLatchValue = 0; uint DIHighLatchValue = 0; uint DOLowLatchValue = 0; uint DOHighLatchValue = 0; 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 CheckBox[] cbArr = new CheckBox[DO_MAX_TOTAL_CH]; Label[] lbArr = new Label[DI_MAX_TOTAL_CH]; public Form1() { InitializeComponent(); for (int i = 0; i < DO_MAX_TOTAL_CH; i++) { cbArr[i] = new CheckBox(); cbArr[i].CheckStateChanged += new System.EventHandler(this.checkBox_CheckStateChanged); this.panel1.Controls.Add(cbArr[i]); } for (int i = 0; i < DI_MAX_TOTAL_CH; i++) { lbArr[i] = new Label(); this.panel1.Controls.Add(lbArr[i]); } } private void Form1_Load(object sender, EventArgs e) { cbbSlot.SelectedIndex = 0; cbbDICh.SelectedIndex = 0; cbbDOCh.SelectedIndex = 0; int total_DI_ch = int.Parse(cbbDICh.Items[cbbDICh.SelectedIndex].ToString()); int total_DO_ch = int.Parse(cbbDOCh.Items[cbbDOCh.SelectedIndex].ToString()); printCheckBoxs(total_DO_ch); printLabels(total_DI_ch); } private void printCheckBoxs(int channel) { int x = 10; int y = 10 + DIAMETER + GAP_CIRCLE_HL + LENGTH_CUBE_HL_DIVIDED_3 * 3 + GAP_HL_LABEL + LABEL_HEIGHT + GAP_DI_DO; int vertical_shift = DIAMETER + GAP_CIRCLE_HL + LENGTH_CUBE_HL_DIVIDED_3 * 3 + GAP_HL_LABEL + LABEL_HEIGHT + GAP_DI_DO + CHECKBOX_HEIGHT + GAP_CHECKBOX_HL; int horizontal_shift = DIAMETER + GAP_HORIZONTAL; for (int i = 0; i < channel; i++) { cbArr[i].Location = new Point(x + horizontal_shift * (i % 8), y + vertical_shift * (i / 8)); cbArr[i].Size = new Size(CHECKBOX_WIDTH, CHECKBOX_HEIGHT); cbArr[i].Text = "DO_" + i.ToString("00"); cbArr[i].Visible = true; } for (int j = channel; j < DO_MAX_TOTAL_CH; j++) { cbArr[j].Visible = false; } } private void checkBox_CheckStateChanged(object sender, EventArgs e) { uint DO_value = 0; uint one = 1; for (int i = 0; i < DO_MAX_TOTAL_CH; i++) { if ((cbArr[i].Visible == true) && (cbArr[i].Checked == true)) { DO_value += one << (int.Parse(i.ToString())); } } textBox3.Text = DO_value.ToString(); } private void printLabels(int channel) { int x = 10; int y = 10 + DIAMETER + GAP_CIRCLE_HL + LENGTH_CUBE_HL_DIVIDED_3 * 3 + GAP_HL_LABEL; int vertical_shift = DIAMETER + GAP_CIRCLE_HL + LENGTH_CUBE_HL_DIVIDED_3 * 3 + GAP_HL_LABEL + LABEL_HEIGHT + GAP_DI_DO + CHECKBOX_HEIGHT + GAP_CHECKBOX_HL; int horizontal_shift = DIAMETER + GAP_HORIZONTAL; for (int i = 0; i < channel; i++) { lbArr[i].Location = new Point(x + horizontal_shift * (i % 8), y + vertical_shift * (i / 8)); lbArr[i].Size = new Size(LABEL_WIDTH, LABEL_HEIGHT); lbArr[i].Text = "DI_" + i.ToString("00"); lbArr[i].Visible = true; } for (int j = channel; j < DO_MAX_TOTAL_CH; j++) { lbArr[j].Visible = false; } } private void panel1_Paint(object sender, PaintEventArgs e) { int x = 10; int y = 10; int vertical_shift = DIAMETER + GAP_CIRCLE_HL + LENGTH_CUBE_HL_DIVIDED_3 * 3 + GAP_HL_LABEL + LABEL_HEIGHT + GAP_DI_DO + CHECKBOX_HEIGHT + GAP_CHECKBOX_HL; int horizontal_shift = DIAMETER + GAP_HORIZONTAL; int DI_vertical_shift_HL = DIAMETER + GAP_CIRCLE_HL; int DI_horizontal_shift_L = LENGTH_CUBE_HL_DIVIDED_3 * 3 + GAP_H_L; int DO_vertical_shift_HL = DIAMETER + GAP_CIRCLE_HL + LENGTH_CUBE_HL_DIVIDED_3 * 3 + GAP_HL_LABEL + LABEL_HEIGHT + GAP_DI_DO + CHECKBOX_HEIGHT + GAP_CHECKBOX_HL; int DO_horizontal_shift_L = LENGTH_CUBE_HL_DIVIDED_3 * 3 + GAP_H_L; int total_DI_ch = int.Parse(cbbDICh.Items[cbbDICh.SelectedIndex].ToString()); int total_DO_ch = int.Parse(cbbDOCh.Items[cbbDOCh.SelectedIndex].ToString()); Brush bs; Pen p = new Pen(Color.Black); Brush black = new SolidBrush(Color.Black); Brush blue = new SolidBrush(Color.Blue); Brush green = new SolidBrush(Color.Green); Brush red = new SolidBrush(Color.Red); bs = black; for (int i = 0; i < total_DI_ch; i++) { //print DI value: 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(DILowLatchValue.ToString()), i)) bs = green; else bs = black; printShape(e, bs, x + DI_horizontal_shift_L + horizontal_shift * (i % 8), y + DI_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(DIHighLatchValue.ToString()), i)) bs = blue; else bs = black; printShape(e, bs, x + horizontal_shift * (i % 8), y + DI_vertical_shift_HL + vertical_shift * (i / 8), LENGTH_CUBE_HL_DIVIDED_3, Hshape); } for (int i = 0; i < total_DO_ch; i++) { //print DO Low latch status: if (PACNET.PAC_IO.GetBit(int.Parse(DOLowLatchValue.ToString()), i)) bs = green; else bs = black; printShape(e, bs, x + DO_horizontal_shift_L + horizontal_shift * (i % 8), y + DO_vertical_shift_HL + vertical_shift * (i / 8), LENGTH_CUBE_HL_DIVIDED_3, Lshape); //print DO High latch status: if (PACNET.PAC_IO.GetBit(int.Parse(DOHighLatchValue.ToString()), i)) bs = blue; else bs = black; printShape(e, bs, x + horizontal_shift * (i % 8), y + DO_vertical_shift_HL + vertical_shift * (i / 8), LENGTH_CUBE_HL_DIVIDED_3, Hshape); } } //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 cbbDICh_SelectedIndexChanged(object sender, EventArgs e) { int total_DI_ch = int.Parse(cbbDICh.Items[cbbDICh.SelectedIndex].ToString()); panel1.Refresh(); printLabels(total_DI_ch); } private void cbbDOCh_SelectedIndexChanged(object sender, EventArgs e) { int total_DO_ch = int.Parse(cbbDOCh.Items[cbbDOCh.SelectedIndex].ToString()); printCheckBoxs(total_DO_ch); panel1.Refresh(); } private void button1_Click(object sender, EventArgs e) { int slot = cbbSlot.SelectedIndex + 1; int total_DI_ch = int.Parse(cbbDICh.Items[cbbDICh.SelectedIndex].ToString()); int total_DO_ch = int.Parse(cbbDOCh.Items[cbbDOCh.SelectedIndex].ToString()); IntPtr h = PACNET.UART.Open(""); //Read DIO diValue = 0; doValue = 0; PACNET.PAC_IO.ReadDIO(h, slot, total_DI_ch, total_DO_ch, ref diValue, ref doValue); textBox1.Text = diValue.ToString(); textBox2.Text = doValue.ToString(); //Read DIO Low Latch DILowLatchValue = 0; DOLowLatchValue = 0; PACNET.PAC_IO.ReadDIOLatch(h, slot, total_DI_ch, total_DO_ch, 0, ref DILowLatchValue, ref DOLowLatchValue); textBox7.Text = DILowLatchValue.ToString(); textBox6.Text = DOLowLatchValue.ToString(); //Read DIO High Latch DOHighLatchValue = 0; DIHighLatchValue = 0; PACNET.PAC_IO.ReadDIOLatch(h, slot, total_DI_ch, total_DO_ch, 1, ref DIHighLatchValue, ref DOHighLatchValue); textBox5.Text = DIHighLatchValue.ToString(); textBox4.Text = DOHighLatchValue.ToString(); PACNET.UART.Close(h); panel1.Refresh(); } private void button3_Click(object sender, EventArgs e) { int slot = cbbSlot.SelectedIndex + 1; IntPtr h = PACNET.UART.Open(""); PACNET.PAC_IO.ClearDIOLatch(h, slot); PACNET.UART.Close(h); } private void button2_Click(object sender, EventArgs e) { int slot = cbbSlot.SelectedIndex + 1; int total_DO_ch = int.Parse(cbbDOCh.Items[cbbDOCh.SelectedIndex].ToString()); uint DO_Value = uint.Parse(textBox3.Text); IntPtr h = PACNET.UART.Open(""); PACNET.PAC_IO.WriteDO(h, slot, total_DO_ch, DO_Value); PACNET.UART.Close(h); } private void textBox3_KeyDown(object sender, KeyEventArgs e) { if (e.KeyData == Keys.Enter) { uint DO_value = uint.Parse(textBox3.Text); for (int i = 0; i < DO_MAX_TOTAL_CH; i++) { if (1 == (1 & (DO_value >> i))) { cbArr[i].Checked = true; } else { cbArr[i].Checked = false; } } } } } }