Giter VIP home page Giter VIP logo

serial_port's Introduction

C++ Platform MIT license

SerialPortUtils

A window based serial port library in c++.

Features

  • Get COM port list in firendly names
  • A looper to handle the serial port life cycle

Table of content

Example

#include <serial_port.h>

using namespace SerialPortUtils; // Using namespace is a bad practice. Don't use in your code.

// Get all COM port
std::vector<SerialPortInfo> comPorts = SerialPort::getSerialPortList();
std::cout << comPorts[0].friendlyName << std::endl;

// Open Serial Port
SerialPort serialPort;
serialPort.setBaudRate(9600);
serialPort.open(comPorts[0].port);

// Send ASCII
serialPort.sendASCII("Do your job!!");

// Read ASCII
std::string receivedASCII = serialPort.readASCII();

// Close Serial Port
serialPort.close();

For more details, please refer to the Documentation and Diagram

Requirements

Minimum C++ 14

Installation

Cmake

  1. Create libs folder in you project
  2. Clone this repository into the libs folder
    git clone --recurse-submodules https://github.com/kcwongjoe/serial_port.git
    
  3. Add the code in your project CMakeLists.txt
    # Serial port library
    add_subdirectory(./libs/serial_port)
    target_link_libraries(${PROJECT_NAME}
        PRIVATE
            serial_port
    )
    

For dummy

  1. Copy all files inside src folder and include/serial_port folder to your project.

Looper

Looper is a looping thread worked in detach mode which makes serial port life cycle easy to handle.

Example

#include <serial_port_looper.h>

using namespace SerialPortUtils; // Using namespace is a bad practice. Don't use in your code.

// ****** Create looper ******
SerialPortLooper serialPortLooper;
serialPortLooper.getSerialPort().setBaudRate(9600);
serialPortLooper.setEndOfChar('\n');

// ****** Add processes ******
//  --- Add a start process ---
serialPortLooper.setStartProcess([](std::unique_ptr<SerialPortUtils::SerialPort> &serialport) {
    // Try to connect serial port
    if (!serialport->isOpened())
        serialport->open(1); // Try to open COM1
});

//  --- Add a send process ---
bool sendString = false; // Everytime you set as true, message will be sent. After sent, it will return to false.
serialPortLooper.setSendStringPreProcess([&sendString]() {
    std::string sendAscii;

    // If user set sendString as true, send "Do your job!" to serial port.
    if (sendString) 
    {
        sendAscii = "Do your job!";
        sendString = false;
    }

    return sendAscii;
});

//  --- Add a read string line process --- 
serialPortLooper.setReadStringLineProcess([](std::vector<std::string> buffer) {
    // count the number of line received.
    std::cout << "Number of line read: " + std::to_string(buffer.size()) << std::endl;
});

// ****** Start ******
serialPortLooper.start();

// Send message
sendSring = true;

// ****** Stop ******
serialPortLooper.stop(); // The stop() is work in sync mode. To stop in async, use stop(true)

Flow chart

Looper

Process types

void SerialProcess(std::unique_ptr<SerialPort> &serialPort);
std::pair<unsigned char*, int> SendBytePreProcess();
std::string SendStringPreProcess();
void SendPostProcess(int n);
void ReadByteProcess(unsigned char* buffer, int bufferSize);
void ReadStringProcess(std::string buffer);
void ReadStringLineProcess(std::vector<std::string> buffer);

Default settings

  • BaudRate = 9600
  • Byte Size = 8
  • Stop Bits = 1
  • Parity = No parity
  • Flow Control = None
  • End Of Char = 0
  • Timeout = 50ms

Deploy example codes

  1. Clone this repository

    git clone --recurse-submodules https://github.com/kcwongjoe/serial_port.git
    
  2. Run build.bat in Solution folder

    Type build x86 or build x64

  3. Go to build folder and open visual studio solution. Set serial_port_examples project as Startup project.

serial_port's People

Contributors

crzyrndm avatar kcwongjoe avatar

Stargazers

 avatar

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.