Last active
March 2, 2020 14:19
-
-
Save arnholm/66e70394e8ba5d7265dc3848213246cd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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