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 777245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:34:28+00:00 2026-05-14T19:34:28+00:00

Would like to know from all you guys what do you think about my

  • 0

Would like to know from all you guys what do you think about my Serial Wrapper class. Had been a while I’ve been working with serial ports but never shared the code what somekind make me closed to my very own vision.

Would like to know if it’s a good/bad approach, if the interface is enough and what more you see on it.

I know that Stackoverflow is for question but at the same time there’s a lot of very good skilled people here and share code and opinion can also benefit everybody, it’s why I decided to post it anyway.

thanks!

using System.Text;
using System.IO;
using System.IO.Ports;
using System;

namespace Driver
{
    class SerialSingleton
    {
        // The singleton instance reference
        private static SerialSingleton instance = null;

        // System's serial port interface
        private SerialPort serial;

        // Current com port identifier
        private string comPort = null;

        // Configuration parameters
        private int confBaudRate;
        private int confDataBits;
        private StopBits confStopBits;
        private Parity confParityControl;

        ASCIIEncoding encoding = new ASCIIEncoding();

// ==================================================================================
// Constructors

        public static SerialSingleton getInstance()
        {
            if (instance == null)
            {
                instance = new SerialSingleton();
            }
            return instance;
        }

        private SerialSingleton()
        {
            serial = new SerialPort();
        }

// ===================================================================================
// Setup Methods

        public string ComPort
        {
            get { return comPort; }
            set {
                if (value == null)
                {
                    throw new SerialException("Serial port name canot be null.");
                }

                if (nameIsComm(value))
                {
                    close();
                    comPort = value;
                }
                else
                {
                    throw new SerialException("Serial Port '" + value + "' is not a valid com port.");
                }
            }

        }

        public void setSerial(string baudRate, int dataBits, StopBits stopBits, Parity parityControl)
        {
            if (baudRate == null)
            {
                throw new SerialException("Baud rate cannot be null");
            }

            string[] baudRateRef = { "300", "600", "1200", "1800", "2400", "3600", "4800", "7200", "9600", "14400", "19200", "28800", "38400", "57600", "115200" };

            int confBaudRate;
            if (findString(baudRateRef, baudRate) != -1)
            {
                confBaudRate = System.Convert.ToInt32(baudRate);
            }
            else
            {
                throw new SerialException("Baurate parameter invalid.");
            }

            int confDataBits;
            switch (dataBits)
            {
                case 5:
                    confDataBits = 5;
                    break;
                case 6:
                    confDataBits = 6;
                    break;
                case 7:
                    confDataBits = 7;
                    break;
                case 8:
                    confDataBits = 8;
                    break;
                default:
                    throw new SerialException("Databits parameter invalid");
            }

            if (stopBits == StopBits.None)
            {
                throw new SerialException("StopBits parameter cannot be NONE");
            }

            this.confBaudRate = confBaudRate;
            this.confDataBits = confDataBits;
            this.confStopBits = stopBits;
            this.confParityControl = parityControl;
        }

// ==================================================================================

        public string[] PortList
        {
            get {
                return SerialPort.GetPortNames();
            }
        }

        public int PortCount
        {
            get { return SerialPort.GetPortNames().Length; }
        }

// ==================================================================================
// Open/Close Methods


        public void open()
        {
            open(comPort);
        }

        private void open(string comPort) 
        {
            if (isOpen())
            {
                throw new SerialException("Serial Port is Already open");
            }
            else
            {
                if (comPort == null)
                {
                    throw new SerialException("Serial Port not defined. Cannot open");
                }


                bool found = false;
                if (nameIsComm(comPort))
                {
                    string portId;
                    string[] portList = SerialPort.GetPortNames();
                    for (int i = 0; i < portList.Length; i++)
                    {
                        portId = (portList[i]);
                        if (portId.Equals(comPort))
                        {
                            found = true;
                            break;
                        }
                    }
                }
                else
                {
                    throw new SerialException("The com port identifier '" + comPort + "' is not a valid serial port identifier");
                }
                if (!found)
                {
                    throw new SerialException("Serial port '" + comPort + "' not found");
                }

                serial.PortName = comPort;
                try
                {
                    serial.Open();
                }
                catch (UnauthorizedAccessException uaex)
                {
                    throw new SerialException("Cannot open a serial port in use by another application", uaex);
                }

                try
                {
                    serial.BaudRate = confBaudRate;
                    serial.DataBits = confDataBits;
                    serial.Parity = confParityControl;
                    serial.StopBits = confStopBits;
                }
                catch (Exception e)
                {
                    throw new SerialException("Serial port parameter invalid for '" + comPort + "'.\n" + e.Message, e);
                }
            }
        }

