的SerialPort没有收到任何数据 [英] SerialPort not receiving any data

查看:627
本文介绍了的SerialPort没有收到任何数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发程序,它需要与COM端口进行交互。

通过从这个Q&放学习; A: .NET的SerialPort DataReceived事件检索不触发,我让我的code类的。

 命名空间的ConsoleApplication1
{
 类节目
 {
    静态的SerialPort相称;    公共静态无效OnSerialDataReceived(对象发件人,SerialDataReceivedEventArgs参数)
    {
        字符串数据= ComPort.ReadExisting();
        Console.Write(data.Replace(\\ R,\\ n));
    }    静态无效的主要(字串[] args)
    {
        字符串端口=COM4;
        INT波特率= 9600;
        如果(args.Length> = 1)
        {
            端口= ARGS [0];
        }
        如果(args.Length&GT = 2)
        {
            波特= int.Parse(参数[1]);
        }        InitializeComPort(端口,波特);        字符串文本;
        做
        {
            的String [] myString的= System.IO.Ports.SerialPort.GetPortNames();            文字=到Console.ReadLine();
            诠释STX = 0X2;
            INT ETX = 0x3中;
            ComPort.Write(Char.ConvertFromUtf32(STX)+文字+ Char.ConvertFromUtf32(ETX));
        }而(text.ToLower()=Q!);
    }    私有静态无效InitializeComPort(串口,诠释波特)
    {
        COMPORT =新的SerialPort(端口,波特);
        ComPort.PortName =口;
        ComPort.BaudRate =波特;
        ComPort.Parity = Parity.None;
        ComPort.StopBits = StopBits.One;
        ComPort.DataBits = 8;
        ComPort.ReceivedBytesThreshold = 9;
        ComPort.RtsEnable = TRUE;
        ComPort.DtrEnable = TRUE;
        ComPort.Handshake = System.IO.Ports.Handshake.XOnXOff;
        ComPort.DataReceived + = OnSerialDataReceived;
        OpenPort(相称);
    }    公共静态无效OpenPort(的SerialPort相称)
    {
        尝试
        {
            如果(!ComPort.IsOpen)
            {
                ComPort.Open();
            }
        }
        赶上(例外五)
        {
            Ë扔掉;
        }
    }
}
}

我的问题是DataReceived事件检索永远不会被解雇了。

我的程序规范是:


  1. 只是.NET控制台程序

  2. 我用VSPE从 http://www.eterlogic.com

  3. 我的电​​脑COM1和COM2端口了。

  4. 我创建COM2和COM4使用VSPE。

  5. 我从MyString的阵列得到的输出结果(COM1,COM2,COM3,COM4)

但我仍然不知道为什么不解雇 DataReceived 事件。


更新

不幸的是,我仍然不能让以任何方式解雇 DataReceived 事件。

所以,我希望通过我将面临一个办法解决创造了新的项目。

在新项目[只控制台应用程序],我创建了一个类...

 公共类MyTest的
{
    公众的SerialPort SPCOM4;    公共MyTest的()
    {        SPCOM4 =新的SerialPort();
        如果(this.SerialPortOpen(SPCOM4,4))
        {
            this.SendToPort(SPCOM4,COM测试...);
        }    }    私人布尔SerialPortOpen(System.IO.Ports.SerialPort objCom,串PORTNAME)
    {
        布尔blnOpenStatus = FALSE;
        尝试
        {
            objCom.PortName =COM+端口名;
            objCom.BaudRate = 9600;
            objCom.DataBits = 8;            INT SerParity = 2;
            INT SerStop = 0;            开关(SerParity)
            {
                情况下0:
                    objCom.Parity = System.IO.Ports.Parity.Even;
                    打破;
                情况1:
                    objCom.Parity = System.IO.Ports.Parity.Odd;
                    打破;
                案例2:
                    objCom.Parity = System.IO.Ports.Parity.None;
                    打破;
                案例3:
                    objCom.Parity = System.IO.Ports.Parity.Mark;
                    打破;
            }            开关(SerStop)
            {
                情况下0:
                    objCom.StopBits = System.IO.Ports.StopBits.One;
                    打破;
                情况1:
                    objCom.StopBits = System.IO.Ports.StopBits.Two;
                    打破;
            }            objCom.RtsEnable = FALSE;
            objCom.DtrEnable = FALSE;
            objCom.Handshake = System.IO.Ports.Handshake.XOnXOff;
            objCom.Open();
            blnOpenStatus =真;        }
        赶上(异常前)
        {
            扔恩;
        }
        返回blnOpenStatus;
    }    私人布尔SendToPort(System.IO.Ports.SerialPort objCom,字符串strText中)
    {
        尝试
        {
            诠释STX = 0X2;
            INT ETX = 0x3中;            如果(objCom.IsOpen&放大器;&放大器;!strText中=)
            {
                objCom.Write(Char.ConvertFromUtf32(STX)+ strText的+ Char.ConvertFromUtf32(ETX));
            }
        }
        赶上(异常前)
        {
            扔恩;
        }
        返回true;
    }
}

