Torna al Thread

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO.Ports; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication2 { public partial class Form1 : Form { private SerialPort _serialPort; public Form1() { InitializeComponent(); _serialPort = new SerialPort("COM3"); _serialPort.BaudRate = 9600; _serialPort.Parity = Parity.None; _serialPort.StopBits = StopBits.One; _serialPort.DataBits = 8; _serialPort.Handshake = Handshake.None; _serialPort.DataReceived += SerialPortOnDataReceived; } private void SerialPortOnDataReceived(object sender, SerialDataReceivedEventArgs serialDataReceivedEventArgs) { var data = _serialPort.ReadExisting(); this.Invoke((Action)delegate { this.textBox1.Text = data; }); } private void button1_Click(object sender, EventArgs e) { this.toolStripStatusLabel1.Text = "Connecting..."; if (!_serialPort.IsOpen) { _serialPort.Open(); } this.toolStripStatusLabel1.Text = string.Format("Receiving data on port {0}...", _serialPort.PortName); } /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing) { if (components != null) { components.Dispose(); } if (_serialPort != null) { _serialPort.Dispose(); } } base.Dispose(disposing); } } }
Copyright © dotNetHell.it 2002-2025
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5