C#基础语法知识:https://www.runoob.com/csharp/csharp-tutorial.html
C#基础语法知识:https://www.w3cschool.cn/csharp/

使用窗口程序进行简单的串口测试

案例一、使用帮助类来实现

1、接口帮助类

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

namespace WindowsFormsApp1
{
    public class SerialPortHelper
    {
        private long _receiveByteCount = 0, _sendByteOfCount = 0;
        public long ReceiveByteCount { get { return _receiveByteCount; } set { _receiveByteCount = value; } }
        public long SendByteOfCount { get { return _sendByteOfCount; } set { _sendByteOfCount = value; } }

        SerialPort _serialPort = new SerialPort();

        private DataReceived _received = null;

        public delegate void DataReceived(byte[] data);

        public bool IsOpen { get { return _serialPort.IsOpen; } }

        public static string[] SerialPortNames
        {
            get
            {
                string[] serialports = SerialPort.GetPortNames();
                Array.Sort(serialports, new Comparison<string>(
                    delegate (string x, string y)
                    {
                        if (x.Length > 3 && y.Length > 3)
                        {
                            string index1 = x.Substring(3);
                            string index2 = y.Substring(3);
                            try
                            {
                                int n1 = Convert.ToInt32(index1);
                                int n2 = Convert.ToInt32(index2);
                                return n1 - n2;
                            }
                            catch
                            {
                            }
                        }
                        return 0;
                    }));
                return serialports;
            }
        }

        public SerialPortHelper(DataReceived received)
        {
            _received = received;
            _serialPort.NewLine = "\r\n";
            _serialPort.DataReceived += delegate
            {
                System.Threading.Thread.Sleep(50);
                int ReadLength = _serialPort.BytesToRead;
                if (ReadLength > 0)
                {
                    _receiveByteCount += ReadLength;
                    byte[] ReadBuffer = new byte[ReadLength];
                    _serialPort.Read(ReadBuffer, 0, ReadLength);
                    if (_received != null)
                    {
                        _received(ReadBuffer);
                    }
                }
            };
        }

        public void Open(string PortName, int BaudRate)
        {
            _serialPort.PortName = PortName;
            _serialPort.StopBits = StopBits.One;
            _serialPort.Parity = Parity.None;
            _serialPort.DataBits = 8;
            _serialPort.BaudRate = BaudRate;
            _serialPort.Open();
        }

        public void Close()
        {
            _serialPort.Close();
        }

        public void WriteLine(string text)
        {
            if (_serialPort.IsOpen)
            {
                _sendByteOfCount += text.Length;
                _serialPort.WriteLine(text);
            }
        }

        public void Write(byte[] buffer)
        {
            if (_serialPort.IsOpen)
            {
                _serialPort.Write(buffer, 0, buffer.Length);
                _sendByteOfCount += buffer.Length;
            }
        }
    }
}
View Code

相关文章:

  • 2021-07-27
  • 2021-07-18
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-10-31
  • 2022-12-23
  • 2021-09-09
  • 2021-09-05
相关资源
相似解决方案