C#对serialport 类的封装

实现是来并不难,主要是面向接口编程思想:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClassLibrary1
{
    //串口接口定义
    public interface IComserial
    {
        int ReadBuf(byte[] buf, int offset, int count);
        void WriteBuf(byte[] buf, int offSet, int count);
        string ReadLine();
        void WriteLine(string str);
        string Com_port { get; set; }
        int Com_Speed { get; set; }
        int Read_Timeout { get; set; }
        int Write_Timeout { get; set; }
        string NewLine { get; set; }
        bool Open();
        bool Close();
        bool IsOpen();
    }
}


using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClassLibrary1
{
    public class SerialComEntity : IComserial
    {
        public string Com_port { get; set; }
        public int Com_Speed { get; set; }
        public int Read_Timeout { get; set; }
        public int Write_Timeout { get; set; }
        public string NewLine { get ; set; }

        private SerialPort serialPort;

        ~SerialComEntity()
        { this.Close(); }

        public bool Close()
        {
            if (serialPort != null)
            {
                serialPort.Close();
                serialPort.Dispose();
                serialPort = null;
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool IsOpen()
        {
            return serialPort != null && serialPort.IsOpen;
        }

        public bool Open()
        {
            Close();
            this.serialPort = new SerialPort(Com_port, Com_Speed, Parity.None, 8, StopBits.One);
            serialPort.ReadTimeout = this.Read_Timeout;
            serialPort.WriteTimeout = Write_Timeout;
            if (string.IsNullOrEmpty( this.NewLine))
            {
                serialPort.NewLine = this.NewLine;
            }
            try
            {
                serialPort.Open();
            }
            catch (Exception)
            {
                Close();
                return false;
            }
            return true;
        }

        public int ReadBuf(byte[] buf, int offset, int count)
        {
            if (!this.IsOpen())
            {
                return -1;
            }
            return this.serialPort.Read(buf, offset, count);
        }

        public void WriteBuf(byte[] buf, int offSet, int count)
        {
            if (!this.IsOpen())
            {
                return;
            }
            serialPort.Write(buf, offSet, count);
        }

        public string ReadLine()
        {
            if (!Open())
            {
                return string.Empty;
            }
            return serialPort.ReadLine();
        }

        public void WriteLine(string str)
        {
            if (!Open())
            {
                return ;
            }
            serialPort.Write(str);
        }
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

望天hous

你的鼓励是我最大动力~谢谢啦!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值