Skip to content

Instantly share code, notes, and snippets.

@arnholm
Last active March 2, 2020 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arnholm/66e70394e8ba5d7265dc3848213246cd to your computer and use it in GitHub Desktop.
Save arnholm/66e70394e8ba5d7265dc3848213246cd to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
#include "cf_serial/cf_serial_simple.h"
int main(int argc, char **argv)
{
try {
cf_serial_simple serial("COM8");
string prompt="ping";
while(true) {
// send prompt to Arduino
serial.write_string(prompt);
cout << prompt << " -> ";
// receive reply
string reply;
size_t timeout_ms = 1050;
if(serial.read(reply,timeout_ms)) {
cout << reply << endl;
// send the reply back as new prompt
prompt = reply;
}
else {
cout << "timeout" << endl;
}
}
}
catch(std::exception& ex) {
cout << "Error: " << ex.what() << endl;
}
return 0;
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
if(Serial.available() > 0) {
String input = Serial.readString();
if(input=="ping") Serial.write("pong");
else if(input=="pong") Serial.write("ping");
else Serial.write(" huh?");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment