对C#SerialPort.Write()方法的误解

时间:2019-12-23 12:23:38

标签: c# .net serial-port

我的任务是通过代码将输入数据发送到串行端口。我使用.NET Framework 4客户端配置文件。我有一个成功处理扫描仪数据的示例,我想以编程方式进行模拟。似乎C#SerialPort.Write()可以满足要求,但是port_DataReceived处理程序不执行任何操作:

namespace ConsoleApplication_ReadTabel
{
    class SerialPortProgram
    {
        private SerialPort port; 

        string input = "";

        [STAThread]
        static void Main(string[] args)
        {
            new SerialPortProgram();
        }

        private SerialPortProgram()
        {
            port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);

            Console.WriteLine("Incoming Data:");

            port.DataReceived += new
            SerialDataReceivedEventHandler(port_DataReceived);

            port.Open();

            port.Write("ghghgh");

            Console.Read();
        }

        private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            input = port.ReadLine();
            Console.WriteLine(input);
            Console.WriteLine("----");
        }
    }
}

2 个答案:

答案 0 :(得分:0)

问题是Console.Read(); 你可以试试这个

namespace ConsoleApplication_ReadTabel
{
    class SerialPortProgram
    {
        private SerialPort port; 

        string input = "";

        [STAThread]
        static void Main(string[] args)
        {
            new SerialPortProgram();
        }

        private SerialPortProgram()
        {
            port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);

            Console.WriteLine("Incoming Data:");

            port.DataReceived += new
            SerialDataReceivedEventHandler(port_DataReceived);

            port.Open();

            port.Write("ghghgh");

            Console.Read();//when program is waiting here, send something the port and continue

            Thread.Sleep(5000);

            Console.Read();
        }

        private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            input = port.ReadLine();
            Console.WriteLine(input);
            Console.WriteLine("----");
        }
    }
}

答案 1 :(得分:0)

Kunif是正确的。我们不能通过代码(至少使用.NET Framework 4)在同一端口上同时进行读写。因此,决定使用免费软件com0com在2个虚拟端口之间创建连接。第一个调用write()方法,第二个处理输入信号。

class SerialPortProgram
{
    private SerialPort port, port2;

    [STAThread]
    static void Main(string[] args)
    {
        new SerialPortProgram();
    }

    private SerialPortProgram()
    {
        port = new SerialPort("COM3", 9600, Parity.None, 8, StopBits.One);
        port.Open();

        port2 = new SerialPort("COM4", 9600, Parity.None, 8, StopBits.One);           
        port2.DataReceived += new SerialDataReceivedEventHandler(port2_DataReceived);
        port2.Open();

        port.Write("Hello!");

        Console.Read();
    }

    void port2_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        Console.WriteLine("COM4 incoming data:");
        Thread.Sleep(300);
        string inp = port2.ReadExisting();
        Console.WriteLine(inp);
        Console.WriteLine("----");
    }
}