using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using Medivators.Common;
using Medivators.Common.Enums;
using Medivators.Common.Settings;

namespace Medivators.Comm
{
	/// <summary>
	/// Summary description for dlgCommPortSimulation.
	/// </summary>
	public class dlgCommPort: System.Windows.Forms.Form
	{
		private Queue data = new Queue();

		#region declarations

		#region *	custom

		private ICommDevice		_device		= null ;
		private bool			_changing	= false;
		private bool			_dirty		= false;
		private delegate void UpdateDelegate();
		#endregion

		#region *	controls

		internal System.Timers.Timer timerDisplay;
		internal System.Windows.Forms.Button commandCancel;
		internal System.Windows.Forms.RadioButton optSettings;
		internal System.Windows.Forms.Label lblOnOff;
		internal System.Windows.Forms.RadioButton optControl;
		internal System.Windows.Forms.Button commandConnect;
		internal System.Windows.Forms.PictureBox picOnOff;
		internal System.Windows.Forms.Button commandApply;
		internal System.Windows.Forms.Panel pnlControl;
		internal System.Windows.Forms.TextBox txtResponse;
		internal System.Windows.Forms.Button commandWriteByte;
		internal System.Windows.Forms.Button commandWriteString;
		internal System.Windows.Forms.Label Label16;
		internal System.Windows.Forms.TextBox txtWriteByte;
		internal System.Windows.Forms.Label Label15;
		internal System.Windows.Forms.TextBox txtWriteString;
		internal System.Windows.Forms.Label Label14;
		internal System.Windows.Forms.Panel pnlSettings;
		internal System.Windows.Forms.Label Label12;
		internal System.Windows.Forms.Label Label11;
		internal System.Windows.Forms.Label Label10;
		internal System.Windows.Forms.Label Label9;
		internal System.Windows.Forms.ComboBox comboHandShake;
		internal System.Windows.Forms.ComboBox comboStopBits;
		internal System.Windows.Forms.ComboBox comboParity;
		internal System.Windows.Forms.ComboBox comboDataBits;
		internal System.Windows.Forms.ComboBox comboBaudRate;
		internal System.Windows.Forms.ComboBox comboCommPort;
		internal System.Windows.Forms.Label Label8;
		internal System.Windows.Forms.Label Label13;
		private RadioButton radioByte;
		private Panel panel1;
		private RadioButton radioString;
		internal System.Windows.Forms.Button commandOK;

		#endregion

		#endregion

		#region construct/init/dispose

		public dlgCommPort()
		{
			_changing = true;
			InitializeComponent();
			_changing = false;
		}
		protected override void Dispose( bool disposing )
		{
			base.Dispose( disposing );
		}

		#endregion

