c# 實現串口編程-操做LED屏幕

串口編程主要用到SerialPort這個類,主要實現對串口發送字節數組而後點陣屏顯示相關信息,其實這個功能很簡單下面給你們把總體思路用流程圖展示以下:、編程

其實總體思路就如流程圖。下面是整個流程圖的一個實現代碼以下:數組

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

namespace portchart
{
    class Serilaztion
    {
        SerialPort _serialPort;
        public Serilaztion(string com)
        {
            _serialPort = new SerialPort();
            _serialPort.PortName = com; 
            _serialPort.BaudRate = 4800; 
            _serialPort.Parity = Parity.None; 
            _serialPort.DataBits = 8; 
            _serialPort.StopBits = StopBits.One; 
            _serialPort.ReadBufferSize = 4096;
            _serialPort.Handshake = Handshake.None;  
            _serialPort.ReadTimeout = 500;
            _serialPort.WriteTimeout = 500;
            _serialPort.Open();
        }
        private static byte[] strToToHexByte(string hexString)
        {
            hexString = hexString.Replace(" ", "");
            if ((hexString.Length % 2) != 0)
                hexString += " ";
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
            return returnBytes;
        } 
        public static string StringToHexString(string s, Encoding encode)
        {
            byte[] b = encode.GetBytes(s);//按照指定編碼將string編程字節數組
            string result = string.Empty;
            for (int i = 0; i < b.Length; i++)//逐字節變爲16進制字符,以%隔開
            {
                result += Convert.ToString(b[i], 16);
                
            }
            return result;
        }
        
          
        public void SendMessage(string message, string com,string type)
        {
            System.Text.Encoding GB2312 = System.Text.Encoding.GetEncoding("GB2312");
            message = message.Trim();
            string kzm = "1B0104"; //開始碼
            string hh = StringToHexString(message, GB2312);
            string js = "0D00"; //結束碼
            string zf = kzm + hh + js;
            string s = "1B010320202020C7EB20C9CF20CFDF202020200D00";

            byte[] by = strToToHexByte(zf);
            byte[] bt = strToToHexByte(s);
       
          
                _serialPort.Write(by, 0, by.Length);
                _serialPort.Write(bt, 0, bt.Length);
        

        }
        public void Close()
        {
            _serialPort.Close();
        }
        public static string SetPortName(string defaultPortName)
        {
            string portName;

            Console.WriteLine("驗證端口:");
            foreach (string s in SerialPort.GetPortNames())
            {
                Console.WriteLine("   {0}", s);
            }

            Console.Write("請輸入Com端口(默認: {0}): ", defaultPortName);
            portName = Console.ReadLine();

            if (portName == "" || !(portName.ToLower()).StartsWith("com"))
            {
                portName = defaultPortName;
            }
            return portName;
        }
    
        public static int SetPortBaudRate(int defaultPortBaudRate)
        {
            string baudRate;

            Console.Write("設置波特率(默認:{0}): ", defaultPortBaudRate);
            baudRate = Console.ReadLine();

            if (baudRate == "")
            {
                baudRate = defaultPortBaudRate.ToString();
            }

            return int.Parse(baudRate);
        }

  
        public static Parity SetPortParity(Parity defaultPortParity)
        {
            string parity;

            Console.WriteLine("驗證奇偶校驗位:");
            foreach (string s in Enum.GetNames(typeof(Parity)))
            {
                Console.WriteLine("   {0}", s);
            }

            Console.Write("進入校驗位 (默認: {0}):", defaultPortParity.ToString(), true);
            parity = Console.ReadLine();

            if (parity == "")
            {
                parity = defaultPortParity.ToString();
            }

            return (Parity)Enum.Parse(typeof(Parity), parity, true);
        }
    
        public static int SetPortDataBits(int defaultPortDataBits)
        {
            string dataBits;
            Console.Write("設置每一個字節的標準數據位長度 (默認: {0}): ", defaultPortDataBits);
            dataBits = Console.ReadLine();
            if (dataBits == "")
            {
                dataBits = defaultPortDataBits.ToString();
            }
            return int.Parse(dataBits.ToUpperInvariant());
        }
      
        public static StopBits SetPortStopBits(StopBits defaultPortStopBits)
        {
            string stopBits;
            Console.WriteLine("設置每一個字節的標準中止位數:");
            foreach (string s in Enum.GetNames(typeof(StopBits)))
            {
                Console.WriteLine("   {0}", s);
            }

       
            stopBits = Console.ReadLine();
            if (stopBits == "")
            {
                stopBits = defaultPortStopBits.ToString();
            }
            return (StopBits)Enum.Parse(typeof(StopBits), stopBits, true);
        }
        public static Handshake SetPortHandshake(Handshake defaultPortHandshake)
        {
            string handshake;

            Console.WriteLine("設置串行端口數據傳輸的握手協議:");
            foreach (string s in Enum.GetNames(typeof(Handshake)))
            {
                Console.WriteLine("   {0}", s);
            }
            Console.Write("端口數據傳輸的握手協議(默認: {0}):", defaultPortHandshake.ToString());
            handshake = Console.ReadLine();
            if (handshake == "")
            {
                handshake = defaultPortHandshake.ToString();
            }
            return (Handshake)Enum.Parse(typeof(Handshake), handshake, true);
        }
    }

}

上面我把串口的參數固定了,能夠根據本身的需求設置本身要傳的參數。編碼

相關文章
相關標籤/搜索