Windows <-> Arduino C++ Library

For anyone interested in writing Windows C++ programs that interface with Arduinos, ArduSerial is an easy to use solution!

Direct from the repo README:

This library allows Windows C++ programers to interface with COM ports in a way extremely similar to how Arduino’s interface with UART ports.

The library uses a class called Serial that includes member functions named after Arduino Serial member functions. Below is a list of all member functions used in this Windows C++ library:

  • begin()
  • connected()
  • end()
  • available()
  • read()
  • write()

Member functions read() and write() are also overloaded to handle char arrays.

Example Echo Program (Arduino):

void setup()
{
  Serial.begin(115200); // COM25

  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);
}

void loop()
{
  while(Serial.available())
  {
    Serial.write(Serial.read());
  }
}

Example Echo Program (Windows):

#include "pch.h"
#include "ArduSerial.h"

Serial Serial1;

int main()
{
	Serial1.begin(115200, 25);
	while (!Serial1.connected());

	if (Serial1.connected())
	{
		Serial1.write('h');
		Serial1.write('i');
		Serial1.write('\n');
	}

	while (Serial1.available() < 3);

	while (Serial1.available())
	{
		printf("%c", Serial1.read());
	}

	return 0;
}

Expected PC Output:

hi

Did some updates today to make interfacing with COM ports easier. Now WindowsSerial class mimics the Arduino Serial class even further. For instance if you are trying to access a COM port less than 31, you can simply call

Serial<COM_PORT_NUMBER>.begin(<BAUD>);

To initialize the “serial” port.

ALSO, I’ve added print() and println() functionality.

Here is the updated example C++ code (Arduino echo sketch doesn’t change):

#include "pch.h"
#include <iostream>
#include "ArduSerial.h"



std::string data = "hi";




void setup()
{
 // Serial27 means access Arduino on COM27 ***************
 Serial27.begin(115200);

 std::cout << "Starting..." << std::endl;

 while (!Serial27.connected());

 while (Serial27.available())
   Serial27.read();

 std::cout << "Connected" << std::endl;
}




void loop()
{
 std::cout << "Sending data: " << data << std::endl;
 Serial27.println(data);

 std::cout << "Looking for echo..." << std::endl;
 while (Serial27.available() < data.length());

 std::cout << "Found the following echo:" << std::endl;
 while (Serial27.available())
   printf("%c", Serial27.read());

 std::cout << std::endl << "Waiting before transmitting again..." << std::endl;
 Sleep(1000);
}




int main()
{
 setup();

 while (true)
   loop();

 return 0;
}

Just updated the library so that instead of having to call

while (!Serial<COM_PORT>.connected());

you can simply call

while (!Serial<COM_PORT>);

Here is an updated version of the example C++ code:

#include "pch.h"
#include <iostream>
#include "ArduSerial.h"



std::string data = "hi";




void setup()
{
 // Serial27 means access Arduino on COM27 ***************
 Serial27.begin(115200);

 std::cout << "Starting..." << std::endl;

 while (!Serial27);

 while (Serial27.available())
   Serial27.read();

 std::cout << "Connected" << std::endl;
}




void loop()
{
 std::cout << "Sending data: " << data << std::endl;
 Serial27.println(data);

 std::cout << "Looking for echo..." << std::endl;
 while (Serial27.available() < data.length());

 std::cout << "Found the following echo:" << std::endl;
 while (Serial27.available())
   printf("%c", Serial27.read());

 std::cout << std::endl << "Waiting before transmitting again..." << std::endl;
 Sleep(1000);
}




int main()
{
 setup();

 while (true)
   loop();

 return 0;
}

I’ve just had a look at it as you are promoting your PC library quite heavily :slight_smile:

I had some questions, just for me to learn and understand. And suggestions for improvements.

  1. The function of pch.h (and pch.cpp)?
  2. Why the use of setPort()? You could have set the port directly in the constructor (or the begin method).
  3. No use of try/catch to harden your code. No need for it if somebody disconnects the Arduino?

An improvement could be to add an option to specify the format (e.g. 7E2) and an option to disable the auto-reset of the Arduino with the fDtrControl flag in the dcb.

sterretje: I've just had a look at it as you are promoting your PC library quite heavily :)

Not really, I usually point people to my other libraries like the super usefull serial transfer library. (Robin2 still refuses to add it to his auto-response for all USART related posts, lol)

sterretje: The function of pch.h (and pch.cpp)?

That's honestly an artifact of how I set up my VisualStudio Solution - you don't really need pch.h, but I did include a blank one just in case somebody else uses the same config.

sterretje: Why the use of setPort()? You could have set the port directly in the constructor (or the begin method).

I mean, I call it in the constructor. I also wanted the user to change the port dynamically, outside the constructor if need be. I believe in giving the user as much flexibility as possible without allowing them to be too dangerous :)

sterretje: No use of try/catch to harden your code. No need for it if somebody disconnects the Arduino?

It's been a while since I've used the code myself, but I don't remember it being an issue. If someone runs into that situation and it crashes things, I'll look into it.

sterretje: An improvement could be to add an option to specify the format (e.g. 7E2) and an option to disable the auto-reset of the Arduino with the fDtrControl flag in the dcb.

Interesting, I'll take a look into it. Thanks!