如何从串口读取和写入

我刚开始学习如何通过C#GUI从我的硬件发送和接收数据。

任何人都可以写一个详细的如何从串口读取数据?

SerialPort(RS-232串行COM端口)在C#.NET中
本文介绍如何使用.NET中的SerialPort类来读取和写入数据,确定您的机器上可用的串行端口以及如何发送文件。 它甚至涵盖了端口本身的引脚分配。

示例代码:

 using System; using System.IO.Ports; using System.Windows.Forms; namespace SerialPortExample { class SerialPortProgram { // Create the serial port with basic settings private SerialPort port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One); [STAThread] static void Main(string[] args) { // Instatiate this class new SerialPortProgram(); } private SerialPortProgram() { Console.WriteLine("Incoming Data:"); // Attach a method to be called when there // is data waiting in the port's buffer port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived); // Begin communications port.Open(); // Enter an application loop to keep this thread alive Application.Run(); } private void port_DataReceived(object sender, SerialDataReceivedEventArgs e) { // Show all the incoming data in the port's buffer Console.WriteLine(port.ReadExisting()); } } }