1. 程式人生 > 實用技巧 >HashMap初始大小和擴容後的大小?

HashMap初始大小和擴容後的大小?

使用視窗程式進行簡單的串列埠測試

一、介面幫助類

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

二、呼叫方法

 public partial class Form1 : Form
    {
        SerialPortHelper _helper = null;
        int _baudRate = 9600;//特波率
        public Form1()
        {
            InitializeComponent();
            //初始化串列埠號
            InitView();
            //接受資訊
            _helper = new SerialPortHelper(delegate (byte[] data)
            {
                this.Invoke((EventHandler)delegate
                 {
                     StringBuilder readStr = new StringBuilder();
                     foreach (byte b in data)
                     {
                         string temp = string.Format("{0:X2} ", b);
                         readStr.Append(temp);
                     }
                     readStr.Append("已接收\r\n");
                     textBox1.Text = readStr.ToString();
                 }
                );
            });

        }
        //傳送資訊
        private void btn_send_Click(object sender, EventArgs e)
        {
            if (_helper.IsOpen)
            {
                byte[] bytes = convertHexStringToBytes(textBox2.Text);
                _helper.Write(bytes);
            }
            else
            {
                MessageBox.Show("尚未開啟串列埠,請開啟後再發送");
            }
        }

        //初始化
        private void InitView()
        {
            string[] serialnums = SerialPortHelper.SerialPortNames;
            comboBox1.Items.AddRange(serialnums);
        }

        //開關按鈕
        private void btn_open_Click(object sender, EventArgs e)
        {
            if (_helper!=null&&!_helper.IsOpen)
            {
                try
                {
                    _helper.Open(comboBox1.Text, _baudRate);
                    btn_open.Text = "關閉串列埠";
                    btn_open.ForeColor = Color.Red;
                }
                catch (Exception)
                {
                    MessageBox.Show("串列埠關閉失敗");
                }
            }
            else
            {
                try
                {
                    _helper.Close();
                    btn_open.Text = "開啟串列埠";
                    btn_open.ForeColor = Color.Black;
                }
                catch (Exception)
                {
                    MessageBox.Show("串列埠關閉失敗");
                }
            }
        }
        //16進位制格式轉換
        public static byte[] convertHexStringToBytes(string hexString)
        {
            try
            {
                String[] hexBytes = hexString.Split(' ');
                byte[] bytes = new byte[hexBytes.Length];
                for (int i = 0; i < bytes.Length; i++)
                {
                    int value = Convert.ToInt32(hexBytes[i], 16);
                    bytes[i] = Convert.ToByte(value);
                }
                return bytes;
            }
            catch (Exception e3)
            {
                MessageBox.Show("16進位制的格式不對,請重試");
                return null;
            }
        }
    }
View Code

頁面展示:

使用串列埠助手測試: