SerialPort not receiving any data

前端 未结 3 938
一生所求
一生所求 2020-11-22 11:40

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

相关标签:
3条回答
  • 2020-11-22 12:10

    I have never worked with VSPE so I'm not sure if that causes the problem. I have worked with a COM port before and I looked up my code. The only main difference is the way you declare the event. You have:

    ComPort.DataReceived += OnSerialDataReceived;
    

    I have it like this:

    ComPort.DataReceived += new SerialDataReceivedEventHandler(OnSerialDataReceived);
    

    OnSerialDataReceived is your eventhandler. I'm not sure if this will make any difference, but you can try it. I hope this helps!

    0 讨论(0)
  • 2020-11-22 12:11
        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.

    0 讨论(0)
  • 2020-11-22 12:12

    I had a quite similar problem. In a graphical application (C# win form) I had a class which encapsulate a SerialPort component. The DataReceived event was firing only one time, but then any following data received didn't fire any event. I solved the problem by calling the Close method in my principal form Closed event function. No idea of why that changes anything, but now it's working.

    0 讨论(0)
提交回复
热议问题