using System; using System.Linq; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace Diagnostic { public partial class Form1 : Form { const int startX = 20; const int startY = 30; const int UNIT_LENGTH = 55; const int GAP_SLOT = 5; const int GAP_MODULE_LABEL = 20; const int LABEL_HEIGHT = 20; int slotCount = 0; bool[] ModuleExist = new bool[7]; string[] ModuleName = new string[7]; Label[] lbSlot = new Label[7]; Label[] lbmoduleName = new Label[7]; Image headImage; //image of CPU-power module Image moduleImage; //image of I/O slot module Image emptyImage; //image of a slot in empty status PictureBox pbHead = new PictureBox(); PictureBox[] pbModule = new PictureBox[7]; public Form1() { InitializeComponent(); headImage = imageList1.Images[1]; moduleImage = imageList1.Images[2]; emptyImage = imageList1.Images[0]; //initialize image of CPU-power module pbHead.Image = headImage; pbHead.SizeMode = PictureBoxSizeMode.StretchImage; pbHead.Size = new Size(UNIT_LENGTH * 3, UNIT_LENGTH * 3); pbHead.Location = new Point(startX, startY); this.Controls.Add(pbHead); for (int i = 0; i < 7; i++) { //initialize Labels of Slot1, Slot2, ... lbSlot[i] = new Label(); lbSlot[i].Text = "Slot " + (i+1).ToString (); lbSlot[i].Size = new Size(UNIT_LENGTH, LABEL_HEIGHT); this.Controls.Add(lbSlot[i]); //initialize PictureBoxs which hold image of I/O module status pbModule[i] = new PictureBox(); pbModule[i].SizeMode = PictureBoxSizeMode.StretchImage; pbModule[i].Size = new Size(UNIT_LENGTH, UNIT_LENGTH * 3); pbModule[i].Location = new Point(startX + UNIT_LENGTH * 3 + GAP_SLOT + (UNIT_LENGTH + GAP_SLOT) * i, startY); pbModule[i].Image = emptyImage; this.Controls.Add(pbModule[i]); //initialize Labels to hold module names lbmoduleName[i] = new Label(); lbmoduleName[i].Size = new Size(UNIT_LENGTH, LABEL_HEIGHT); this.Controls.Add(lbmoduleName[i]); } } private void Form1_Load(object sender, EventArgs e) { slotCount = PACNET.Sys.GetSlotCount(); printSlotLabels(startX + UNIT_LENGTH * 3 + GAP_SLOT, startY + UNIT_LENGTH * 3 + GAP_MODULE_LABEL); button1_Click(sender, e); } private void showModuleImages() { for (int i = 1; i <= 7; i++) { if (i <= slotCount) { if (ModuleExist[i-1]) { pbModule[i-1].Image = moduleImage; } else { pbModule[i-1].Image = emptyImage; } } else { pbModule[i - 1].Visible = false; } } } private void printSlotLabels(int x, int y) { for (int i = 1; i <= 7; i++) { if (i <= slotCount) { lbSlot[i - 1].Location = new Point(x + (UNIT_LENGTH + GAP_SLOT) * (i - 1), y); } else { lbSlot[i - 1].Visible = false; } } } private void printModuleNameLabels(int x, int y) { for (int i = 1; i <= 7; i++) { if (i <= slotCount) { lbmoduleName[i - 1].Location = new Point(x + (UNIT_LENGTH + GAP_SLOT) * (i - 1), y); } else { lbmoduleName[i - 1].Visible = false; } } } private void button1_Click(object sender, EventArgs e) //Diagnose { IntPtr h = PACNET.UART.Open(""); //COM1:,115200,N,8,1 IntPtr _for_8K = IntPtr.Zero;// (IntPtr)0; //used for pac_ModuleExists int moduleType = 0; byte[] cmd = new byte[64]; byte[] result = new byte[64]; cmd = PACNET.MISC.AnsiString("$00M"); for (byte i = 1; i <= slotCount; i++) { ModuleExist[i - 1] = PACNET.Sys.ModuleExists(_for_8K, i); //for 8K IO module only if (ModuleExist[i - 1]) { moduleType = PACNET.Sys.GetModuleName(i, ref ModuleName[i - 1]); if (255 == moduleType) //87K IO modules { PACNET.Sys.ChangeSlot(i); PACNET.UART.SendCmd(h, cmd, result); // $00M: ask the device name string strResult = PACNET.MISC.WideString(result); ModuleName[i - 1] = strResult.Substring(3); //return format for example: !0087055 } } else { ModuleName[i - 1] = ""; } lbmoduleName[i - 1].Text = ModuleName[i - 1]; } PACNET.UART.Close(h); printModuleNameLabels(startX + UNIT_LENGTH * 3 + GAP_SLOT, startY - GAP_MODULE_LABEL); showModuleImages(); } } }