代码改自:https://msdn.microsoft.com/zh-cn/library/system.io.ports.serialport
1. 使用RS-232线连接两台PC(无RS-232线,可以使用两台虚拟机,并设置串行端口)
2. 在两台PC上都创建C#控制台项目,复制运行下面的代码就可以了
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
using System;
using System.IO.Ports;
using System.Threading;
public class Program
{
static bool _continue;
static SerialPort _serialPort;
public static void Main()
{
string message;
StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
Thread readThread = new Thread(Read);
// 创建串口对象
_serialPort = new SerialPort();
// 设置端口对象的属性
_serialPort.PortName = SetPortName(_serialPort.PortName);
_serialPort.BaudRate = SetPortBaudRate(_serialPort.BaudRate);
_serialPort.Parity = SetPortParity(_serialPort.Parity);
_serialPort.DataBits = SetPortDataBits(_serialPort.DataBits);
_serialPort.StopBits = SetPortStopBits(_seria