Skip to content

Instantly share code, notes, and snippets.

@manashmandal
Last active May 22, 2016 15:53
Show Gist options
  • Save manashmandal/2ea57505cd1e0dc5aa1de63a0621a560 to your computer and use it in GitHub Desktop.
Save manashmandal/2ea57505cd1e0dc5aa1de63a0621a560 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "SerialPort.h"
using namespace std;
//String for getting the output from arduino
char output[MAX_DATA_LENGTH];
/*Portname must contain these backslashes, and remember to
replace the following com port*/
char *port_name = "\\\\.\\COM20";
//String for incoming data
char incomingData[MAX_DATA_LENGTH];
int main()
{
SerialPort arduino(port_name);
if (arduino.isConnected()) cout << "Connection Established" << endl;
else cout << "ERROR, check port name";
while (arduino.isConnected()){
cout << "Write something: \n";
std::string input_string;
//Getting input
getline(cin, input_string);
//Creating a c string
char *c_string = new char[input_string.size() + 1];
//copying the std::string to c string
std::copy(input_string.begin(), input_string.end(), c_string);
//Adding the delimiter
c_string[input_string.size()] = '\n';
//Writing string to arduino
arduino.writeSerialPort(c_string, MAX_DATA_LENGTH);
//Getting reply from arduino
arduino.readSerialPort(output, MAX_DATA_LENGTH);
//printing the output
puts(output);
//freeing c_string memory
delete[] c_string;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment