串行端口不返回任何数据

时间:2018-03-28 12:19:42

标签: c# serial-port read-data

我需要编写一个程序来读取指标中的数据。在计算机上的设备管理器中,指示器定义为USB-ITN并创建虚拟串行端口(COM18)。 enter image description here enter image description here

这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;


namespace SerialPort
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            //configuring the serial port
            serialPort1.PortName = "COM18";
            serialPort1.BaudRate = 9600;
            serialPort1.DataBits = 8;
            serialPort1.Parity = Parity.None;
            serialPort1.StopBits = StopBits.One;
            serialPort1.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
        }


        private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            // Read all the data waiting in the buffer
            string data = serialPort1.ReadExisting();
            rtbTerminal.AppendText(data);
        }


        private void button2_Click(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen)
            {
                serialPort1.Close();
                rtbTerminal.AppendText("Port Closed!\n");
            }
            else
            {
                serialPort1.Open();
                rtbTerminal.AppendText("Port Open!\n");
            }
        }
    }
}

enter image description here

为什么打开串口后无法从指示灯接收数据?

0 个答案:

没有答案