C # implement serial programming-operate the LED screen,

Source: Internet
Author: User

C # implement serial programming-operate the LED screen,

Serial programming mainly uses the SerialPort class, mainly to send byte arrays to the serial port and then display related information on the dot matrix screen. In fact, this function is very simple. The overall idea is shown in the flowchart below :,

In fact, the overall idea is like a flowchart. The following is an implementation code of the entire flowchart:

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 = Stop Bits. one; _ serialPort. readBufferSize = 4096; _ serialPort. handshake = Handshake. none; _ serialPort. readTimeout = 500; _ serialPort. writeTimeout = 500; _ serialPort. open ();} private static byte [] strtohexbyte (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 programming byte array string result = string according to the specified encoding. empty; for (int I = 0; I <B. length; I ++) // change by byte It is a hexadecimal character separated by % {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"; // start code string hh = StringToHexString (message, GB2312); string js = "0D00 "; // end code string zf = kzm + hh + js; string s = "1B010320202020C7EB20 C9CF20CFDF202020200D00 "; byte [] by = strtohexbyte (zf); byte [] bt = strtohexbyte (s); _ serialPort. write (by, 0,. length); _ serialPort. write (bt, 0, bt. length);} public void Close () {_ serialPort. close ();} public static string SetPortName (string defaultPortName) {string portName; Console. writeLine ("authentication port:"); foreach (string s in SerialPort. getPortNames () {Console. writeLine ("{0}", s);} Conso Le. Write ("Enter the Com port (default: {0}):", defaultPortName); portName = Console. ReadLine (); if (portName = "" |! (PortName. toLower ()). startsWith ("com") {portName = defaultPortName;} return portName;} public static int SetPortBaudRate (int defaportportbaudrate) {string baudRate; Console. write ("set baud rate (default: {0}):", defaultPortBaudRate); baudRate = Console. readLine (); if (baudRate = "") {baudRate = defaultPortBaudRate. toString ();} return int. parse (baudRate);} public static Parity SetPortParity (Parity defaultPortParity) {string parity; Console. writeLine ("Verification parity bit:"); foreach (string s in Enum. getNames (typeof (Parity) {Console. writeLine ("{0}", s);} Console. write ("Enter the check bit (default: {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 defaportportdatabits) {string dataBits; Console. write ("set the length of standard data bits for each byte (default: {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 ("set the standard stop bits for each byte:"); 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 ("set the handshake protocol for serial port data transmission:"); foreach (string s in Enum. getNames (typeof (Handshake) {Console. writeLine ("{0}", s);} Console. write ("handshake protocol for port data transmission (default: {0}):", defaultPortHandshake. toString (); handshake = Console. readLine (); if (handshake = "") {handshake = defaultPortHandshake. toString ();} return (Handshake) Enum. parse (typeof (Handshake), handshake, true );}}}

I fixed the parameters of the serial port above. You can set the parameters you want to transmit as needed.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.