Giter VIP home page Giter VIP logo

godsharp.serialport's Introduction

GodSharp.SerialPort

An easy-to-use .NET SerialPort class.

AppVeyor build status NuGet MyGet

Requirement

.NET Framework >= 3.5

Getting Started

  1. New instance GodSerialPort.
GodSerialPort serial = new GodSerialPort("COM1", 9600,0);
  1. Use DataReceived event with received data action: Action<byte[]>.

Notice:This is not need when you read data by read method.

serial.UseDataReceived(true,(sp,bytes)=>{});
  1. Open SerialPort object.
serial.Open();
  1. Write/Send data.
byte[] bytes = new byte[]{31,32,33,34};
serial.Write(bytes);
serial.Write(bytes,offset:1,count:2);
serial.WriteHexString("7E 48 53 44");
serial.WriteAsciiString("ascii string");
  1. Read data.
byte[] bytes = serial.Read();
string stringAsciiOrHex = serial.ReadString();

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,0);
        gsp.UseDataReceived(true,(sp,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);
    }
}

Notes

1.2.0

  • 1.support .NET Core 2.0.

1.1.2

  • 1.Add GodSerialPort to event action as signature param for initial a list.

1.1.1

  • 1.Add constructor and change the constructor signature.
  • 2.Add PortUtil class.

1.1.0

  • 1.Add UseDataReceived method use to trigger DataReceived event.
  • 2.The read metnod can be used to end character.
  • 3.Add sleep time when try read data.

1.0.1

  • 1.Fix ctor and comments.

1.0.0

  • 1.The first version release.

godsharp.serialport's People

Contributors

seayxu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

godsharp.serialport's Issues

Serial port close method

I saw serial port close method like this if (!serialPort.IsOpen) serialPort.Close();.
correct me if I am wrong. the port need to close if the port is at open state or close state?

for some serial device, maybe need add a sleep

for some serial device, maybe need add a sleep:
byte[] newMsg = HexToByte(msg);
//send the message to the port
comPort.Write(newMsg, 0, newMsg.Length);
Thread.Sleep(100); //fire add this sentence, important
SendEndOfLine();

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.