Skip to content

Code-Theft-Auto/Serial-Port

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SerialPort

C++ Library for Serial Communication with following criteria

  • Plain C++ Compatible
  • Windows Supported
  • Include and Run Type Library
  • supports softwareserial
  • supports serial event

Initialization

#include "SerialPort.hpp"
#include <iostream>

const char *portName = "\\\\.\\COM20";

//Declare a global object
SerialPort *arduino;

int main(void)
{
  arduino = new SerialPort(portName);
  std::cout << "Is connected: " << arduino->isConnected() << std::endl;
}

Sending Data

#define DATA_LENGTH 255

#include "SerialPort.hpp"
#include <iostream>

const char *portName = "\\\\.\\COM20";

//Declare a global object
SerialPort *arduino;

//Here '\n' is a delimiter 
const char *sendString = "Hello World\n"; 

int main(void)
{
  arduino = new SerialPort(portName);
  if (arduino->isConnected()){
    bool hasWritten = arduino->writeSerialPort(sendString, DATA_LENGTH);
    if (hasWritten) std::cout << "Data Written Successfully" << std::endl;
    else std::cerr << "Data was not written" << std::endl;
  }
}

Receiving Data

#define DATA_LENGTH 255

#include "SerialPort.hpp"
#include <iostream>

const char *portName = "\\\\.\\COM20";

//Declare a global object
SerialPort *arduino;

char receivedString[DATA_LENGTH];

int main(void)
{
  arduino = new SerialPort(portName);
  if (arduino->isConnected()){
    int hasRead = arduino->readSerialPort(receivedString, DATA_LENGTH);
    if (hasRead) std::cout <<  receivedString << "\n";
    else std::cerr << "Error occured reading data" << "\n";
  }
}

About

C++ Library for Serial Communication with Arduino

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages