很简单,是我自己摸出来的,不要用c#自带的serialPort控件,自己定义一个静态实例即可。
public static SerialPort serialPort1 = new SerialPort();
然后连接虚拟串口COM3:
private void bt_connectPlc_Click(object sender, EventArgs e)
{
try
{
if (serialPort1.IsOpen)
{ serialPort1.Close(); }
else
{
serialPort1.PortName = "COM3";
//serialPort1.PortName = ReadTenCom();//设置