Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SerialPort not receiving any data

Tags:

c#

serial-port

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?

like image 260
Frank Myat Thu Avatar asked Jan 18 '12 09:01

Frank Myat Thu


1 Answers

    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.

like image 63
Hans Passant Avatar answered Nov 09 '22 06:11

Hans Passant