c# 實現串口編程-操作LED螢幕

來源:互聯網
上載者:User

標籤:

串口編程主要用到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);        }    }}

上面我把串口的參數固定了,可以根據自己的需求設定自己要傳的參數。

c# 實現串口編程-操作LED螢幕

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.