Go Down

Topic: Windows <-> Arduino C++ Library (Read 1 time) previous topic - next topic

Power_Broker

Jun 10, 2019, 01:42 am Last Edit: Jun 10, 2019, 02:09 am by Power_Broker
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):
Code: [Select]
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):
Code: [Select]
#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:
Quote
hi
"The desire that guides me in all I do is the desire to harness the forces of nature to the service of mankind."
   - Nikola Tesla

Power_Broker

#1
Jun 26, 2019, 03:01 am Last Edit: Jun 27, 2019, 02:18 am by Power_Broker
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
Code: [Select]

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):
Code: [Select]

#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;
}
"The desire that guides me in all I do is the desire to harness the forces of nature to the service of mankind."
   - Nikola Tesla

Power_Broker

#2
Jun 27, 2019, 02:16 am Last Edit: Jun 27, 2019, 02:17 am by Power_Broker
Just updated the library so that instead of having to call
Code: [Select]

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

you can simply call
Code: [Select]

while (!Serial<COM_PORT>);


Here is an updated version of the example C++ code:
Code: [Select]


#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;
}
"The desire that guides me in all I do is the desire to harness the forces of nature to the service of mankind."
   - Nikola Tesla

Go Up