using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace _8K_dio { 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_LABEL = 5; const int LABEL_WIDTH = 60; const int LABEL_HEIGHT = 20; const int GAP_DI_DO = 10; const int CHECKBOX_WIDTH = 60; const int CHECKBOX_HEIGHT = 20; const int GAP_HORIZONTAL = 10; uint diValue; uint doValue; 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_LABEL + LABEL_HEIGHT + GAP_DI_DO; int vertical_shift = DIAMETER + GAP_CIRCLE_LABEL + LABEL_HEIGHT + GAP_DI_DO + CHECKBOX_HEIGHT + GAP_DI_DO; 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 printLabels(int channel) { int x = 10; int y = 10 + DIAMETER + GAP_CIRCLE_LABEL; int vertical_shift = DIAMETER + GAP_CIRCLE_LABEL + LABEL_HEIGHT + GAP_DI_DO + CHECKBOX_HEIGHT + GAP_DI_DO; 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_LABEL + LABEL_HEIGHT + GAP_DI_DO + CHECKBOX_HEIGHT + GAP_DI_DO; int horizontal_shift = DIAMETER + GAP_HORIZONTAL; int total_DI_ch = int.Parse(cbbDICh.Items[cbbDICh.SelectedIndex].ToString()); Brush bs; Pen p = new Pen(Color.Black); Brush black = new SolidBrush(Color.Black); Brush red = new SolidBrush(Color.Red); bs = black; for (int i = 0; i < total_DI_ch; i++) { 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); } } 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 cbbDOCh_SelectedIndexChanged(object sender, EventArgs e) { int total_DO_ch = int.Parse(cbbDOCh.Items[cbbDOCh.SelectedIndex].ToString()); printCheckBoxs(total_DO_ch); } 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 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()); diValue = 0; doValue = 0; PACNET.PAC_IO.ReadDIO((IntPtr)null, slot, total_DI_ch, total_DO_ch, ref diValue, ref doValue); textBox1.Text = diValue.ToString(); textBox2.Text = doValue.ToString(); panel1.Refresh(); } 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); PACNET.PAC_IO.WriteDO((IntPtr)null, slot, total_DO_ch, DO_Value); } 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; } } } } } }