using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO.Ports; using System.Threading; namespace WindowsFormsApplication2 { public class Form1 : Form { static bool _continue; static SerialPort _serialPort; Thread readThread = new Thread(Read); public System.Windows.Forms.ComboBox ComBoxPortName; public System.Windows.Forms.ComboBox ComBoxPortBaudRate; public System.Windows.Forms.ComboBox ComBoxPortParity; public System.Windows.Forms.ComboBox ComBoxPortDataBits; public System.Windows.Forms.ComboBox ComBoxPortStopBits; public System.Windows.Forms.ComboBox ComBoxPortHandshake; public System.Windows.Forms.TextBox txtRecieve; private System.Windows.Forms.Button btnOk; public Form1() { InitializeComponent(); } private void InitializeComponent() { // // ComBoxPortname // this.ComBoxPortName = new System.Windows.Forms.ComboBox(); this.ComBoxPortName.Anchor = ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right); this.ComBoxPortName.DropDownWidth = 280; foreach (string s in SerialPort.GetPortNames()) { this.ComBoxPortName.Items.Add(s); } this.ComBoxPortName.SelectedIndex = 0; this.ComBoxPortName.Location = new System.Drawing.Point(0, 0); this.ComBoxPortName.Size = new System.Drawing.Size(280, 21); this.ComBoxPortName.TabIndex = 7; // // ComBoxPortBaudRate // this.ComBoxPortBaudRate = new System.Windows.Forms.ComboBox(); this.ComBoxPortBaudRate.Anchor = ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right); this.ComBoxPortBaudRate.DropDownWidth = 280; this.ComBoxPortBaudRate.Items.AddRange(new object[] {"75", "110", "134", "150", "300", "600", "1200", "1800", "2400", "4800", "7200", "9600", "14400", "19200", "38400", "57600", "115200", "128000"}); this.ComBoxPortBaudRate.SelectedIndex = 0; this.ComBoxPortBaudRate.Location = new System.Drawing.Point(0, 21); this.ComBoxPortBaudRate.Size = new System.Drawing.Size(280, 21); this.ComBoxPortBaudRate.TabIndex = 7; // // ComBoxPortParity // this.ComBoxPortParity = new System.Windows.Forms.ComboBox(); this.ComBoxPortParity.Anchor = ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right); this.ComBoxPortParity.DropDownWidth = 280; this.ComBoxPortParity.Items.AddRange(new object[] {"Even", "Odd", "None", "Mark", "Space"}); this.ComBoxPortParity.SelectedIndex = 0; this.ComBoxPortParity.Location = new System.Drawing.Point(0, 42); this.ComBoxPortParity.Size = new System.Drawing.Size(280, 21); this.ComBoxPortParity.TabIndex = 7; // // ComBoxPortDataBits // this.ComBoxPortDataBits = new System.Windows.Forms.ComboBox(); this.ComBoxPortDataBits.Anchor = ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right); this.ComBoxPortDataBits.DropDownWidth = 280; this.ComBoxPortDataBits.Items.AddRange(new object[] {"5", "6", "7", "8"}); this.ComBoxPortDataBits.SelectedIndex = 0; this.ComBoxPortDataBits.Location = new System.Drawing.Point(0, 63); this.ComBoxPortDataBits.Size = new System.Drawing.Size(280, 21); this.ComBoxPortDataBits.TabIndex = 7; // // ComBoxPortStopBits // this.ComBoxPortStopBits = new System.Windows.Forms.ComboBox(); this.ComBoxPortStopBits.Anchor = ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right); this.ComBoxPortStopBits.DropDownWidth = 280; this.ComBoxPortStopBits.Items.AddRange(new object[] {"1", "2"}); this.ComBoxPortStopBits.SelectedIndex = 0; this.ComBoxPortStopBits.Location = new System.Drawing.Point(0, 84); this.ComBoxPortStopBits.Size = new System.Drawing.Size(280, 21); this.ComBoxPortStopBits.TabIndex = 7; // // ComBoxPortHandshake // this.ComBoxPortHandshake = new System.Windows.Forms.ComboBox(); this.ComBoxPortHandshake.Anchor = ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right); this.ComBoxPortHandshake.DropDownWidth = 280; this.ComBoxPortHandshake.Items.AddRange(new object[] {"None", "XOnXOff", "RequestToSend", "RequestToSendXOnXOff"}); this.ComBoxPortHandshake.SelectedIndex = 0; this.ComBoxPortHandshake.Location = new System.Drawing.Point(0, 105); this.ComBoxPortHandshake.Size = new System.Drawing.Size(280, 21); this.ComBoxPortHandshake.TabIndex = 7; // // btnBeenden // this.btnOk = new System.Windows.Forms.Button(); this.btnOk.Location = new System.Drawing.Point(8, 248); this.btnOk.Name = "btnOk"; this.btnOk.Size = new System.Drawing.Size(59, 19); this.btnOk.TabIndex = 1; this.btnOk.Text = "Ok"; this.btnOk.UseVisualStyleBackColor = true; this.btnOk.Click += new System.EventHandler(this.btnOk_Click); // // txtrecieve // this.txtRecieve = new System.Windows.Forms.TextBox(); this.txtRecieve.Location = new System.Drawing.Point(0, 126); this.txtRecieve.Size = new System.Drawing.Size(280, 126); this.txtRecieve.TabIndex = 5; this.txtRecieve.Text = ""; // // Form // this.ClientSize = new System.Drawing.Size(292, 273); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.ComBoxPortName, this.ComBoxPortBaudRate, this.ComBoxPortParity, this.ComBoxPortDataBits, this.ComBoxPortStopBits, this.ComBoxPortHandshake, this.txtRecieve, this.btnOk}); this.Text = "ComPort Sample"; } private void btnOk_Click(object sender, System.EventArgs e) { InitializeSerialPort(); OpenSerialPort(); _continue = true; readThread.Start(); } private void InitializeSerialPort() { // Create a new SerialPort object with default settings. _serialPort = new SerialPort(); // Allow the user to set the appropriate properties. _serialPort.PortName = GetPortName(_serialPort.PortName); _serialPort.BaudRate = GetPortBaudRate(_serialPort.BaudRate); _serialPort.Parity = GetPortParity(_serialPort.Parity); _serialPort.DataBits = GetPortDataBits(_serialPort.DataBits); _serialPort.StopBits = GetPortStopBits(_serialPort.StopBits); _serialPort.Handshake = GetPortHandshake(_serialPort.Handshake); } private string GetPortName(string defaultPortName) { string portName; Object selectedItem = ComBoxPortName.SelectedItem; portName = selectedItem.ToString(); return portName; } private int GetPortBaudRate(int defaultPortBaudRate) { string baudRate; Object selectedItem = ComBoxPortBaudRate.SelectedItem; baudRate = selectedItem.ToString(); return int.Parse(baudRate); } private Parity GetPortParity(Parity defaultPortParity) { string parity; Object selectedItem = ComBoxPortParity.SelectedItem; parity = selectedItem.ToString(); return (Parity)Enum.Parse(typeof(Parity), parity); } private int GetPortDataBits(int defaultPortDataBits) { string dataBits; Object selectedItem = ComBoxPortDataBits.SelectedItem; dataBits = selectedItem.ToString(); return int.Parse(dataBits); } private StopBits GetPortStopBits(StopBits defaultPortStopBits) { string stopBits; Object selectedItem = ComBoxPortStopBits.SelectedItem; stopBits = selectedItem.ToString(); return (StopBits)Enum.Parse(typeof(StopBits), stopBits); } private Handshake GetPortHandshake(Handshake defaultPortHandshake) { string handshake; Object selectedItem = ComBoxPortHandshake.SelectedItem; handshake = selectedItem.ToString(); return (Handshake)Enum.Parse(typeof(Handshake), handshake); } private void OpenSerialPort() { _serialPort.Open(); } public static void Read() { while (_continue) { try { string message = _serialPort.ReadLine(); MessageBox.Show( "Selected Item Text: " +message ); txtRecieve.Text = message; } catch (TimeoutException) { } } } } }