        public void close()
        {
            if (serial.IsOpen)
            {
                serial.Close();
            }
        }

// ===================================================================================
// Auxiliary private Methods

        private int findString(string[] set, string search)
        {
            if (set != null)
            {
                for (int i = 0; i < set.Length; i++)
                {
                    if (set[i].Equals(search))
                    {
                        return i;
                    }
                }
            }
            return -1;
        }

        private bool nameIsComm(string name)
        {
            int comNumber;
            int.TryParse(name.Substring(3), out comNumber);
            if (name.Substring(0, 3).Equals("COM"))
            {
                if (comNumber > -1 && comNumber < 256)
                {
                    return true;
                }
            }
            return false;
        }

// =================================================================================
// Device state Methods

        public bool isOpen()
        {
            return serial.IsOpen;
        }

        public bool hasData()
        {
            int amount = serial.BytesToRead;
            if (amount > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

// ==================================================================================
// Input Methods

        public char getChar()
        {
            int data = serial.ReadByte();
            return (char)data;
        }

        public int getBytes(ref byte[] b)
        {
            int size = b.Length;
            char c;
            int counter = 0;
            for (counter = 0; counter < size; counter++)
            {
                if (tryGetChar(out c))
                {
                    b[counter] = (byte)c;
                }
                else
                {
                    break;
                }
            }
            return counter;
        }

        public string getStringUntil(char x)
        {
            char c;
            string response = "";
            while (tryGetChar(out c))
            {
                response = response + c;
                if (c == x)
                {
                    break;
                }
            }
            return response;
        }

        public bool tryGetChar(out char c)
        {
            c = (char)0x00;
            byte[] b = new byte[1];
            long to = 10;
            long ft = System.Environment.TickCount + to;
            while (System.Environment.TickCount < ft)
            {
                if (hasData())
                {
                    int data = serial.ReadByte();
                    c = (char)data;
                    return true;
                }
            }
            return false;
        }

// ================================================================================
// Output Methods

        public void sendString(string data)
        {
            byte[] bytes = encoding.GetBytes(data);
            serial.Write(bytes, 0, bytes.Length);
        }

        public void sendChar(char c)
        {
            char[] data = new char[1];
            data[0] = c;
            serial.Write(data, 0, 1);
        }


        public void sendBytes(byte[] data)
        {
            serial.Write(data, 0, data.Length);
        }

        public void clearBuffer()
        {
            if (serial.IsOpen)
            {
                serial.DiscardInBuffer();
                serial.DiscardOutBuffer();
            }
        }

    }

}
  • 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-14T19:34:29+00:00Added an answer on May 14, 2026 at 7:34 pm

    I’m assuming that you have an app that needs to access a serial port the way other apps use stdin/stdout. If that’s not the case, you should reconsider your use of a singleton for this.

    • The setSerial method doesn’t do anything useful if the serial port is already open. It should throw an exception, change the settings of the open port, or close the port and reopen it with new settings.

    • getInstance, isOpen, and hasData should probably be read-only properties rather than methods.

    • sendString, sendChar, and sendBytes could all be different overloads of a send method.

    • tryGetChar has a busy loop. That’s very bad. Either use ReadTimeout or have the reading thread wait on an event with a timeout and signal the event from a DataReceived handler.

    • You should consider having a send timeout mechanism.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know if I migrate from Windows Server 2003 to Windows
I would like to know the current idle time from user input on a
I would like to know how to read elements from a const boost::multi_array object.
I would like to know how to get data from preference activity when my
I would like to know how this homepage from Google Docs app is build
I would like to know how I can show the output from this piece
I would like to know if it's possible to download a file from a
I would like to know how could I use JSON for passing data from
I would like to know how you would run a stored procedure from a
I would like to know the regex for splitting up the decimal number from

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.