我不知道,我面对的是运气好还是运气不好,因为这个新的类可以使火 DataReceived 事件,这是仍在运行旧的控制台应用程序。这是奇迹,我,我不知道这是怎么发生的。

让我告诉你更详细的,这样你可以给我建议,为更好的办法。


  1. 最后我创造了2台的项目。

  2. 第一个项目是昨天我作为一个问题提出的类。

  3. 第二个项目是在两个项目的运行叫MyTest的这可能使火从第一个项目 DataReceived 事件的类,在同一时间。

任何人都可以给我建议,我怎么能这两个项目合并为一个单一的项目?


解决方案

  ComPort.Handshake = Handshake.None;

问题不在于DataReceived事件检索不火,问题是串行端口接收不到任何数据。有非常,在所有使用无握手很少的串行设备。如果你把它设置为无则驱动程序将不会打开DTR(数据终端就绪)和RTS(请求发送)信号。这是机器关闭(DTR)串口设备间$ P $点,或机器还没有准备好接收数据(RTS)。因此,它不会发送任何东西,你DataReceived事件检索不会触发。

如果您的真正的希望无然后设置DTREnable和RTSEnable属性设置为true。但很可能你想HandShake.RequestToSend因为设备似乎开始重视握手信号。

如果您仍然有问题,然后使用像腻子或超级另一个串口程序,以确保连接和通信参数都不错,设备响应。 Sysinternals的PortMon工具让驾驶员互动的一个低级别的视图,以便可以比较好的VS坏的。

I am developing program which need to interact with COM ports.

By learning from this Q&A: .NET SerialPort DataReceived event not firing, I make my code like that.

namespace ConsoleApplication1
{
 class Program
 {
    static SerialPort ComPort;        

    public static void OnSerialDataReceived(object sender, SerialDataReceivedEventArgs args)
    {
        string data = ComPort.ReadExisting();
        Console.Write(data.Replace("\r", "\n"));
    }

    static void Main(string[] args)
    {
        string port = "COM4";
        int baud = 9600;
        if (args.Length >= 1)
        {
            port = args[0];
        }
        if (args.Length >= 2)
        {
            baud = int.Parse(args[1]);
        }

        InitializeComPort(port, baud);

        string text;
        do
        {
            String[] mystring = System.IO.Ports.SerialPort.GetPortNames();

            text = Console.ReadLine();
            int STX = 0x2;
            int ETX = 0x3;
            ComPort.Write(Char.ConvertFromUtf32(STX) + text + Char.ConvertFromUtf32(ETX));
        } while (text.ToLower() != "q");
    }

    private static void InitializeComPort(string port, int baud)
    {
        ComPort = new SerialPort(port, baud);
        ComPort.PortName = port;
        ComPort.BaudRate = baud;
        ComPort.Parity = Parity.None;
        ComPort.StopBits = StopBits.One;
        ComPort.DataBits = 8;
        ComPort.ReceivedBytesThreshold = 9;
        ComPort.RtsEnable = true;
        ComPort.DtrEnable = true;
        ComPort.Handshake = System.IO.Ports.Handshake.XOnXOff;
        ComPort.DataReceived += OnSerialDataReceived;            
        OpenPort(ComPort);            
    }

