C # Implementation of serial programming-operating LED screen

Source: Internet
Author: User

Serial programming mainly used to serialport this class, the main implementation of the serial port to send a byte array and then dot-matrix screen display related information, in fact, this function is very simple below for everyone to the overall idea with a flowchart to show as follows:,

In fact, the overall idea is like a flowchart. Here is an implementation code for the entire flowchart:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.IO.Ports;usingSystem.Threading;namespaceportchart{classserilaztion {SerialPort _serialport;  PublicSerilaztion (stringcom) {_serialport=NewSerialPort (); _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= -; _serialport.writetimeout= -;        _serialport.open (); }        Private Static byte[] Strtotohexbyte (stringhexstring) {hexstring= Hexstring.replace (" ",""); if((hexstring.length%2) !=0) hexstring+=" "; byte[] Returnbytes =New byte[Hexstring.length/2];  for(inti =0; i < returnbytes.length; i++) Returnbytes[i]= Convert.tobyte (hexstring.substring (i *2,2), -); returnreturnbytes; }          Public Static stringStringtohexstring (strings, Encoding encode) {            byte[] b = encode. GetBytes (s);//encodes a string into a byte array according to the specified encoding            stringresult =string.            Empty;  for(inti =0; i < b.length; i++)//byte to 16 characters, separated by%{result+ = Convert.ToString (B[i], -); }            returnresult; }                           Public voidSendMessage (stringMessagestringComstringtype) {System.Text.Encoding GB2312= System.Text.Encoding.GetEncoding ("GB2312"); Message=message.            Trim (); stringKZM ="1b0104";//Start Code            stringHH =stringtohexstring (message, GB2312); stringJS ="0d00";//End Code            stringZF = Kzm + hh +JS; strings ="1b010320202020c7eb20c9cf20cfdf202020200d00"; byte[] by =Strtotohexbyte (ZF); byte[] bt =Strtotohexbyte (s); _serialport.write (By,0, by.                Length); _serialport.write (BT,0, Bt.                Length); }         Public voidClose () {_serialport.close (); }         Public Static stringSetportname (stringdefaultportname) {            stringPortName; Console.WriteLine ("Verify Port:"); foreach(stringSinchSerialport.getportnames ()) {Console.WriteLine ("{0}", s); } console.write ("Please enter the COM port (default: {0}):", Defaultportname); PortName=Console.ReadLine (); if(PortName = =""|| ! (Portname.tolower ()). StartsWith ("com") ) {PortName=Defaultportname; }            returnPortName; }             Public Static intSetportbaudrate (intdefaultportbaudrate) {            stringbaudrate; Console.Write ("set baud rate (default: {0}):", defaultportbaudrate); BaudRate=Console.ReadLine (); if(BaudRate = ="") {baudrate=defaultportbaudrate.tostring (); }            return int.        Parse (baudrate); }           Public StaticParity setportparity (Parity defaultportparity) {stringparity; Console.WriteLine ("verify parity bit:"); foreach(stringSinchEnum.getnames (typeof(Parity))) {Console.WriteLine ("{0}", s); } console.write ("Enter check digit (default: {0}):", Defaultportparity.tostring (),true); Parity=Console.ReadLine (); if(Parity = ="") {Parity=defaultportparity.tostring (); }            return(Parity) Enum.parse (typeof(Parity), Parity,true); }             Public Static intSetportdatabits (intdefaultportdatabits) {            stringdatabits; Console.Write ("set the standard data bit length for each byte (default: {0}):", defaultportdatabits); DataBits=Console.ReadLine (); if(DataBits = ="") {databits=defaultportdatabits.tostring (); }            return int.        Parse (Databits.toupperinvariant ()); }               Public Staticstopbits setportstopbits (stopbits defaultportstopbits) {stringstopbits; Console.WriteLine ("set the standard number of stop bits per byte:"); foreach(stringSinchEnum.getnames (typeof(stopbits))) {Console.WriteLine ("{0}", s); } stopbits=Console.ReadLine (); if(StopBits = ="") {StopBits=defaultportstopbits.tostring (); }            return(stopbits) Enum.parse (typeof(stopbits), StopBits,true); }         Public StaticHandshake Setporthandshake (handshake defaultporthandshake) {stringhandshake; Console.WriteLine ("to set the handshake protocol for serial port data transfer:"); foreach(stringSinchEnum.getnames (typeof(handshake))) {Console.WriteLine ("{0}", s); } console.write ("Handshake protocol for Port data transfer (default: {0}):", defaultporthandshake.tostring ()); Handshake=Console.ReadLine (); if(Handshake = ="") {Handshake=defaultporthandshake.tostring (); }            return(handshake) Enum.parse (typeof(handshake), handshake,true); }    }}

Above I put the parameters of the serial port fixed, can be set according to their own needs to pass the parameters.

C # Implementation of serial programming-operating LED screen

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.