微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C#串口驱动程序包装器类代码和概念质量

想知道你们所有人对你的Serial Wrapper课程有什么看法.曾经有一段时间我一直在使用串行端口,但从来没有共享代码,而某些人让我关闭了我自己的愿景.

想知道这是一个好/坏的方法,如果界面足够,你还能看到更多.

我知道Stackoverflow是有问题的,但同时在这里有很多非常优秀的技术人员,分享代码和意见也可以使每个人受益,这就是我决定发布它的原因.

谢谢!

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,bytes.Length);
        }

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


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

        public void clearBuffer()
        {
            if (serial.IsOpen)
            {
                serial.discardInBuffer();
                serial.discardOutBuffer();
            }
        }

    }

}

解决方法

我假设你有一个应用程序需要访问串口,就像其他应用程序使用stdin / stdout一样.如果情况并非如此,您应该重新考虑使用单身人士.

>如果串行端口已打开,则setSerial方法不会执行任何有用的操作.它应抛出异常,更改打开端口的设置,或关闭端口并使用新设置重新打开它.> getInstance,isOpen和hasData应该是只读属性而不是方法.> sendString,sendChar和sendBytes都可以是send方法的不同重载.> tryGetChar有一个繁忙的循环.那太糟糕了.使用ReadTimeout或让读取线程等待超时的事件,并从DataReceived处理程序发出事件信号.>您应该考虑使用发送超时机制.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