串口编程主要用到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); } } }
上面我把串口的参数固定了,可以根据自己的需求设置自己要传的参数。
时间: 2024-10-12 22:50:53