• c#串口通信类代码可以直接调用


    文章首发于浩瀚先森博客

    直接上代码

    public struct SerialPara
        {
            private string portName;
            public string PortNameSetGet
            {
                get { return portName; }
                set { portName = value; }
            }
    
            private int baudRate;
            public int BaudRateSetGet
            {
                get { return baudRate; }
                set { baudRate = value; }
            }
    
            private Parity parity;
            public Parity ParitySetGet
            {
                get { return parity; }
                set { parity = value; }
            }
    
            private int databit;
            public int DatabitSetGet
            {
                get { return databit; }
                set { databit = value; }
            }
    
            private StopBits stopBits;
            public StopBits StopBitsSetGet
            {
                get { return stopBits; }
                set { stopBits = value; }
            }
        }
    
        public class SerialCommClass
        {
            SerialPara sp;
            public SerialCommClass(SerialPara sp_temp)
            {
                this.sp = new SerialPara();
                sp.PortNameSetGet = sp_temp.PortNameSetGet;
                sp.BaudRateSetGet = sp_temp.BaudRateSetGet;
                sp.ParitySetGet = sp_temp.ParitySetGet;
                sp.DatabitSetGet = sp_temp.DatabitSetGet;
                sp.StopBitsSetGet = sp_temp.StopBitsSetGet;
            }
    
            public bool Serial_Connect(ref SerialPort serialPort)
            {
                /*check serial parameter*/
                if(!checkSerialPara(this.sp))
                    return false;
                
                /*Serial Port configuration*/
                serialPort.PortName = sp.PortNameSetGet;
                serialPort.BaudRate = sp.BaudRateSetGet;
                serialPort.DataBits = sp.DatabitSetGet;
                serialPort.Parity = sp.ParitySetGet;
                serialPort.StopBits = sp.StopBitsSetGet;
    
                try
                {
                    serialPort.Open();
                }
                catch (Exception exc)
                {
                    return false;
                }
                return true;
            }
    
            //发送字符串数据    
            public bool SendStringData(ref SerialPort serialPort,string data)
            {
                if (serialPort.IsOpen)
                {
                    serialPort.Write(data);
                    return true;
                }
                else
                {
                    return false;
                }
            }
            
            //发送二进制数据    
            public string SendBytesData(ref SerialPort serialPort, string data)
            {
                //byte[] bytesSend = System.Text.Encoding.Default.GetBytes(data);
                byte[] bytesSend = strToToHexByte(data);
                if (bytesSend == null)
                    return null;
                try
                {
                    serialPort.Write(bytesSend, 0, bytesSend.Length);
                    return bytesSend.Length + "";
                }
                catch (Exception e)
                {
                    return null;
                }
            }
    
            /*check serial parameter*/
            private bool checkSerialPara(SerialPara sp)
            {
                if(sp.PortNameSetGet==null)
                   return false;
                return true;
            }
    
            /// <summary> 
            /// 字符串转16进制字节数组 
            /// </summary> 
            /// <param name="hexString"></param> 
            /// <returns></returns> 
            private static byte[] strToToHexByte(string hexString)
            {
                hexString = hexString.Replace(" ", "");
                if (hexString.Substring(0, 2) == "0x" || hexString.Substring(0, 2) == "0X")
                    hexString = hexString.Substring(2, hexString.Length - 2);
                byte[] returnBytes = new byte[hexString.Length / 2];
                try
                {
                    for (int i = 0; i < returnBytes.Length; i++)
                        returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
                        //returnBytes[returnBytes.Length - i - 1] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
                }
                catch (Exception e)
                {
                    return null;
                }
                return returnBytes;
            } 
        }
    
  • 相关阅读:
    Win7远程连接凭据不工作的诡异问题解决
    pip介绍与使用
    Java Web整合开发(35) -- JPA规范
    爬虫
    零基础自学用Python 3开发网络爬虫
    learn资料
    Linux定时任务Crontab命令详解 转
    VirtualBox Host-only Adapter,Failed to create the host-only adapter 转
    Nginx报 No input file specified. 的问题解决之路 转
    ci上传图片
  • 原文地址:https://www.cnblogs.com/harleygwak1206/p/5856600.html
Copyright © 2020-2023  润新知