標籤:
串口編程主要用到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螢幕