# GodSharp.SerialPort **Repository Path**: xingchensoft/GodSharp.SerialPort ## Basic Information - **Project Name**: GodSharp.SerialPort - **Description**: Easy to use SerialPort class. - **Primary Language**: C# - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 17 - **Created**: 2017-04-17 - **Last Updated**: 2022-06-01 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # GodSharp.SerialPort Easy to use SerialPort class. [![AppVeyor build status](https://img.shields.io/appveyor/ci/seayxu/godsharp-serialport.svg?label=appveyor&style=flat-square)](https://ci.appveyor.com/project/seayxu/godsharp-serialport/) [![NuGet](https://img.shields.io/nuget/v/GodSharp.SerialPort.svg?label=nuget&style=flat-square)](https://www.nuget.org/packages/GodSharp.SerialPort/) [![MyGet](https://img.shields.io/myget/seay/v/GodSharp.SerialPort.svg?label=myget&style=flat-square)](https://www.myget.org/Package/Details/seay?packageType=nuget&packageId=GodSharp.SerialPort) # Getting Started 1. New instance GodSerialPort. ``` GodSerialPort serial = new GodSerialPort("COM1", 9600); ``` 2. Initialize the GodSerialPort instance with received data action: `Action`. ``` serial.Init((bytes)=>{}); ``` 3. Open SerialPort object. ``` serial.Open(); ``` 4. Write/Send data. ``` byte[] bytes = new byte[]{31,32,33,34}; serial.Write(bytes); serial.WriteAsciiString("ascii string"); serial.WriteHexString("7E 48 53 44"); ``` 5. Parity value. - Parity.Space:0|s|space - Parity.Mark:1|m|mark - Parity.Even:2|e|even - Parity.Odd:3|o|odd - Parity.None:4|n|none 6. StopBits value. - StopBits.None:0|n|none - StopBits.One:1|o|one - StopBits.OnePointFive:3|opf|of|f - StopBits.Two:2|t|two 7. Handshake value. - Handshake.None:0|n|none - Handshake.RequestToSend:1|r|rst - Handshake.RequestToSendXOnXOff:2|rtsxx|rsxx|rtsx|rsx|rx - Handshake.XOnXOff:3|x|xx # Sample ``` class Program { static void Main(string[] args) { Console.Write("input serialport number(only 0-9):"); string read = Console.ReadLine(); bool flag = uint.TryParse(read, out uint num); if (!flag) { Exit(); } GodSerialPort gsp = new GodSerialPort("COM"+num, 9600); gsp.Init((bytes) => { string buffer = string.Join(" ", bytes); Console.WriteLine("receive data:" + buffer); }); flag = gsp.Open(); if (!flag) { Exit(); } Console.WriteLine("serialport opend"); Console.WriteLine("press any thing as data to send,press key 'q' to quit."); string data = null; while (data == null || data.ToLower()!="q") { if (!string.IsNullOrEmpty(data)) { Console.WriteLine("send data:"+data); gsp.WriteAsciiString(data); } data = Console.ReadLine(); } } static void Exit() { Console.WriteLine("press any key to quit."); Console.ReadKey(); Environment.Exit(0); } } ```