Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7159113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:10:35+00:00 2026-05-28T13:10:35+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T13:10:36+00:00Added an answer on May 28, 2026 at 1:10 pm
        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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing a matlab program in which I uses polygons(concave or convex). I need
I am developing a program in which I need to generate a random number
Greetings! I am developing Asp.net application, in which i need to use sqlite as
I am developing a program which sends images from one computer to another (similar
I'm developing a program which allows users to input some information which then gets
I am developing some program in C# which will send the mail using outlook
I am developing a windows CE program for the Motorola MC9090G which is running
I am currently developing a program that uses C#'s Dictionary container (specifically, SortedDictionary). This
I am developing a program where I find myself doing this like this a
first question here. I'm developing a program in C# (.NET 3.5) that displays files

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.