		#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.timerDisplay = new System.Timers.Timer();
			this.commandCancel = new System.Windows.Forms.Button();
			this.optSettings = new System.Windows.Forms.RadioButton();
			this.lblOnOff = new System.Windows.Forms.Label();
			this.optControl = new System.Windows.Forms.RadioButton();
			this.commandConnect = new System.Windows.Forms.Button();
			this.picOnOff = new System.Windows.Forms.PictureBox();
			this.commandApply = new System.Windows.Forms.Button();
			this.pnlControl = new System.Windows.Forms.Panel();
			this.panel1 = new System.Windows.Forms.Panel();
			this.radioString = new System.Windows.Forms.RadioButton();
			this.radioByte = new System.Windows.Forms.RadioButton();
			this.txtResponse = new System.Windows.Forms.TextBox();
			this.commandWriteByte = new System.Windows.Forms.Button();
			this.commandWriteString = new System.Windows.Forms.Button();
			this.Label16 = new System.Windows.Forms.Label();
			this.txtWriteByte = new System.Windows.Forms.TextBox();
			this.Label15 = new System.Windows.Forms.Label();
			this.txtWriteString = new System.Windows.Forms.TextBox();
			this.Label14 = new System.Windows.Forms.Label();
			this.pnlSettings = new System.Windows.Forms.Panel();
			this.Label12 = new System.Windows.Forms.Label();
			this.Label11 = new System.Windows.Forms.Label();
			this.Label10 = new System.Windows.Forms.Label();
			this.Label9 = new System.Windows.Forms.Label();
			this.comboHandShake = new System.Windows.Forms.ComboBox();
			this.comboStopBits = new System.Windows.Forms.ComboBox();
			this.comboParity = new System.Windows.Forms.ComboBox();
			this.comboDataBits = new System.Windows.Forms.ComboBox();
			this.comboBaudRate = new System.Windows.Forms.ComboBox();
			this.comboCommPort = new System.Windows.Forms.ComboBox();
			this.Label13 = new System.Windows.Forms.Label();
			this.Label8 = new System.Windows.Forms.Label();
			this.commandOK = new System.Windows.Forms.Button();
			((System.ComponentModel.ISupportInitialize)(this.timerDisplay)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.picOnOff)).BeginInit();
			this.pnlControl.SuspendLayout();
			this.panel1.SuspendLayout();
			this.pnlSettings.SuspendLayout();
			this.SuspendLayout();
			// 
			// timerDisplay
			// 
			this.timerDisplay.SynchronizingObject = this;
			this.timerDisplay.Elapsed += new System.Timers.ElapsedEventHandler(this.timerDisplay_Elapsed);
			// 
			// commandCancel
			// 
			this.commandCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
			this.commandCancel.BackColor = System.Drawing.SystemColors.Control;
			this.commandCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.commandCancel.Font = new System.Drawing.Font("Tahoma", 8F);
			this.commandCancel.Location = new System.Drawing.Point(236, 327);
			this.commandCancel.Name = "commandCancel";
			this.commandCancel.Size = new System.Drawing.Size(64, 23);
			this.commandCancel.TabIndex = 4;
			this.commandCancel.Text = "&Cancel";
			this.commandCancel.UseVisualStyleBackColor = false;
			this.commandCancel.Click += new System.EventHandler(this.commandCancel_Click);
			// 
			// optSettings
			// 
			this.optSettings.AllowDrop = true;
			this.optSettings.Appearance = System.Windows.Forms.Appearance.Button;
			this.optSettings.BackColor = System.Drawing.SystemColors.GradientInactiveCaption;
			this.optSettings.Checked = true;
			this.optSettings.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.optSettings.ForeColor = System.Drawing.SystemColors.ControlText;
			this.optSettings.Location = new System.Drawing.Point(4, 7);
			this.optSettings.Name = "optSettings";
			this.optSettings.Size = new System.Drawing.Size(72, 24);
			this.optSettings.TabIndex = 0;
			this.optSettings.TabStop = true;
			this.optSettings.Text = "Settings";
			this.optSettings.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			this.optSettings.UseVisualStyleBackColor = false;
			this.optSettings.CheckedChanged += new System.EventHandler(this.Option_CheckedChanged);
			// 
			// lblOnOff
			// 
			this.lblOnOff.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
			this.lblOnOff.BackColor = System.Drawing.Color.Black;
			this.lblOnOff.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.lblOnOff.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.lblOnOff.ForeColor = System.Drawing.Color.Red;
			this.lblOnOff.Location = new System.Drawing.Point(300, 7);
			this.lblOnOff.Name = "lblOnOff";
			this.lblOnOff.Size = new System.Drawing.Size(64, 24);
			this.lblOnOff.TabIndex = 29;
			this.lblOnOff.Text = "Off";
			this.lblOnOff.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// optControl
			// 
			this.optControl.Appearance = System.Windows.Forms.Appearance.Button;
			this.optControl.BackColor = System.Drawing.SystemColors.GradientInactiveCaption;
			this.optControl.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.optControl.ForeColor = System.Drawing.SystemColors.ControlText;
			this.optControl.Location = new System.Drawing.Point(76, 7);
			this.optControl.Name = "optControl";
			this.optControl.Size = new System.Drawing.Size(72, 24);
			this.optControl.TabIndex = 1;
			this.optControl.Text = "Control";
			this.optControl.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			this.optControl.UseVisualStyleBackColor = false;
			this.optControl.CheckedChanged += new System.EventHandler(this.Option_CheckedChanged);
			// 
			// commandConnect
			// 
			this.commandConnect.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
			this.commandConnect.BackColor = System.Drawing.SystemColors.Control;
			this.commandConnect.ForeColor = System.Drawing.SystemColors.ControlText;
			this.commandConnect.Location = new System.Drawing.Point(236, 7);
			this.commandConnect.Name = "commandConnect";
			this.commandConnect.Size = new System.Drawing.Size(64, 23);
			this.commandConnect.TabIndex = 2;
			this.commandConnect.Text = "Open";
			this.commandConnect.UseVisualStyleBackColor = false;
			this.commandConnect.Click += new System.EventHandler(this.commandConnect_Click);
			// 
			// picOnOff
			// 
			this.picOnOff.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
			this.picOnOff.BackColor = System.Drawing.Color.Black;
			this.picOnOff.Location = new System.Drawing.Point(316, 7);
			this.picOnOff.Name = "picOnOff";
			this.picOnOff.Size = new System.Drawing.Size(22, 20);
			this.picOnOff.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
			this.picOnOff.TabIndex = 30;
			this.picOnOff.TabStop = false;
			// 
			// commandApply
			// 
			this.commandApply.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
			this.commandApply.BackColor = System.Drawing.SystemColors.Control;
			this.commandApply.Enabled = false;
			this.commandApply.Font = new System.Drawing.Font("Tahoma", 8F);
			this.commandApply.Location = new System.Drawing.Point(300, 327);
			this.commandApply.Name = "commandApply";
			this.commandApply.Size = new System.Drawing.Size(64, 23);
			this.commandApply.TabIndex = 5;
			this.commandApply.Text = "&Apply";
			this.commandApply.UseVisualStyleBackColor = false;
			this.commandApply.Click += new System.EventHandler(this.commandApply_Click);
			// 
			// pnlControl
			// 
			this.pnlControl.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
						| System.Windows.Forms.AnchorStyles.Left)
						| System.Windows.Forms.AnchorStyles.Right)));
			this.pnlControl.Controls.Add(this.panel1);
			this.pnlControl.Controls.Add(this.txtResponse);
			this.pnlControl.Controls.Add(this.commandWriteByte);
			this.pnlControl.Controls.Add(this.commandWriteString);
			this.pnlControl.Controls.Add(this.Label16);
			this.pnlControl.Controls.Add(this.txtWriteByte);
			this.pnlControl.Controls.Add(this.Label15);
			this.pnlControl.Controls.Add(this.txtWriteString);
			this.pnlControl.Controls.Add(this.Label14);
			this.pnlControl.Location = new System.Drawing.Point(4, 39);
			this.pnlControl.Name = "pnlControl";
			this.pnlControl.Size = new System.Drawing.Size(360, 288);
			this.pnlControl.TabIndex = 6;
			// 
			// panel1
			// 
			this.panel1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
			this.panel1.Controls.Add(this.radioString);
			this.panel1.Controls.Add(this.radioByte);
			this.panel1.Location = new System.Drawing.Point(175, 104);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(177, 17);
			this.panel1.TabIndex = 9;
			// 
			// radioString
			// 
			this.radioString.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
			this.radioString.AutoSize = true;
			this.radioString.Checked = true;
			this.radioString.Location = new System.Drawing.Point(15, -1);
			this.radioString.Name = "radioString";
			this.radioString.Size = new System.Drawing.Size(53, 17);
			this.radioString.TabIndex = 8;
			this.radioString.TabStop = true;
			this.radioString.Text = "String";
			this.radioString.UseVisualStyleBackColor = true;
			// 
			// radioByte
			// 
			this.radioByte.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
			this.radioByte.AutoSize = true;
			this.radioByte.Location = new System.Drawing.Point(74, -1);
			this.radioByte.Name = "radioByte";
			this.radioByte.Size = new System.Drawing.Size(105, 17);
			this.radioByte.TabIndex = 4;
			this.radioByte.Text = "Formatted Bytes";
			this.radioByte.UseVisualStyleBackColor = true;
			// 
			// txtResponse
			// 
			this.txtResponse.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
						| System.Windows.Forms.AnchorStyles.Left)
						| System.Windows.Forms.AnchorStyles.Right)));
			this.txtResponse.BackColor = System.Drawing.Color.Black;
			this.txtResponse.Font = new System.Drawing.Font("Courier New", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.txtResponse.ForeColor = System.Drawing.Color.Yellow;
			this.txtResponse.Location = new System.Drawing.Point(8, 120);
			this.txtResponse.Multiline = true;
			this.txtResponse.Name = "txtResponse";
			this.txtResponse.ReadOnly = true;
			this.txtResponse.Size = new System.Drawing.Size(344, 160);
			this.txtResponse.TabIndex = 7;
			// 
			// commandWriteByte
			// 
			this.commandWriteByte.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
			this.commandWriteByte.BackColor = System.Drawing.SystemColors.Control;
			this.commandWriteByte.Font = new System.Drawing.Font("Tahoma", 8F);
			this.commandWriteByte.Location = new System.Drawing.Point(312, 72);
			this.commandWriteByte.Name = "commandWriteByte";
			this.commandWriteByte.Size = new System.Drawing.Size(44, 20);
			this.commandWriteByte.TabIndex = 3;
			this.commandWriteByte.Text = "Write";
			this.commandWriteByte.UseVisualStyleBackColor = false;
			this.commandWriteByte.Click += new System.EventHandler(this.commandWriteByte_Click);
			// 
			// commandWriteString
			// 
			this.commandWriteString.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
			this.commandWriteString.BackColor = System.Drawing.SystemColors.Control;
			this.commandWriteString.Font = new System.Drawing.Font("Tahoma", 8F);
			this.commandWriteString.Location = new System.Drawing.Point(312, 24);
			this.commandWriteString.Name = "commandWriteString";
			this.commandWriteString.Size = new System.Drawing.Size(44, 20);
			this.commandWriteString.TabIndex = 1;
			this.commandWriteString.Text = "Write";
			this.commandWriteString.UseVisualStyleBackColor = false;
			this.commandWriteString.Click += new System.EventHandler(this.commandWriteString_Click);
			// 
			// Label16
			// 
			this.Label16.Location = new System.Drawing.Point(8, 104);
			this.Label16.Name = "Label16";
			this.Label16.Size = new System.Drawing.Size(336, 16);
			this.Label16.TabIndex = 4;
			this.Label16.Text = "Response";
			// 
			// txtWriteByte
			// 
			this.txtWriteByte.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
						| System.Windows.Forms.AnchorStyles.Right)));
			this.txtWriteByte.Location = new System.Drawing.Point(8, 72);
			this.txtWriteByte.MaxLength = 3;
			this.txtWriteByte.Name = "txtWriteByte";
			this.txtWriteByte.Size = new System.Drawing.Size(296, 21);
			this.txtWriteByte.TabIndex = 2;
			// 
			// Label15
			// 
			this.Label15.Location = new System.Drawing.Point(8, 56);
			this.Label15.Name = "Label15";
			this.Label15.Size = new System.Drawing.Size(336, 16);
			this.Label15.TabIndex = 2;
			this.Label15.Text = "Write Byte (0-255)";
			// 
			// txtWriteString
			// 
			this.txtWriteString.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
						| System.Windows.Forms.AnchorStyles.Right)));
			this.txtWriteString.Location = new System.Drawing.Point(8, 24);
			this.txtWriteString.Name = "txtWriteString";
			this.txtWriteString.Size = new System.Drawing.Size(296, 21);
			this.txtWriteString.TabIndex = 0;
			// 
			// Label14
			// 
			this.Label14.Location = new System.Drawing.Point(8, 8);
			this.Label14.Name = "Label14";
			this.Label14.Size = new System.Drawing.Size(336, 16);
			this.Label14.TabIndex = 0;
			this.Label14.Text = "Write String";
			// 
			// pnlSettings
			// 
			this.pnlSettings.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
						| System.Windows.Forms.AnchorStyles.Left)
						| System.Windows.Forms.AnchorStyles.Right)));
			this.pnlSettings.Controls.Add(this.Label12);
			this.pnlSettings.Controls.Add(this.Label11);
			this.pnlSettings.Controls.Add(this.Label10);
			this.pnlSettings.Controls.Add(this.Label9);
			this.pnlSettings.Controls.Add(this.comboHandShake);
			this.pnlSettings.Controls.Add(this.comboStopBits);
			this.pnlSettings.Controls.Add(this.comboParity);
			this.pnlSettings.Controls.Add(this.comboDataBits);
			this.pnlSettings.Controls.Add(this.comboBaudRate);
			this.pnlSettings.Controls.Add(this.comboCommPort);
			this.pnlSettings.Controls.Add(this.Label13);
			this.pnlSettings.Controls.Add(this.Label8);
			this.pnlSettings.Location = new System.Drawing.Point(4, 39);
			this.pnlSettings.Name = "pnlSettings";
			this.pnlSettings.Size = new System.Drawing.Size(360, 288);
			this.pnlSettings.TabIndex = 24;
			// 
			// Label12
			// 
			this.Label12.Location = new System.Drawing.Point(0, 192);
			this.Label12.Name = "Label12";
			this.Label12.Size = new System.Drawing.Size(360, 16);
			this.Label12.TabIndex = 10;
			this.Label12.Text = "Stop Bits";
			// 
			// Label11
			// 
			this.Label11.Location = new System.Drawing.Point(0, 146);
			this.Label11.Name = "Label11";
			this.Label11.Size = new System.Drawing.Size(360, 16);
			this.Label11.TabIndex = 9;
			this.Label11.Text = "Parity";
			// 
			// Label10
			// 
			this.Label10.Location = new System.Drawing.Point(0, 100);
			this.Label10.Name = "Label10";
			this.Label10.Size = new System.Drawing.Size(360, 16);
			this.Label10.TabIndex = 8;
			this.Label10.Text = "Data Bits";
			// 
			// Label9
			// 
			this.Label9.Location = new System.Drawing.Point(0, 54);
			this.Label9.Name = "Label9";
			this.Label9.Size = new System.Drawing.Size(360, 16);
			this.Label9.TabIndex = 7;
			this.Label9.Text = "Baud Rate";
			// 
			// comboHandShake
			// 
			this.comboHandShake.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
						| System.Windows.Forms.AnchorStyles.Right)));
			this.comboHandShake.Location = new System.Drawing.Point(0, 254);
			this.comboHandShake.Name = "comboHandShake";
			this.comboHandShake.Size = new System.Drawing.Size(360, 21);
			this.comboHandShake.TabIndex = 5;
			this.comboHandShake.SelectedIndexChanged += new System.EventHandler(this.SettingComboChanged);
			// 
			// comboStopBits
			// 
			this.comboStopBits.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
						| System.Windows.Forms.AnchorStyles.Right)));
			this.comboStopBits.Location = new System.Drawing.Point(0, 208);
			this.comboStopBits.Name = "comboStopBits";
			this.comboStopBits.Size = new System.Drawing.Size(360, 21);
			this.comboStopBits.TabIndex = 4;
			this.comboStopBits.SelectedIndexChanged += new System.EventHandler(this.SettingComboChanged);
			// 
			// comboParity
			// 
			this.comboParity.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
						| System.Windows.Forms.AnchorStyles.Right)));
			this.comboParity.Location = new System.Drawing.Point(0, 162);
			this.comboParity.Name = "comboParity";
			this.comboParity.Size = new System.Drawing.Size(360, 21);
			this.comboParity.TabIndex = 3;
			this.comboParity.SelectedIndexChanged += new System.EventHandler(this.SettingComboChanged);
			// 
			// comboDataBits
			// 
			this.comboDataBits.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
						| System.Windows.Forms.AnchorStyles.Right)));
			this.comboDataBits.Location = new System.Drawing.Point(0, 116);
			this.comboDataBits.Name = "comboDataBits";
			this.comboDataBits.Size = new System.Drawing.Size(360, 21);
			this.comboDataBits.TabIndex = 2;
			this.comboDataBits.SelectedIndexChanged += new System.EventHandler(this.SettingComboChanged);
			// 
			// comboBaudRate
			// 
			this.comboBaudRate.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
						| System.Windows.Forms.AnchorStyles.Right)));
			this.comboBaudRate.Location = new System.Drawing.Point(0, 70);
			this.comboBaudRate.Name = "comboBaudRate";
			this.comboBaudRate.Size = new System.Drawing.Size(360, 21);
			this.comboBaudRate.TabIndex = 1;
			this.comboBaudRate.SelectedIndexChanged += new System.EventHandler(this.SettingComboChanged);
			// 
			// comboCommPort
			// 
			this.comboCommPort.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
						| System.Windows.Forms.AnchorStyles.Right)));
			this.comboCommPort.Location = new System.Drawing.Point(0, 24);
			this.comboCommPort.Name = "comboCommPort";
			this.comboCommPort.Size = new System.Drawing.Size(360, 21);
			this.comboCommPort.TabIndex = 0;
			this.comboCommPort.SelectedIndexChanged += new System.EventHandler(this.SettingComboChanged);
			// 
			// Label13
			// 
			this.Label13.Location = new System.Drawing.Point(0, 238);
			this.Label13.Name = "Label13";
			this.Label13.Size = new System.Drawing.Size(360, 16);
			this.Label13.TabIndex = 11;
			this.Label13.Text = "Handshake";
			// 
			// Label8
			// 
			this.Label8.Location = new System.Drawing.Point(0, 8);
			this.Label8.Name = "Label8";
			this.Label8.Size = new System.Drawing.Size(360, 16);
			this.Label8.TabIndex = 6;
			this.Label8.Text = "Comm Port";
			// 
			// commandOK
			// 
			this.commandOK.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
			this.commandOK.BackColor = System.Drawing.SystemColors.Control;
			this.commandOK.Font = new System.Drawing.Font("Tahoma", 8F);
			this.commandOK.Location = new System.Drawing.Point(164, 327);
			this.commandOK.Name = "commandOK";
			this.commandOK.Size = new System.Drawing.Size(64, 23);
			this.commandOK.TabIndex = 3;
			this.commandOK.Text = "&OK";
			this.commandOK.UseVisualStyleBackColor = false;
			this.commandOK.Click += new System.EventHandler(this.commandOK_Click);
			// 
			// dlgCommPort
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
			this.ClientSize = new System.Drawing.Size(368, 357);
			this.Controls.Add(this.commandCancel);
			this.Controls.Add(this.optSettings);
			this.Controls.Add(this.lblOnOff);
			this.Controls.Add(this.optControl);
			this.Controls.Add(this.commandConnect);
			this.Controls.Add(this.picOnOff);
			this.Controls.Add(this.commandApply);
			this.Controls.Add(this.commandOK);
			this.Controls.Add(this.pnlSettings);
			this.Controls.Add(this.pnlControl);
			this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.Name = "dlgCommPort";
			this.Text = "dlgCommPortSimulation";
			this.Load += new System.EventHandler(this.dlgCommPort_Load);
			((System.ComponentModel.ISupportInitialize)(this.timerDisplay)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.picOnOff)).EndInit();
			this.pnlControl.ResumeLayout(false);
			this.pnlControl.PerformLayout();
			this.panel1.ResumeLayout(false);
			this.panel1.PerformLayout();
			this.pnlSettings.ResumeLayout(false);
			this.ResumeLayout(false);
			this.PerformLayout();

		}
		#endregion

		//imbedded device that this form manages
		internal ICommDevice Device
		{
			get { return this._device; }
			set 
			{
				try
				{
					this._device = value;

					//	wire event handlers
					_device.CommError += new CommErrorEventHandler(this.CommError_Handler);
					_device.DataArrived += new DataArrivedEventHandler(DataReceived_Handler);
					_device.SettingsChanged += new SettingsChangedEventHandler(SettingsChanged_Handler);

					// update form display
					this.Text = _device.GetType().Name;
					ManageConnectDisplay();
					ManageSettingsDisplay();
				}
				catch (Exception SerialEx)
				{
					ErrorHandler.GetInstance().LogException(true, this._device.Port.ToString() + " reports exception opening in Manager form: " + SerialEx.Message);
				}
			}
		}
		// tracks state of user entry
		public bool Dirty
		{
			get { return _dirty; }
			set
			{
				_dirty = value;
				this.commandApply.Enabled = _dirty;

			}
		}

		#region form events

		private void dlgCommPort_Load(object sender, EventArgs e)
		{
			LoadCombos();
			_changing = true;
			this.comboCommPort.SelectedIndex = this.comboCommPort.Items.IndexOf(_device.Port.ToString());
			this.comboBaudRate.SelectedIndex = this.comboBaudRate.Items.IndexOf(_device.Settings.BaudRate.ToString());
			this.comboDataBits.SelectedIndex = this.comboDataBits.Items.IndexOf(_device.Settings.DataBit.ToString());
			this.comboHandShake.SelectedIndex = this.comboHandShake.Items.IndexOf(_device.Settings.HandShake.ToString());
			this.comboParity.SelectedIndex = this.comboParity.Items.IndexOf(_device.Settings.Parity.ToString());
			this.comboStopBits.SelectedIndex = this.comboStopBits.Items.IndexOf(_device.Settings.StopBit.ToString());
			_changing = false;

			this.timerDisplay.Enabled = true;
		}
		private void commandConnect_Click(object sender, System.EventArgs e)
		{
			if ( this._device != null )
			{
				if ( this._device.IsOpen )
					this._device.CloseCommPort();
				else
					this._device.OpenCommPort();				
			}
		}
		private void commandWriteString_Click(object sender, EventArgs e)
		{
			try { this._device.WriteString(this.txtWriteString.Text); }
			catch (Exception ex) { MessageBox.Show(ex.Message); }
		}
		private void commandWriteByte_Click(object sender, EventArgs e)
		{
			try { this._device.WriteByte(Convert.ToByte(this.txtWriteByte.Text)); }
			catch (Exception ex) { MessageBox.Show(ex.Message); }
		}
		private void Option_CheckedChanged(object sender, EventArgs e)
		{
			this.pnlControl.Visible = this.optControl.Checked;
			this.pnlSettings.Visible = this.optSettings.Checked;
		}
		private void timerDisplay_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
		{
			ManageConnectDisplay();
			ManageSettingsDisplay();
		}
		private void commandCancel_Click(object sender, EventArgs e)
		{
			this.Hide();
		}
		private void commandOK_Click(object sender, EventArgs e)
		{
			this.Hide();
		}
		private void SettingComboChanged(object sender, EventArgs e)
		{
			if ( ! _changing )
				this.Dirty = true;
		}
		private void commandApply_Click(object sender, EventArgs e)
		{
			if (_dirty)
			{
				_changing = true;
				CommPortSettings set = new CommPortSettings();
				set.BaudRate = (BaudRates)Enum.Parse(typeof(BaudRates), comboBaudRate.SelectedItem.ToString());
				set.DataBit = (DataBits)Enum.Parse(typeof(DataBits), comboDataBits.SelectedItem.ToString());
				set.HandShake = (HandShakes)Enum.Parse(typeof(HandShakes), comboHandShake.SelectedItem.ToString());
				set.Parity = (Parities)Enum.Parse(typeof(Parities), comboParity.SelectedItem.ToString());
				set.StopBit = (StopBits)Enum.Parse(typeof(StopBits), comboStopBits.SelectedItem.ToString());

				_device.Port = (CommPorts)Enum.Parse(typeof(CommPorts), comboCommPort.SelectedItem.ToString());
				_device.Settings = set;
				_changing = false;
				this.Dirty = false;
			}
		}

		#endregion

		#region data events

		private void CommError_Handler(object sender, CommErrorEventArgs e)
		{
			string errText = this._device.Port.ToString();
			errText += " reports comm error (" ;
			errText += e.CommEvent.ToString() + ") ";
			errText += e.Message;
			ErrorHandler.GetInstance().LogException(true, errText);
		}
		private void DataReceived_Handler(object sender, DataArrivedEventArgs e)		
		{
			data.Enqueue(e.Data);
			UpdateDelegate update = this.UpdateDisplayFromQueue;
			txtResponse.Invoke(update);			
		}
		private void SettingsChanged_Handler(object sender, SettingsChangedEventArgs e)
		{
			ManageSettingsDisplay();
		}
		private void UpdateDisplayFromQueue()
		{
			if ( data.Count > 0 )
				txtResponse.AppendText(Utilities.ByteArrayToString((byte[])data.Dequeue(), radioByte.Checked) + Constants.NewLine);
		}


		#endregion

		#region private helpers

		private void ManageConnectDisplay()
		{

			if ((this._device != null) && this._device.IsOpen)
			{
				this.lblOnOff.Text = "On";
				this.lblOnOff.ForeColor = Color.Lime;
				this.commandConnect.Text = "Close";
			}
			else
			{
				this.lblOnOff.Text = "Off";
				this.lblOnOff.ForeColor = Color.Red;
				this.commandConnect.Text = "Open";
			}
		}
		private void ManageSettingsDisplay()
		{
			bool enableSettings = false;
			if (this._device != null)
			{
				enableSettings = (!_device.IsOpen);
			}
			this.comboCommPort.Enabled	= enableSettings;
			this.comboBaudRate.Enabled	= enableSettings;
			this.comboDataBits.Enabled	= enableSettings;
			this.comboHandShake.Enabled = enableSettings;
			this.comboParity.Enabled	= enableSettings;
			this.comboStopBits.Enabled = enableSettings;
		}
		private void LoadCombos()
		{
			_changing = true;
			this.comboBaudRate.Items.AddRange(Enum.GetNames(typeof(BaudRates)));
			this.comboCommPort.Items.AddRange(Enum.GetNames(typeof(CommPorts)));
			this.comboDataBits.Items.AddRange(Enum.GetNames(typeof(DataBits)));
			this.comboHandShake.Items.AddRange(Enum.GetNames(typeof(HandShakes)));
			this.comboParity.Items.AddRange(Enum.GetNames(typeof(Parities)));
			this.comboStopBits.Items.AddRange(Enum.GetNames(typeof(StopBits)));
			_changing = false;
		}
		
		#endregion
	}
}
