using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using PCANLight;

namespace ICLRead
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		#region Structures
		/// <summary>
		/// Message Status structure used to show CAN Messages
		/// in a ListView
		/// </summary>
		private struct MessageStatus
		{
			private TCLightMsg Msg;
			private int iIndex;

			public MessageStatus(TCLightMsg CanMsg,int Index)
			{
				Msg = CanMsg;
				iIndex = Index;
			}

			public TCLightMsg CANMessage
			{
				get { return Msg; }
			}

			public int Position
			{
				get { return iIndex; }
			}
		}	
		#endregion

		private System.Windows.Forms.GroupBox groupBox3;
		private System.Windows.Forms.TextBox txtInfo;
		private System.Windows.Forms.Button btnClose;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.Button btnWrite;
		private System.Windows.Forms.CheckBox chbExtended;
		private System.Windows.Forms.CheckBox chbRemote;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.Label label7;
		private System.Windows.Forms.Label label8;
		private System.Windows.Forms.TextBox txtID;
		private System.Windows.Forms.TextBox txtData7;
		private System.Windows.Forms.TextBox txtData6;
		private System.Windows.Forms.TextBox txtData5;
		private System.Windows.Forms.TextBox txtData4;
		private System.Windows.Forms.TextBox txtData3;
		private System.Windows.Forms.TextBox txtData2;
		private System.Windows.Forms.TextBox txtData1;
		private System.Windows.Forms.TextBox txtData0;
		private System.Windows.Forms.NumericUpDown nudLength;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.ComboBox cbbHws;
		private System.Windows.Forms.ComboBox cbbMsgType;
		private System.Windows.Forms.ComboBox cbbInterrupt;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.ComboBox cbbIO;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.ComboBox cbbBaudrates;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Button btnInfo;
		private System.Windows.Forms.Button btnRelease;
		private System.Windows.Forms.Button btnInit;
		private System.Windows.Forms.GroupBox groupBox4;
		private System.Windows.Forms.ListView lstMessages;
		private System.Windows.Forms.ColumnHeader clhType;
		private System.Windows.Forms.ColumnHeader clhID;
		private System.Windows.Forms.ColumnHeader clhLength;
		private System.Windows.Forms.ColumnHeader clhData;
		private System.Windows.Forms.ColumnHeader clhCount;
		private System.Windows.Forms.Timer tmrRead;
		/// <summary>
		/// Required designer variable.
		/// </summary>		
		private System.ComponentModel.IContainer components = null;

		/// <summary>
		/// Save the current initiated hardware type
		/// </summary>
		private HardwareType ActiveHardware;
		private System.Windows.Forms.GroupBox groupBox5;
		private System.Windows.Forms.RadioButton rdbStandard;
		private System.Windows.Forms.RadioButton rdbExtended;
		private System.Windows.Forms.Label label9;
		private System.Windows.Forms.TextBox txtIdFrom;
		private System.Windows.Forms.Label label10;
		private System.Windows.Forms.TextBox txtIdTo;
		private System.Windows.Forms.Button btnSetFilter;
		private System.Windows.Forms.Button btnResetFilter;
		private System.Windows.Forms.Button btnGetUsbDevNumber;
		private System.Windows.Forms.Button btnSetUsbDevNumber;
		private System.Windows.Forms.NumericUpDown nudDevNumber;

		/// <summary>
		/// CAN messages Array. Store the Message Status for its display
		/// </summary>
		private ArrayList LastMsgsList;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			// We set the variable to know which hardware is 
			// currently selected (none!)
			//
			ActiveHardware = (HardwareType)(-1);
			// Create a list to store the displayed mesasges 
			//
			LastMsgsList = new ArrayList();
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
			this.groupBox3 = new System.Windows.Forms.GroupBox();
			this.txtInfo = new System.Windows.Forms.TextBox();
			this.btnClose = new System.Windows.Forms.Button();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.chbRemote = new System.Windows.Forms.CheckBox();
			this.btnWrite = new System.Windows.Forms.Button();
			this.chbExtended = new System.Windows.Forms.CheckBox();
			this.label6 = new System.Windows.Forms.Label();
			this.label7 = new System.Windows.Forms.Label();
			this.label8 = new System.Windows.Forms.Label();
			this.txtID = new System.Windows.Forms.TextBox();
			this.txtData7 = new System.Windows.Forms.TextBox();
			this.txtData6 = new System.Windows.Forms.TextBox();
			this.txtData5 = new System.Windows.Forms.TextBox();
			this.txtData4 = new System.Windows.Forms.TextBox();
			this.txtData3 = new System.Windows.Forms.TextBox();
			this.txtData2 = new System.Windows.Forms.TextBox();
			this.txtData1 = new System.Windows.Forms.TextBox();
			this.txtData0 = new System.Windows.Forms.TextBox();
			this.nudLength = new System.Windows.Forms.NumericUpDown();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.nudDevNumber = new System.Windows.Forms.NumericUpDown();
			this.cbbHws = new System.Windows.Forms.ComboBox();
			this.cbbMsgType = new System.Windows.Forms.ComboBox();
			this.cbbInterrupt = new System.Windows.Forms.ComboBox();
			this.label5 = new System.Windows.Forms.Label();
			this.cbbIO = new System.Windows.Forms.ComboBox();
			this.label4 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.cbbBaudrates = new System.Windows.Forms.ComboBox();
			this.label2 = new System.Windows.Forms.Label();
			this.label1 = new System.Windows.Forms.Label();
			this.btnInfo = new System.Windows.Forms.Button();
			this.btnRelease = new System.Windows.Forms.Button();
			this.btnInit = new System.Windows.Forms.Button();
			this.btnGetUsbDevNumber = new System.Windows.Forms.Button();
			this.btnSetUsbDevNumber = new System.Windows.Forms.Button();
			this.groupBox4 = new System.Windows.Forms.GroupBox();
			this.lstMessages = new System.Windows.Forms.ListView();
			this.clhType = new System.Windows.Forms.ColumnHeader();
			this.clhID = new System.Windows.Forms.ColumnHeader();
			this.clhLength = new System.Windows.Forms.ColumnHeader();
			this.clhData = new System.Windows.Forms.ColumnHeader();
			this.clhCount = new System.Windows.Forms.ColumnHeader();
			this.tmrRead = new System.Windows.Forms.Timer(this.components);
			this.groupBox5 = new System.Windows.Forms.GroupBox();
			this.btnResetFilter = new System.Windows.Forms.Button();
			this.btnSetFilter = new System.Windows.Forms.Button();
			this.label10 = new System.Windows.Forms.Label();
			this.txtIdTo = new System.Windows.Forms.TextBox();
			this.label9 = new System.Windows.Forms.Label();
			this.txtIdFrom = new System.Windows.Forms.TextBox();
			this.rdbExtended = new System.Windows.Forms.RadioButton();
			this.rdbStandard = new System.Windows.Forms.RadioButton();
			this.groupBox3.SuspendLayout();
			this.groupBox2.SuspendLayout();
			((System.ComponentModel.ISupportInitialize)(this.nudLength)).BeginInit();
			this.groupBox1.SuspendLayout();
			((System.ComponentModel.ISupportInitialize)(this.nudDevNumber)).BeginInit();
			this.groupBox4.SuspendLayout();
			this.groupBox5.SuspendLayout();
			this.SuspendLayout();
			// 
			// groupBox3
			// 
			this.groupBox3.Controls.Add(this.txtInfo);
			this.groupBox3.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.groupBox3.Location = new System.Drawing.Point(8, 433);
			this.groupBox3.Name = "groupBox3";
			this.groupBox3.Size = new System.Drawing.Size(400, 72);
			this.groupBox3.TabIndex = 44;
			this.groupBox3.TabStop = false;
			this.groupBox3.Text = "Information";
			// 
			// txtInfo
			// 
			this.txtInfo.AutoSize = false;
			this.txtInfo.BackColor = System.Drawing.Color.White;
			this.txtInfo.Cursor = System.Windows.Forms.Cursors.Arrow;
			this.txtInfo.ImeMode = System.Windows.Forms.ImeMode.NoControl;
			this.txtInfo.Location = new System.Drawing.Point(8, 16);
			this.txtInfo.Multiline = true;
			this.txtInfo.Name = "txtInfo";
			this.txtInfo.ReadOnly = true;
			this.txtInfo.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
			this.txtInfo.Size = new System.Drawing.Size(384, 48);
			this.txtInfo.TabIndex = 38;
			this.txtInfo.TabStop = false;
			this.txtInfo.Text = "Select a Hardware and a configuration for it. Then click \"Initialize\" button";
			this.txtInfo.DoubleClick += new System.EventHandler(this.txtInfo_DoubleClick);
			// 
			// btnClose
			// 
			this.btnClose.Cursor = System.Windows.Forms.Cursors.Hand;
			this.btnClose.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnClose.Location = new System.Drawing.Point(352, 512);
			this.btnClose.Name = "btnClose";
			this.btnClose.Size = new System.Drawing.Size(56, 23);
			this.btnClose.TabIndex = 43;
			this.btnClose.Text = "Close";
			this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.Add(this.chbRemote);
			this.groupBox2.Controls.Add(this.btnWrite);
			this.groupBox2.Controls.Add(this.chbExtended);
			this.groupBox2.Controls.Add(this.label6);
			this.groupBox2.Controls.Add(this.label7);
			this.groupBox2.Controls.Add(this.label8);
			this.groupBox2.Controls.Add(this.txtID);
			this.groupBox2.Controls.Add(this.txtData7);
			this.groupBox2.Controls.Add(this.txtData6);
			this.groupBox2.Controls.Add(this.txtData5);
			this.groupBox2.Controls.Add(this.txtData4);
			this.groupBox2.Controls.Add(this.txtData3);
			this.groupBox2.Controls.Add(this.txtData2);
			this.groupBox2.Controls.Add(this.txtData1);
			this.groupBox2.Controls.Add(this.txtData0);
			this.groupBox2.Controls.Add(this.nudLength);
			this.groupBox2.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.groupBox2.Location = new System.Drawing.Point(8, 150);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(400, 94);
			this.groupBox2.TabIndex = 42;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "Write Messages";
			// 
			// chbRemote
			// 
			this.chbRemote.Cursor = System.Windows.Forms.Cursors.Hand;
			this.chbRemote.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.chbRemote.Location = new System.Drawing.Point(112, 64);
			this.chbRemote.Name = "chbRemote";
			this.chbRemote.Size = new System.Drawing.Size(112, 24);
			this.chbRemote.TabIndex = 33;
			this.chbRemote.Text = "Remote Request";
			this.chbRemote.CheckedChanged += new System.EventHandler(this.chbRemote_CheckedChanged);
			// 
			// btnWrite
			// 
			this.btnWrite.Cursor = System.Windows.Forms.Cursors.Hand;
			this.btnWrite.Enabled = false;
			this.btnWrite.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnWrite.Location = new System.Drawing.Point(304, 64);
			this.btnWrite.Name = "btnWrite";
			this.btnWrite.Size = new System.Drawing.Size(88, 23);
			this.btnWrite.TabIndex = 36;
			this.btnWrite.Text = "Write Message";
			this.btnWrite.Click += new System.EventHandler(this.btnWrite_Click);
			// 
			// chbExtended
			// 
			this.chbExtended.Cursor = System.Windows.Forms.Cursors.Hand;
			this.chbExtended.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.chbExtended.Location = new System.Drawing.Point(8, 64);
			this.chbExtended.Name = "chbExtended";
			this.chbExtended.Size = new System.Drawing.Size(112, 24);
			this.chbExtended.TabIndex = 34;
			this.chbExtended.Text = "Extended Frame";
			this.chbExtended.CheckedChanged += new System.EventHandler(this.chbExtended_CheckedChanged);
			// 
			// label6
			// 
			this.label6.AutoSize = true;
			this.label6.Location = new System.Drawing.Point(144, 16);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(61, 16);
			this.label6.TabIndex = 32;
			this.label6.Text = "Data (0..7):";
			// 
			// label7
			// 
			this.label7.AutoSize = true;
			this.label7.Location = new System.Drawing.Point(96, 16);
			this.label7.Name = "label7";
			this.label7.Size = new System.Drawing.Size(42, 16);
			this.label7.TabIndex = 31;
			this.label7.Text = "Length:";
			// 
			// label8
			// 
			this.label8.AutoSize = true;
			this.label8.Location = new System.Drawing.Point(8, 16);
			this.label8.Name = "label8";
			this.label8.Size = new System.Drawing.Size(49, 16);
			this.label8.TabIndex = 30;
			this.label8.Text = "ID (Hex):";
			// 
			// txtID
			// 
			this.txtID.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
			this.txtID.Location = new System.Drawing.Point(8, 40);
			this.txtID.MaxLength = 3;
			this.txtID.Name = "txtID";
			this.txtID.Size = new System.Drawing.Size(80, 20);
			this.txtID.TabIndex = 20;
			this.txtID.Text = "0";
			this.txtID.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtID_KeyPress);
			this.txtID.Leave += new System.EventHandler(this.txtID_Leave);
			// 
			// txtData7
			// 
			this.txtData7.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
			this.txtData7.Location = new System.Drawing.Point(368, 40);
			this.txtData7.MaxLength = 2;
			this.txtData7.Name = "txtData7";
			this.txtData7.Size = new System.Drawing.Size(24, 20);
			this.txtData7.TabIndex = 29;
			this.txtData7.Text = "00";
			this.txtData7.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
			this.txtData7.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtID_KeyPress);
			this.txtData7.Leave += new System.EventHandler(this.txtData0_Leave);
			// 
			// txtData6
			// 
			this.txtData6.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
			this.txtData6.Location = new System.Drawing.Point(336, 40);
			this.txtData6.MaxLength = 2;
			this.txtData6.Name = "txtData6";
			this.txtData6.Size = new System.Drawing.Size(24, 20);
			this.txtData6.TabIndex = 28;
			this.txtData6.Text = "00";
			this.txtData6.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
			this.txtData6.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtID_KeyPress);
			this.txtData6.Leave += new System.EventHandler(this.txtData0_Leave);
			// 
			// txtData5
			// 
			this.txtData5.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
			this.txtData5.Location = new System.Drawing.Point(304, 40);
			this.txtData5.MaxLength = 2;
			this.txtData5.Name = "txtData5";
			this.txtData5.Size = new System.Drawing.Size(24, 20);
			this.txtData5.TabIndex = 27;
			this.txtData5.Text = "00";
			this.txtData5.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
			this.txtData5.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtID_KeyPress);
			this.txtData5.Leave += new System.EventHandler(this.txtData0_Leave);
			// 
			// txtData4
			// 
			this.txtData4.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
			this.txtData4.Location = new System.Drawing.Point(272, 40);
			this.txtData4.MaxLength = 2;
			this.txtData4.Name = "txtData4";
			this.txtData4.Size = new System.Drawing.Size(24, 20);
			this.txtData4.TabIndex = 26;
			this.txtData4.Text = "00";
			this.txtData4.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
			this.txtData4.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtID_KeyPress);
			this.txtData4.Leave += new System.EventHandler(this.txtData0_Leave);
			// 
			// txtData3
			// 
			this.txtData3.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
			this.txtData3.Location = new System.Drawing.Point(240, 40);
			this.txtData3.MaxLength = 2;
			this.txtData3.Name = "txtData3";
			this.txtData3.Size = new System.Drawing.Size(24, 20);
			this.txtData3.TabIndex = 25;
			this.txtData3.Text = "00";
			this.txtData3.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
			this.txtData3.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtID_KeyPress);
			this.txtData3.Leave += new System.EventHandler(this.txtData0_Leave);
			// 
			// txtData2
			// 
			this.txtData2.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
			this.txtData2.Location = new System.Drawing.Point(208, 40);
			this.txtData2.MaxLength = 2;
			this.txtData2.Name = "txtData2";
			this.txtData2.Size = new System.Drawing.Size(24, 20);
			this.txtData2.TabIndex = 24;
			this.txtData2.Text = "00";
			this.txtData2.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
			this.txtData2.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtID_KeyPress);
			this.txtData2.Leave += new System.EventHandler(this.txtData0_Leave);
			// 
			// txtData1
			// 
			this.txtData1.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
			this.txtData1.Location = new System.Drawing.Point(176, 40);
			this.txtData1.MaxLength = 2;
			this.txtData1.Name = "txtData1";
			this.txtData1.Size = new System.Drawing.Size(24, 20);
			this.txtData1.TabIndex = 23;
			this.txtData1.Text = "00";
			this.txtData1.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
			this.txtData1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtID_KeyPress);
			this.txtData1.Leave += new System.EventHandler(this.txtData0_Leave);
			// 
			// txtData0
			// 
			this.txtData0.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
			this.txtData0.Location = new System.Drawing.Point(144, 40);
			this.txtData0.MaxLength = 2;
			this.txtData0.Name = "txtData0";
			this.txtData0.Size = new System.Drawing.Size(24, 20);
			this.txtData0.TabIndex = 22;
			this.txtData0.Text = "00";
			this.txtData0.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
			this.txtData0.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtID_KeyPress);
			this.txtData0.Leave += new System.EventHandler(this.txtData0_Leave);
			// 
			// nudLength
			// 
			this.nudLength.BackColor = System.Drawing.Color.White;
			this.nudLength.Location = new System.Drawing.Point(96, 40);
			this.nudLength.Maximum = new System.Decimal(new int[] {
																	  8,
																	  0,
																	  0,
																	  0});
			this.nudLength.Name = "nudLength";
			this.nudLength.ReadOnly = true;
			this.nudLength.Size = new System.Drawing.Size(40, 20);
			this.nudLength.TabIndex = 21;
			this.nudLength.Value = new System.Decimal(new int[] {
																	8,
																	0,
																	0,
																	0});
			this.nudLength.ValueChanged += new System.EventHandler(this.nudLength_ValueChanged);
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.nudDevNumber);
			this.groupBox1.Controls.Add(this.cbbHws);
			this.groupBox1.Controls.Add(this.cbbMsgType);
			this.groupBox1.Controls.Add(this.cbbInterrupt);
			this.groupBox1.Controls.Add(this.label5);
			this.groupBox1.Controls.Add(this.cbbIO);
			this.groupBox1.Controls.Add(this.label4);
			this.groupBox1.Controls.Add(this.label3);
			this.groupBox1.Controls.Add(this.cbbBaudrates);
			this.groupBox1.Controls.Add(this.label2);
			this.groupBox1.Controls.Add(this.label1);
			this.groupBox1.Controls.Add(this.btnInfo);
			this.groupBox1.Controls.Add(this.btnRelease);
			this.groupBox1.Controls.Add(this.btnInit);
			this.groupBox1.Controls.Add(this.btnGetUsbDevNumber);
			this.groupBox1.Controls.Add(this.btnSetUsbDevNumber);
			this.groupBox1.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.groupBox1.Location = new System.Drawing.Point(8, 8);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(400, 138);
			this.groupBox1.TabIndex = 41;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Connection";
			// 
			// nudDevNumber
			// 
			this.nudDevNumber.BackColor = System.Drawing.Color.White;
			this.nudDevNumber.Location = new System.Drawing.Point(344, 64);
			this.nudDevNumber.Maximum = new System.Decimal(new int[] {
																		 255,
																		 0,
																		 0,
																		 0});
			this.nudDevNumber.Name = "nudDevNumber";
			this.nudDevNumber.ReadOnly = true;
			this.nudDevNumber.Size = new System.Drawing.Size(48, 20);
			this.nudDevNumber.TabIndex = 47;
			this.nudDevNumber.Value = new System.Decimal(new int[] {
																	   8,
																	   0,
																	   0,
																	   0});
			// 
			// cbbHws
			// 
			this.cbbHws.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cbbHws.Items.AddRange(new object[] {
														"ISA",
														"ISA 2-CH",
														"PCI",
														"PCI 2-CH",
														"PCC",
														"PCC 2-CH",
														"USB 1-CH",
														"USB 2-CH",
														"DONGLE PRO",
														"DONGLE"});
			this.cbbHws.Location = new System.Drawing.Point(8, 32);
			this.cbbHws.Name = "cbbHws";
			this.cbbHws.Size = new System.Drawing.Size(128, 21);
			this.cbbHws.TabIndex = 32;
			this.cbbHws.SelectedIndexChanged += new System.EventHandler(this.cbbHws_SelectedIndexChanged);
			// 
			// cbbMsgType
			// 
			this.cbbMsgType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cbbMsgType.Items.AddRange(new object[] {
															"Standard",
															"Extended"});
			this.cbbMsgType.Location = new System.Drawing.Point(112, 109);
			this.cbbMsgType.Name = "cbbMsgType";
			this.cbbMsgType.Size = new System.Drawing.Size(88, 21);
			this.cbbMsgType.TabIndex = 37;
			// 
			// cbbInterrupt
			// 
			this.cbbInterrupt.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cbbInterrupt.Items.AddRange(new object[] {
															  "3",
															  "4",
															  "5",
															  "7",
															  "9",
															  "10",
															  "11",
															  "12",
															  "15"});
			this.cbbInterrupt.Location = new System.Drawing.Point(304, 109);
			this.cbbInterrupt.Name = "cbbInterrupt";
			this.cbbInterrupt.Size = new System.Drawing.Size(88, 21);
			this.cbbInterrupt.TabIndex = 39;
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(304, 93);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(72, 23);
			this.label5.TabIndex = 44;
			this.label5.Text = "Interrupt:";
			// 
			// cbbIO
			// 
			this.cbbIO.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cbbIO.Items.AddRange(new object[] {
													   "0100",
													   "0120",
													   "0140",
													   "0200",
													   "0220",
													   "0240",
													   "0260",
													   "0278",
													   "0280",
													   "02A0",
													   "02C0",
													   "02E0",
													   "02E8",
													   "02F8",
													   "0300",
													   "0320",
													   "0340",
													   "0360",
													   "0378",
													   "0380",
													   "03BC",
													   "03E0",
													   "03E8",
													   "03F8"});
			this.cbbIO.Location = new System.Drawing.Point(208, 109);
			this.cbbIO.Name = "cbbIO";
			this.cbbIO.Size = new System.Drawing.Size(88, 21);
			this.cbbIO.TabIndex = 38;
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(208, 93);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(72, 23);
			this.label4.TabIndex = 43;
			this.label4.Text = "I/O address:";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(112, 93);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(64, 23);
			this.label3.TabIndex = 42;
			this.label3.Text = "Msg Type:";
			// 
			// cbbBaudrates
			// 
			this.cbbBaudrates.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cbbBaudrates.Items.AddRange(new object[] {
															  "1 MBit/sec",
															  "500 KBit/sec",
															  "250 KBit/sec",
															  "125 KBit/sec",
															  "100 KBit/sec",
															  "50 KBit/sec",
															  "20 KBit/sec",
															  "10 KBit/sec",
															  "5 KBit/sec"});
			this.cbbBaudrates.Location = new System.Drawing.Point(8, 109);
			this.cbbBaudrates.Name = "cbbBaudrates";
			this.cbbBaudrates.Size = new System.Drawing.Size(96, 21);
			this.cbbBaudrates.TabIndex = 36;
			this.cbbBaudrates.SelectedIndexChanged += new System.EventHandler(this.cbbBaudrates_SelectedIndexChanged);
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 93);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(56, 23);
			this.label2.TabIndex = 41;
			this.label2.Text = "Baudrate:";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 16);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(56, 23);
			this.label1.TabIndex = 40;
			this.label1.Text = "Hardware:";
			// 
			// btnInfo
			// 
			this.btnInfo.Cursor = System.Windows.Forms.Cursors.Hand;
			this.btnInfo.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnInfo.Location = new System.Drawing.Point(144, 32);
			this.btnInfo.Name = "btnInfo";
			this.btnInfo.Size = new System.Drawing.Size(56, 23);
			this.btnInfo.TabIndex = 33;
			this.btnInfo.Text = "Get Info";
			this.btnInfo.Click += new System.EventHandler(this.btnInfo_Click);
			// 
			// btnRelease
			// 
			this.btnRelease.Cursor = System.Windows.Forms.Cursors.Hand;
			this.btnRelease.Enabled = false;
			this.btnRelease.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnRelease.Location = new System.Drawing.Point(336, 32);
			this.btnRelease.Name = "btnRelease";
			this.btnRelease.Size = new System.Drawing.Size(56, 23);
			this.btnRelease.TabIndex = 35;
			this.btnRelease.Text = "Release";
			this.btnRelease.Click += new System.EventHandler(this.btnRelease_Click);
			// 
			// btnInit
			// 
			this.btnInit.Cursor = System.Windows.Forms.Cursors.Hand;
			this.btnInit.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnInit.Location = new System.Drawing.Point(272, 32);
			this.btnInit.Name = "btnInit";
			this.btnInit.Size = new System.Drawing.Size(56, 23);
			this.btnInit.TabIndex = 34;
			this.btnInit.Text = "Initialize";
			this.btnInit.Click += new System.EventHandler(this.btnInit_Click);
			// 
			// btnGetUsbDevNumber
			// 
			this.btnGetUsbDevNumber.Location = new System.Drawing.Point(8, 64);
			this.btnGetUsbDevNumber.Name = "btnGetUsbDevNumber";
			this.btnGetUsbDevNumber.Size = new System.Drawing.Size(160, 23);
			this.btnGetUsbDevNumber.TabIndex = 45;
			this.btnGetUsbDevNumber.Text = "Get USB Device Number";
			this.btnGetUsbDevNumber.Click += new System.EventHandler(this.btnGetUsbDevNumber_Click);
			// 
			// btnSetUsbDevNumber
			// 
			this.btnSetUsbDevNumber.Location = new System.Drawing.Point(176, 64);
			this.btnSetUsbDevNumber.Name = "btnSetUsbDevNumber";
			this.btnSetUsbDevNumber.Size = new System.Drawing.Size(152, 23);
			this.btnSetUsbDevNumber.TabIndex = 46;
			this.btnSetUsbDevNumber.Text = "Set USB Device Number";
			this.btnSetUsbDevNumber.Click += new System.EventHandler(this.btnSetUsbDevNumber_Click);
			// 
			// groupBox4
			// 
			this.groupBox4.Controls.Add(this.lstMessages);
			this.groupBox4.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.groupBox4.Location = new System.Drawing.Point(8, 317);
			this.groupBox4.Name = "groupBox4";
			this.groupBox4.Size = new System.Drawing.Size(400, 112);
			this.groupBox4.TabIndex = 45;
			this.groupBox4.TabStop = false;
			this.groupBox4.Text = "Read Messages";
			// 
			// lstMessages
			// 
			this.lstMessages.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																						  this.clhType,
																						  this.clhID,
																						  this.clhLength,
																						  this.clhData,
																						  this.clhCount});
			this.lstMessages.FullRowSelect = true;
			this.lstMessages.Location = new System.Drawing.Point(8, 16);
			this.lstMessages.MultiSelect = false;
			this.lstMessages.Name = "lstMessages";
			this.lstMessages.Size = new System.Drawing.Size(384, 88);
			this.lstMessages.TabIndex = 28;
			this.lstMessages.View = System.Windows.Forms.View.Details;
			this.lstMessages.DoubleClick += new System.EventHandler(this.lstMessages_DoubleClick);
			// 
			// clhType
			// 
			this.clhType.Text = "Type";
			this.clhType.Width = 69;
			// 
			// clhID
			// 
			this.clhID.Text = "ID";
			this.clhID.Width = 73;
			// 
			// clhLength
			// 
			this.clhLength.Text = "Length";
			this.clhLength.Width = 50;
			// 
			// clhData
			// 
			this.clhData.Text = "Data";
			this.clhData.Width = 138;
			// 
			// clhCount
			// 
			this.clhCount.Text = "Count";
			this.clhCount.Width = 49;
			// 
			// tmrRead
			// 
			this.tmrRead.Interval = 50;
			this.tmrRead.Tick += new System.EventHandler(this.tmrRead_Tick);
			// 
			// groupBox5
			// 
			this.groupBox5.Controls.Add(this.btnResetFilter);
			this.groupBox5.Controls.Add(this.btnSetFilter);
			this.groupBox5.Controls.Add(this.label10);
			this.groupBox5.Controls.Add(this.txtIdTo);
			this.groupBox5.Controls.Add(this.label9);
			this.groupBox5.Controls.Add(this.txtIdFrom);
			this.groupBox5.Controls.Add(this.rdbExtended);
			this.groupBox5.Controls.Add(this.rdbStandard);
			this.groupBox5.Location = new System.Drawing.Point(8, 248);
			this.groupBox5.Name = "groupBox5";
			this.groupBox5.Size = new System.Drawing.Size(400, 65);
			this.groupBox5.TabIndex = 46;
			this.groupBox5.TabStop = false;
			this.groupBox5.Text = "Message Filter";
			// 
			// btnResetFilter
			// 
			this.btnResetFilter.Cursor = System.Windows.Forms.Cursors.Hand;
			this.btnResetFilter.Enabled = false;
			this.btnResetFilter.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnResetFilter.Location = new System.Drawing.Point(336, 34);
			this.btnResetFilter.Name = "btnResetFilter";
			this.btnResetFilter.Size = new System.Drawing.Size(56, 23);
			this.btnResetFilter.TabIndex = 36;
			this.btnResetFilter.Text = "Reset";
			this.btnResetFilter.Click += new System.EventHandler(this.btnResetFilter_Click);
			// 
			// btnSetFilter
			// 
			this.btnSetFilter.Cursor = System.Windows.Forms.Cursors.Hand;
			this.btnSetFilter.Enabled = false;
			this.btnSetFilter.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnSetFilter.Location = new System.Drawing.Point(272, 34);
			this.btnSetFilter.Name = "btnSetFilter";
			this.btnSetFilter.Size = new System.Drawing.Size(56, 23);
			this.btnSetFilter.TabIndex = 35;
			this.btnSetFilter.Text = "Set";
			this.btnSetFilter.Click += new System.EventHandler(this.btnSetFilter_Click);
			// 
			// label10
			// 
			this.label10.AutoSize = true;
			this.label10.Location = new System.Drawing.Point(179, 17);
			this.label10.Name = "label10";
			this.label10.Size = new System.Drawing.Size(21, 16);
			this.label10.TabIndex = 34;
			this.label10.Text = "To:";
			// 
			// txtIdTo
			// 
			this.txtIdTo.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
			this.txtIdTo.Location = new System.Drawing.Point(179, 36);
			this.txtIdTo.MaxLength = 3;
			this.txtIdTo.Name = "txtIdTo";
			this.txtIdTo.Size = new System.Drawing.Size(77, 20);
			this.txtIdTo.TabIndex = 33;
			this.txtIdTo.Text = "0";
			this.txtIdTo.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtID_KeyPress);
			this.txtIdTo.Leave += new System.EventHandler(this.txtIdFrom_Leave);
			// 
			// label9
			// 
			this.label9.AutoSize = true;
			this.label9.Location = new System.Drawing.Point(91, 17);
			this.label9.Name = "label9";
			this.label9.Size = new System.Drawing.Size(34, 16);
			this.label9.TabIndex = 32;
			this.label9.Text = "From:";
			// 
			// txtIdFrom
			// 
			this.txtIdFrom.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
			this.txtIdFrom.Location = new System.Drawing.Point(94, 36);
			this.txtIdFrom.MaxLength = 3;
			this.txtIdFrom.Name = "txtIdFrom";
			this.txtIdFrom.Size = new System.Drawing.Size(77, 20);
			this.txtIdFrom.TabIndex = 31;
			this.txtIdFrom.Text = "0";
			this.txtIdFrom.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtID_KeyPress);
			this.txtIdFrom.Leave += new System.EventHandler(this.txtIdFrom_Leave);
			// 
			// rdbExtended
			// 
			this.rdbExtended.Location = new System.Drawing.Point(11, 40);
			this.rdbExtended.Name = "rdbExtended";
			this.rdbExtended.Size = new System.Drawing.Size(104, 16);
			this.rdbExtended.TabIndex = 1;
			this.rdbExtended.Text = "Extended";
			// 
			// rdbStandard
			// 
			this.rdbStandard.Checked = true;
			this.rdbStandard.Location = new System.Drawing.Point(11, 23);
			this.rdbStandard.Name = "rdbStandard";
			this.rdbStandard.Size = new System.Drawing.Size(104, 16);
			this.rdbStandard.TabIndex = 0;
			this.rdbStandard.TabStop = true;
			this.rdbStandard.Text = "Standard";
			this.rdbStandard.CheckedChanged += new System.EventHandler(this.rdbStandard_CheckedChanged);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(416, 544);
			this.Controls.Add(this.groupBox5);
			this.Controls.Add(this.groupBox4);
			this.Controls.Add(this.groupBox3);
			this.Controls.Add(this.btnClose);
			this.Controls.Add(this.groupBox2);
			this.Controls.Add(this.groupBox1);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.MaximizeBox = false;
			this.Name = "Form1";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "I CAN Do It! - Light";
			this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
			this.Load += new System.EventHandler(this.Form1_Load);
			this.groupBox3.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			((System.ComponentModel.ISupportInitialize)(this.nudLength)).EndInit();
			this.groupBox1.ResumeLayout(false);
			((System.ComponentModel.ISupportInitialize)(this.nudDevNumber)).EndInit();
			this.groupBox4.ResumeLayout(false);
			this.groupBox5.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.EnableVisualStyles();
			Application.Run(new Form1());
		}

		#region Miscellaneus Help Functions
		private void ModifyMsgEntry(MessageStatus LastMsg, TCLightMsg NewMsg)
		{
			string strLastData, strNewData;
			MessageStatus NewStatus;
			ListViewItem CurrItem;
			uint iLen;
			int iCount;

			strNewData = strLastData = "";

			// Values from the last messages
			//
			CurrItem = lstMessages.Items[LastMsg.Position];
			iCount = Convert.ToInt32(CurrItem.SubItems[4].Text);
			strLastData = CurrItem.SubItems[3].Text;
			iLen = LastMsg.CANMessage.Len;

			// New values 
			//
			if((NewMsg.MsgType & MsgTypes.MSGTYPE_RTR) != 0)
				strNewData = "Remote Request";
			else
				for(int i=0; i< NewMsg.Len; i++)
					strNewData += string.Format("{0:X2} ",NewMsg.Data[i]);
				
			// Count is updated
			//
			iCount += 1;

			// Set the changes
			//
			if(iLen != NewMsg.Len)
			{
				iLen = NewMsg.Len;		
				CurrItem.SubItems[2].Text = iLen.ToString();
			}

			if(strLastData != strNewData)
				CurrItem.SubItems[3].Text = strNewData;

			CurrItem.SubItems[4].Text = iCount.ToString();

			
			// Save the new Status of the message
			// NOTE: The objects are saved in the same index position which 
			// they use in the Listview
			//
			NewStatus = new MessageStatus(NewMsg,LastMsg.Position);
			LastMsgsList.RemoveAt(LastMsg.Position);
			LastMsgsList.Insert(LastMsg.Position,NewStatus);			
		}

		private void InsertMsgEntry(TCLightMsg NewMsg)
		{
			string strNewData,strTemp;
			ListViewItem CurrItem;
			MessageStatus CurrMsg;

			strTemp = strNewData = "";

			// Add the new ListView Item with the Type of the message
			//	
			if((NewMsg.MsgType & MsgTypes.MSGTYPE_EXTENDED) != 0)
				strTemp = "EXTENDED";
			else
				strTemp = "STANDARD";

			if((NewMsg.MsgType & MsgTypes.MSGTYPE_RTR) == MsgTypes.MSGTYPE_RTR)
				strTemp += "/RTR";

			CurrItem = lstMessages.Items.Add(strTemp);
			
			
			// We format the ID of the message and show it
			//
			if((NewMsg.MsgType & MsgTypes.MSGTYPE_EXTENDED) != 0)
				CurrItem.SubItems.Add(string.Format("{0:X8}h",NewMsg.ID));
			else
				CurrItem.SubItems.Add(string.Format("{0:X3}h",NewMsg.ID));
				
			// We set the length of the Message
			//
			CurrItem.SubItems.Add(NewMsg.Len.ToString());

			// We format the data of the message. Each data is one 
			// byte of Hexadecimal data			
			//
			if((NewMsg.MsgType & MsgTypes.MSGTYPE_RTR) == MsgTypes.MSGTYPE_RTR)
				strNewData = "Remote Request";
			else
				for(int i=0; i< NewMsg.Len; i++)
					strNewData += string.Format("{0:X2} ",NewMsg.Data[i]);
			
			CurrItem.SubItems.Add(strNewData);

			// The message is the First, so count is 1 and there 
			// is not any time difference between messages.
			//
			CurrItem.SubItems.Add("1");
				
			// We add this status in the last message list
			//
			CurrMsg = new MessageStatus(NewMsg,CurrItem.Index);
			LastMsgsList.Add(CurrMsg); 
		}

		private void ProcessMessage(TCLightMsg MyMsg)
		{
			bool bFound = false;
			MessageStatus CurrMessage;

			// Initialization
			//
			CurrMessage = new MessageStatus();

			// We search if a message (Smae ID and Type) is 
			// already received or if this is a new message
			//
			for(int i=0; i< LastMsgsList.Count; i++)
			{
				CurrMessage = (MessageStatus)LastMsgsList[i];
				if(CurrMessage.CANMessage.ID == MyMsg.ID)
					if(CurrMessage.CANMessage.MsgType == MyMsg.MsgType)
					{
						bFound = true;
						break;
					}
			}
			if(bFound)
				// Messages of this kind are already received; we make an update
				//
				ModifyMsgEntry(CurrMessage,MyMsg);
			else
				// Messages of this kind are not received; we make a new entry
				//
				InsertMsgEntry(MyMsg);
		}
		#endregion

		private void Form1_Load(object sender, System.EventArgs e)
		{
			// Set the standard values in the interface
			//
			cbbHws.SelectedIndex = 0;
			cbbBaudrates.SelectedIndex = 0;
			cbbIO.Text = "0378";
			cbbInterrupt.Text = "7";
			cbbMsgType.SelectedIndex = 0;			
		}

		private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			// If we are reading, we stop to read
			//
			tmrRead.Enabled = false;
		}

		private void cbbHws_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			bool bShowIO;
			HardwareType current;

			current = (HardwareType)cbbHws.SelectedIndex;

			// According with the selection in the Hardware list, 
			// we Enable/Disable the input controls for I/O Address and 
			// Interrupt. (This parameters are NOT necessary for all 
			// hardware types) .
			//
			switch (current)
			{
				case HardwareType.DNG:
					bShowIO = true;
					break;
				case HardwareType.DNP:
					bShowIO = true;
					break;
				case HardwareType.ISA_1CH:
					bShowIO = true;
					break;
				case HardwareType.ISA_2CH:
					bShowIO = true;
					break;
				default:
					bShowIO = false;
					break;
			}
			cbbIO.Enabled = bShowIO;
			cbbInterrupt.Enabled = bShowIO;	

			// According with the selection in the Hardware list, we 
			// Enable/Disable the controls for Get/Set the USB device Number.
			//
			btnGetUsbDevNumber.Enabled = (current == HardwareType.USB_1CH) || (current == HardwareType.USB_2CH);
			btnSetUsbDevNumber.Enabled = btnGetUsbDevNumber.Enabled;
			nudDevNumber.Enabled = btnGetUsbDevNumber.Enabled;		
		}

		private void cbbBaudrates_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			// We save the corresponding Baudrate enumeration 
			// type value for every selected Baudrate from the 
			// list.
			//
			switch(cbbBaudrates.SelectedIndex)
			{
				case 0:
					cbbBaudrates.Tag = Baudrates.BAUD_1M;
					break;
				case 1:
					cbbBaudrates.Tag = Baudrates.BAUD_500K;
					break;
				case 2:
					cbbBaudrates.Tag = Baudrates.BAUD_250K;
					break;				
				case 3:
					cbbBaudrates.Tag = Baudrates.BAUD_125K;
					break;				
				case 4:
					cbbBaudrates.Tag = Baudrates.BAUD_100K;
					break;				
				case 5:
					cbbBaudrates.Tag = Baudrates.BAUD_50K;
					break;				
				case 6:
					cbbBaudrates.Tag = Baudrates.BAUD_20K;					
					break;				
				case 7:					
					cbbBaudrates.Tag = Baudrates.BAUD_10K;
					break;				
				case 8:
					cbbBaudrates.Tag = Baudrates.BAUD_5K;
					break;		
				default:
					cbbBaudrates.Tag = 0;
					break;
			}		
		}

		private void txtID_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
		{
			char chCheck;

			// We convert the Character to its Upper case equivalent
			//
			chCheck = char.ToUpper(e.KeyChar);

			// The Key is the Delete (Backspace) Key
			//
			if(chCheck == 8)
				return;
			// The Key is a number between 0-9
			//
			if((chCheck > 47)&&(chCheck < 58))
				return;
			// The Key is a character between A-F
			//
			if((chCheck > 64)&&(chCheck < 71))
				return;

			// Is neither a number nor a character between A(a) and F(f)
			//
			e.Handled = true;			
		}

		private void txtID_Leave(object sender, System.EventArgs e)
		{
			int TextLength;
			uint MaxValue;

			// calculate the text length and Maximum ID value according
			// with the Message Type
			//
			TextLength = (chbExtended.Checked) ? 8 : 3;
			MaxValue = (chbExtended.Checked) ? (uint)0x1FFFFFF : (uint)0x7FF;

			// The Textbox for the ID is represented with 3 characters for 
			// Standard and 8 characters for extended messages.
			// Therefore if the Length of the text is smaller than TextLength,  
			// we add "0"
			//
			while(txtID.Text.Length != TextLength)
				txtID.Text = ("0" + txtID.Text);
			// Because in this example will be sent only Standard messages
			// we check that the ID is not bigger than 0x7FF
			//
			if(Convert.ToUInt32(txtID.Text, 16) > MaxValue)
				txtID.Text = string.Format("{0:X"+TextLength.ToString()+"}",MaxValue);				
		}

		private void nudLength_ValueChanged(object sender, System.EventArgs e)
		{
			TextBox CurrentTextBox;

			CurrentTextBox = txtData0;

			// We enable so much TextBox Data fields as the length of the
			// message will be, that is the value of the UpDown control.
			// 
			for(int i=0; i< 8; i++)
			{
				CurrentTextBox.Enabled = (i < nudLength.Value)? true : false;
				if(i < 7)
					CurrentTextBox = (TextBox)this.GetNextControl(CurrentTextBox,true);
			}				
		}

		private void txtData0_Leave(object sender, System.EventArgs e)
		{
			TextBox CurrentTextbox;
			
			// all the Textbox Data fields are represented with 2 characters.
			// Therefore if the Length of the text is smaller than 2, we add
			// a "0"
			//
			if(sender.GetType().Name == "TextBox")
			{				
				CurrentTextbox = (TextBox)sender;
				while(CurrentTextbox.Text.Length != 2)
					CurrentTextbox.Text = ("0" + CurrentTextbox.Text);			
			}				
		}

		private void chbExtended_CheckedChanged(object sender, System.EventArgs e)
		{
			uint uiTemp;

			txtID.MaxLength = (chbExtended.Checked)? 8: 3;
			
			// the only way that the text length can be bigger als MaxLength
			// is when the change is from Extended to Standard message Type.
			// We have to handle this and set an ID not bigger than the Maximum
			// ID value for a Standard Message (0x7FF)
			//
			if(txtID.Text.Length > txtID.MaxLength)
			{
				uiTemp = Convert.ToUInt32(txtID.Text, 16);
				txtID.Text = (uiTemp < 0x7FF) ?  string.Format("{0:X3}",uiTemp): "7FF";
			}

			txtID_Leave(this,new EventArgs());			
		}

		private void chbRemote_CheckedChanged(object sender, System.EventArgs e)
		{
			TextBox CurrentTextBox;

			CurrentTextBox = txtData0;

			// We enable so much TextBox Data fields as the length of the
			// message will be, that is the value of the UpDown control.
			// 
			for(int i=0; i< 8; i++)
			{
				CurrentTextBox.Visible = !chbRemote.Checked;
				if(i < 7)
					CurrentTextBox = (TextBox)this.GetNextControl(CurrentTextBox,true);
			}			
		}

		private void rdbStandard_CheckedChanged(object sender, System.EventArgs e)
		{
			uint uiTemp;

			txtIdFrom.MaxLength = (rdbExtended.Checked) ? 8 : 3;
			txtIdTo.MaxLength = txtIdFrom.MaxLength;

			// the only way that the text length can be bigger als MaxLength
			// is when the change is from Extended to Standard message Type.
			// We have to handle this and set an ID not bigger than the Maximum
			// ID value for a Standard Message (0x7FF)
			//
			if (txtIdFrom.Text.Length > txtIdFrom.MaxLength)
			{
				uiTemp = Convert.ToUInt32(txtIdFrom.Text,16);
				txtIdFrom.Text = (uiTemp < 0x7FF) ? string.Format("{0:X3}", uiTemp) : "7FF";
			}
			if (txtIdTo.Text.Length > txtIdTo.MaxLength)
			{
				uiTemp = Convert.ToUInt32(txtIdTo.Text,16);
				txtIdTo.Text = (uiTemp < 0x7FF) ? string.Format("{0:X3}", uiTemp) : "7FF";
			}		
		}

		private void txtIdFrom_Leave(object sender, System.EventArgs e)
		{
			int TextLength;
			uint MaxValue;
			TextBox IdBox;

			IdBox = sender as TextBox;
			// calculate the text length and Maximum ID value according
			// with the Message Type
			//
			TextLength = (rdbExtended.Checked) ? 8 : 3;
			MaxValue = (rdbExtended.Checked) ? (uint)0x1FFFFFFF : (uint)0x7FF;

			// The Textbox for the ID is represented with 3 characters for 
			// Standard and 8 characters for extended messages.
			// Therefore if the Length of the text is smaller than TextLength,  
			// we add "0"
			//
			while (IdBox.Text.Length != TextLength)
				IdBox.Text = ("0" + IdBox.Text);

			// Because in this example will be sent only Standard messages
			// we check that the ID is not bigger than 0x7FF
			//
			if (Convert.ToUInt32(IdBox.Text,16) > MaxValue)
				IdBox.Text = string.Format("{0:X" + TextLength.ToString() + "}", MaxValue);		
		}

		private void txtInfo_DoubleClick(object sender, System.EventArgs e)
		{
			// We clear the Information edit box
			//
			txtInfo.Text = "";		
		}

		private void lstMessages_DoubleClick(object sender, System.EventArgs e)
		{
			lock(this)
			{
				lstMessages.Items.Clear();
				LastMsgsList.Clear();
			}		
		}

		private void tmrRead_Tick(object sender, System.EventArgs e)
		{
			TCLightMsg MyMsg;
			CANResult Res;
		
			// We read at least one time the queue looking for messages.
			// If a message is found, we look again trying to find more.
			// If the queue is empty or an error occurr, we get out from
			// the dowhile statement.
			//			
			do
			{
				// We read the queue looking for messages.
				//
				Res = CANLight.Read(ActiveHardware,out MyMsg);

				// A message was received
				// We process the message(s)
				//
				if(Res == CANResult.ERR_OK)
					ProcessMessage(MyMsg);
			}while(!Convert.ToBoolean(Res & CANResult.ERR_QRCVEMPTY));
		}

		private void btnInfo_Click(object sender, System.EventArgs e)
		{
			string strInfo;
			CANResult Res;

			// We execute the "VersionInfo" function of the PCANLight 
			// using as parameter the Hardware type and a string 
			// variable to get the info.
			// 
			Res = CANLight.VersionInfo((HardwareType)cbbHws.SelectedIndex,out strInfo);
			strInfo = strInfo.Replace("\n","\r\n");

			// The function was successfully executed
			//			
			if(Res == CANResult.ERR_OK)
				// We show the Version Information
				//
				txtInfo.Text = strInfo;
				// An error occurred.  We show the error.
				//			
			else
				txtInfo.Text  = "Error: " + Res.ToString();			
		}

		private void btnInit_Click(object sender, System.EventArgs e)
		{
			CANResult Res;
			
			// According with the active parameters/hardware, we
			// use one of the two possible "Init" PCANLight functions.
			// One is for Plug And Play hardware, and the other for
			// Not P&P.
			//
			if(cbbIO.Enabled)				
				// Not P&P Hardware
				//
				Res = CANLight.Init((HardwareType)cbbHws.SelectedIndex,
					(Baudrates)cbbBaudrates.Tag,
					(FramesType)cbbMsgType.SelectedIndex,
					Convert.ToUInt32(cbbIO.Text, 16),
					Convert.ToByte(cbbInterrupt.Text));
			else
				// P&P Hardware
				//
				Res = CANLight.Init((HardwareType)cbbHws.SelectedIndex,
					(Baudrates)cbbBaudrates.Tag,
					(FramesType)cbbMsgType.SelectedIndex);

			// The Hardware was successfully initiated
			//
			if(Res == CANResult.ERR_OK)
			{
				// We save the hardware type which is currently 
				// initiated
				//
				ActiveHardware = (HardwareType)cbbHws.SelectedIndex;

				// We start to read from the CAN Queue
				//
				tmrRead.Enabled = true;
				// We show the information of the configured 
				// and initiated hardware
				//
				btnInit.Enabled = !(btnWrite.Enabled = btnSetFilter.Enabled = btnResetFilter.Enabled = btnRelease.Enabled = true);
				txtInfo.Text = "Active Hardware: " + cbbHws.Text;
				txtInfo.Text += "\r\nBaud Rate: " + cbbBaudrates.Text;
				txtInfo.Text += "\r\nFrame Type: " + cbbMsgType.Text;
				// If was a no P&P Hardware, we show additional information
				//
				if(cbbIO.Enabled)
				{
					txtInfo.Text += "\r\nI/O Addr.: " + cbbIO.Text + "h";
					txtInfo.Text += "\r\nInterrupt: " + cbbInterrupt.Text;
				}
			}
				// An error occurred.  We show the error.
				//
			else
				txtInfo.Text  = "Error: " + Res.ToString();			
		}

		private void btnRelease_Click(object sender, System.EventArgs e)
		{
			CANResult Res;

			// We stopt to read from tehe CAN Queue
			//
			tmrRead.Enabled = false;

			// We close the active hardware using the 
			// "Close" function of the PCANLight using 
			// as parameter the Hardware type.
			//
			Res = CANLight.Close(ActiveHardware);

			// The Hardware was successfully closed
			//
			if(Res == CANResult.ERR_OK)
				txtInfo.Text = "Hardware was successfully Released.\r\n";
				// An error occurred.  We show the error.
				//			
			else
				txtInfo.Text = "Error: " + Res.ToString();
			
			// We set the varibale of active hardware to None
			// and activate/deactivate the corresponding buttons
			//
			ActiveHardware = (HardwareType)(-1);
			btnInit.Enabled = !(btnWrite.Enabled = btnSetFilter.Enabled = btnResetFilter.Enabled = btnRelease.Enabled = false);		
		}

		private void btnWrite_Click(object sender, System.EventArgs e)
		{
			TCLightMsg MsgToSend;
			TextBox CurrentTextBox;		
			CANResult Res;

			// We create a TCLightMsg message structure 
			//
			MsgToSend = new TCLightMsg();

			// We configurate the Message.  The ID (max 0x1FF),
			// Length of the Data, Message Type (Standard in 
			// this example) and die data
			//
			MsgToSend.ID = Convert.ToUInt32(txtID.Text, 16);
			MsgToSend.Len = Convert.ToByte(nudLength.Value);
			MsgToSend.MsgType = (chbExtended.Checked) ? MsgTypes.MSGTYPE_EXTENDED : MsgTypes.MSGTYPE_STANDARD;
			// If a remote frame will be sent, the data bytes are not important.
			//
			if(chbRemote.Checked)
				MsgToSend.MsgType |= MsgTypes.MSGTYPE_RTR;
			else
			{
				// We get so much data as the Len of the message
				//
				CurrentTextBox = txtData0;
				for(int i=0; i < MsgToSend.Len; i++)
				{
					MsgToSend.Data[i] = Convert.ToByte(Convert.ToUInt32(CurrentTextBox.Text, 16));
					if(i < 7)
						CurrentTextBox = (TextBox)this.GetNextControl(CurrentTextBox,true);
				}
			}

			// The message is sent to the configured hardware
			//
			Res = CANLight.Write(ActiveHardware,MsgToSend);
			
			// The Hardware was successfully sent
			//
			if(Res == CANResult.ERR_OK)
				txtInfo.Text = "Message was successfully SENT.\r\n";
				// An error occurred.  We show the error.
				//			
			else
				txtInfo.Text = "Error: " + Res.ToString();		
		}

		private void btnClose_Click(object sender, System.EventArgs e)
		{
			// We terminate the application
			//
			Close();
		}

		private void btnSetFilter_Click(object sender, System.EventArgs e)
		{
			uint FromID, ToID;
			CANResult Res;

			// The range IDs is read
			//
			FromID = Convert.ToUInt32(txtIdFrom.Text,16);
			ToID = Convert.ToUInt32(txtIdTo.Text,16);
            
			// The desired Filter is set on the configured Hardware
			//
			Res = CANLight.MsgFilter(ActiveHardware,FromID,ToID,(rdbStandard.Checked) ? MsgTypes.MSGTYPE_STANDARD : MsgTypes.MSGTYPE_EXTENDED);

			// The Filter was successfully set
			//
			if (Res == CANResult.ERR_OK)
				txtInfo.Text = "Filter was successfully SET.\r\n";
				// An error occurred.  We show the error.
				//			
			else
				txtInfo.Text = "Error: " + Res.ToString();			
		}

		private void btnResetFilter_Click(object sender, System.EventArgs e)
		{
			CANResult Res;

			// The current Filter on the configured Hardware is reset 
			//
			Res = CANLight.ResetFilter(ActiveHardware);

			// The Filter was successfully reset
			//
			if (Res == CANResult.ERR_OK)
				txtInfo.Text = "Filter was successfully RESET.\r\n";
				// An error occurred.  We show the error.
				//			
			else
				txtInfo.Text = "Error: " + Res.ToString();		
		}

		private void btnGetUsbDevNumber_Click(object sender, System.EventArgs e)
		{
			int i;
			CANResult Res;

			// The USB Device Number will asked 
			//
			Res = CANLight.GetUSBDeviceNr((HardwareType)cbbHws.SelectedIndex, out i);

			// The Device number was got successfully
			//
			if(Res == CANResult.ERR_OK)
				MessageBox.Show("USB Device Number is: " + i, "GetUSBDevNr"); 		
				// An error occurred.  We show the error.
				//			
			else
				MessageBox.Show("Get USB Device Number failed: " + Res.ToString(), "GetUSBDevNr"); 		
		}

		private void btnSetUsbDevNumber_Click(object sender, System.EventArgs e)
		{
			CANResult Res;
			
			// The USB Device Number will set 
			//
			Res = CANLight.SetUSBDeviceNr((HardwareType)cbbHws.SelectedIndex, (int)nudDevNumber.Value);

			// The Device number was set successfully
			//
			if(Res == CANResult.ERR_OK)
				MessageBox.Show("USB Device Number was set", "SetUSBDevNr");
				// An error occurred.  We show the error.
				//
			else
				MessageBox.Show("Set USB Device Number failed: " + Res.ToString(), "SetUSBDevNr");		
		}
	}
}
