using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Threading; namespace WindowsApplication1 { public partial class Form1 : Form { private delegate void SetTextDeleg(string text); string data = string.Empty; public Form1() { InitializeComponent(); string[] ports = System.IO.Ports.SerialPort.GetPortNames(); for (int i = 0; i < ports.Length; i++) { comboBox1.Items.Add(ports[i]); } } private void button1_Click(object sender, EventArgs e) { try { serialPort1.PortName = "COM1"; serialPort1.BaudRate = 9600; serialPort1.DataBits = 8; serialPort1.Parity = Parity.None; serialPort1.StopBits = StopBits.One; serialPort1.Open(); serialPort1.ReadTimeout = 2000; serialPort1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(serialPort1_DataReceived); MessageBox.Show("Success to Connect"); serialPort1.Close(); } catch (Exception ex) { MessageBox.Show("Failed because" + ex.Message); } } private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { Thread.Sleep(500); data = serialPort1.ReadLine(); this.BeginInvoke(new SetTextDeleg(si_DataReceived), new object[] { data }); } private void si_DataReceived(string data) { listBox1.Items.Add(data); }
Quote:serialPort1.Close();
button1_Click
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)