    public static void OpenPort(SerialPort ComPort)
    {   
        try
        {   
            if (!ComPort.IsOpen)
            {
                ComPort.Open();                    
            }
        }
        catch (Exception e)
        {
            throw e;
        }
    }
}
}

My problem is DataReceived event never gets fired.

My program specifications are:

  1. Just .net console programming
  2. I use VSPE from http://www.eterlogic.com
  3. My computer has COM1 and COM2 ports already.
  4. I created COM2 and COM4 by using VSPE.
  5. I get output result from mystring array (COM1, COM2, COM3, COM4)

But I still don't know why DataReceived event is not fired.


Updated

Unfortunately, I still could not make to fire DataReceived event in any way.

So, I created new project by hoping that I will face a way to solve.

At that new project [just console application], I created a class...

public class MyTest
{
    public SerialPort SPCOM4;

    public MyTest()
    {

        SPCOM4 = new SerialPort();
        if(this.SerialPortOpen(SPCOM4, "4"))
        {
            this.SendToPort(SPCOM4, "com test...");
        }

    }

    private bool SerialPortOpen(System.IO.Ports.SerialPort objCom, string portName)
    {
        bool blnOpenStatus = false;
        try
        {
            objCom.PortName = "COM" + portName;
            objCom.BaudRate = 9600;
            objCom.DataBits = 8;

            int SerParity = 2;
            int SerStop = 0;

            switch (SerParity)
            {
                case 0:
                    objCom.Parity = System.IO.Ports.Parity.Even;
                    break;
                case 1:
                    objCom.Parity = System.IO.Ports.Parity.Odd;
                    break;
                case 2:
                    objCom.Parity = System.IO.Ports.Parity.None;
                    break;
                case 3:
                    objCom.Parity = System.IO.Ports.Parity.Mark;
                    break;
            }

            switch (SerStop)
            {
                case 0:
                    objCom.StopBits = System.IO.Ports.StopBits.One;
                    break;
                case 1:
                    objCom.StopBits = System.IO.Ports.StopBits.Two;
                    break;
            }

            objCom.RtsEnable = false;
            objCom.DtrEnable = false;
            objCom.Handshake = System.IO.Ports.Handshake.XOnXOff;
            objCom.Open();
            blnOpenStatus = true;

        }
        catch (Exception ex)
        {
            throw ex;
        }
        return blnOpenStatus;
    }

    private bool SendToPort(System.IO.Ports.SerialPort objCom, string strText)
    {
        try
        {
            int STX = 0x2;
            int ETX = 0x3;

            if (objCom.IsOpen && strText != "")
            {
                objCom.Write(Char.ConvertFromUtf32(STX) + strText + Char.ConvertFromUtf32(ETX));
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return true;
    }
}

I am not sure that I face good luck or bad luck because this new class could make fire DataReceived event which is from older console application that is still running. It is miracle to me which I have no idea how this happen.

Let me tell you more detail so that you could give me suggestion for better way.

  1. Finally I created 2 console projects.
  2. First project is the class which I posted as a question yesterday.
  3. Second project is the class called MyTest which could make fire DataReceived event from First project, at the same time when two of the project is running.

Could anyone give me suggestions on how could I combine these two projects as a single project?

解决方案

    ComPort.Handshake = Handshake.None;

The problem is not that the DataReceived event doesn't fire, the problem is that the serial port isn't receiving any data. There are very, very few serial devices that use no handshaking at all. If you set it to None then the driver won't turn on the DTR (Data Terminal Ready) and RTS (Request To Send) signals. Which a serial port device interprets as "the machine is turned off (DTR)" or "the machine isn't ready to receive data (RTS)". So it won't send anything and your DataReceived event won't fire.

If you really want None then set the DTREnable and RTSEnable properties to true. But it is likely you want HandShake.RequestToSend since the device appears to be paying attention to the handshake signals.

If you still have trouble then use another serial port program like Putty or HyperTerminal to ensure the connection and communication parameters are good and the device is responsive. SysInternals' PortMon utility gives a low-level view of the driver interaction so you can compare good vs bad.

这篇关于的SerialPort没有收到任何数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