Sylloge
A C# helper library
code/Controls/Serial.cs
Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00018 using System;
00019 using System.Collections.Generic;
00020 using System.ComponentModel;
00021 using System.Drawing;
00022 using System.Data;
00023 using System.Text;
00024 using System.Windows.Forms;
00025 using System.IO.Ports;
00026 
00027 namespace Sylloge.Controls
00028 {
00032     public partial class Serial : UserControl
00033     {
00034         #region Events
00035 
00036         public delegate void DelegateComPortChanged(SerialPort port);
00040         public event DelegateComPortChanged ComPortChanged;
00041         public delegate void DelegateBaudChanged(int baudRate);
00045         public event DelegateBaudChanged BaudChanged;
00046 
00047         private void OnComPortChanged()
00048         {
00049             if (this.ComPortChanged != null) { this.ComPortChanged(this.Port); }
00050         }
00051 
00052         private void OnBaudChanged()
00053         {
00054             if (this.BaudChanged != null) { this.BaudChanged(this.BaudRate); }
00055         }
00056 
00057         #endregion
00058 
00059         #region Class Methods and Properties
00060 
00061         public Serial()
00062         {
00063             InitializeComponent();
00064             foreach (string Port in System.IO.Ports.SerialPort.GetPortNames()) {
00065                 this.c_ComboBoxComPort.Items.Add(Port);
00066             }
00067             if (this.c_ComboBoxComPort.Items.Count == 0) {
00068                 this.c_ComboBoxComPort.Items.Add("NULL");
00069             }
00070             this.c_ComboBoxComPort.SelectedIndex = 0;
00071             this.c_ComboBoxBaudRate.SelectedIndex = 5;
00072         }
00073 
00074         #endregion
00075 
00076         #region Control Event Handlers
00077 
00081         public SerialPort Port
00082         {
00083             get
00084             {
00085                 return new SerialPort(this.c_ComboBoxComPort.SelectedItem.ToString());
00086             }
00087             set
00088             {
00089                 if (value != null) {
00090                     for (int i = 0; i < this.c_ComboBoxComPort.Items.Count; i++) {
00091                         if (this.c_ComboBoxComPort.Items[i].ToString().ToLower() == value.PortName.ToLower()) {
00092                             this.c_ComboBoxComPort.SelectedIndex = i;
00093                             return;
00094                         }
00095                     }
00096                 }
00097                 if (this.c_ComboBoxComPort.Items.Count > 0) {
00098                     this.c_ComboBoxComPort.SelectedIndex = 0;
00099                 }
00100             }
00101         }
00102 
00106         public int BaudRate
00107         {
00108             get
00109             {
00110                 int Value = 9600;
00111                 if (!this.DesignMode && this.c_ComboBoxBaudRate != null && this.c_ComboBoxBaudRate.SelectedItem != null) {
00112                     Value = (int)this.c_ComboBoxBaudRate.SelectedItem; ;
00113                 }
00114                 return Value;
00115             }
00116             set
00117             {
00118                 this.c_ComboBoxBaudRate.SelectedIndex = 5;
00119                 if (!this.DesignMode) {
00120                     for (int Index = 0; Index < this.c_ComboBoxBaudRate.Items.Count - 1; Index++) {
00121                         if (Convert.ToInt32(this.c_ComboBoxBaudRate.Items[Index]) == value) {
00122                             this.c_ComboBoxBaudRate.SelectedIndex = Index;
00123                             break;
00124                         }
00125                     }
00126                 }
00127             }
00128         }
00129 
00130         private void c_ComboBoxComPort_SelectedIndexChanged(object sender, EventArgs e)
00131         {
00132             this.OnComPortChanged();
00133         }
00134 
00135         private void c_ComboBoxBaudRate_SelectedIndexChanged(object sender, EventArgs e)
00136         {
00137             this.OnBaudChanged();
00138         }
00139 
00140         #endregion
00141     }
00142 }
 All Classes Namespaces Files Functions Variables Enumerations Properties Events