forked from manashmandal/SerialPort
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
75 lines (60 loc) · 1.69 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include "SerialPort.hpp"
#include <stdio.h>
#include <string.h>
using namespace std;
char* portName = "\\\\.\\COM20";
#define MAX_DATA_LENGTH 255
char incomingData[MAX_DATA_LENGTH];
//Control signals for turning on and turning off the led
//Check arduino code
char ledON[] = "ON\n";
char ledOFF[] = "OFF\n";
//Arduino SerialPort object
SerialPort *arduino;
//Blinking Delay
const unsigned int BLINKING_DELAY = 1000;
//If you want to send data then define "SEND" else comment it out
#define SEND
void exampleReceiveData(void)
{
int readResult = arduino->readSerialPort(incomingData, MAX_DATA_LENGTH);
printf("%s", incomingData);
Sleep(10);
}
void exampleWriteData(unsigned int delayTime)
{
arduino->writeSerialPort(ledON, MAX_DATA_LENGTH);
Sleep(delayTime);
arduino->writeSerialPort(ledOFF, MAX_DATA_LENGTH);
Sleep(delayTime);
}
void autoConnect(void)
{
//better than recusion
//avoid stack overflows
while(1) {
// ui - searching
std::cout << "Searching in progress";
// wait connection
while (!arduino->isConnected()) {
Sleep(100);
std::cout << ".";
arduino = new SerialPort(portName);
}
//Checking if arduino is connected or not
if (arduino->isConnected()) {
std::cout << std::endl << "Connection established at port " << portName << std::endl;
}
#ifdef SEND
while(arduino->isConnected()) exampleWriteData(BLINKING_DELAY);
#else // SEND
while(arduino->isConnected()) exampleReceiveData();
#endif // SEND
}
}
int main()
{
arduino = new SerialPort(portName);
autoConnect();
}