-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerial.cpp
More file actions
145 lines (120 loc) · 2.89 KB
/
Serial.cpp
File metadata and controls
145 lines (120 loc) · 2.89 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "stdafx.h"
#include "Serial.h"
using namespace std;
Serial::Serial()
{
connected = false;
};
Serial::~Serial()
{
if(this->connected)
this->disconnect();
};
void Serial::connect(const char* pName, int BoudRate)
{
com_handle = CreateFile ( L"COM3", //Creating handle. Opening COM port
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if(com_handle == INVALID_HANDLE_VALUE)
{
cout << "Cant connect to device. COM port open failure" << endl;
return;
}
else
{
//cout << "Connection established" << endl; //Debug line
this->connected = true;
}
DCB dcbParam;
if(!GetCommState(com_handle, &dcbParam))
{
cout << "GetCommState failure" << endl; //Debug line
return;
}
else
cout << "GetComState success" << endl; //Setting DCB options
dcbParam.BaudRate=BoudRate;
dcbParam.ByteSize=8;
dcbParam.StopBits=ONESTOPBIT;
dcbParam.Parity=NOPARITY;
if(!SetCommState(com_handle, &dcbParam)) //Setting COM properties
{
cout << "SetComState failure" << endl;
return;
}
else
cout << "SetComState success" << endl;
}
void Serial::disconnect() //Disconnection. Handle release.
{
if(this->connected)
{
CloseHandle(com_handle);
connected = false;
}
}
int Serial::writeLine(char* buff) //Sending bytes to port
{
DWORD BytesSend;
if(!WriteFile(this->com_handle , (void*)buff , sizeof(buff) , &BytesSend , 0))
{
cout<< "Write failure";
}
return BytesSend;
}
int Serial::readLine(int* buff) //Reading Int from port
{
DWORD BytesRead = 0;
do
{
ClearCommError(this->com_handle, &this->errors, &this->status);
}
while(status.cbInQue == 0);
ReadFile(com_handle , (void*)buff , 2 , &BytesRead, 0);
return BytesRead;
}
int Serial::readLine(float* buff) //Reading float from port
{
DWORD BytesRead = 0;
do
{
ClearCommError(this->com_handle, &this->errors, &this->status);
}
while(status.cbInQue == 0);
ReadFile(com_handle , (void*)buff , 4 , &BytesRead, 0);
return BytesRead;
}
void Serial::gatherData() //Starting scan and reading port for new data
{
int angle = 0;
float range = 0;
float x_coord = 0;
float y_coord = 0;
char start = 's';
float dergeesToRadians = 3.14159265 / 180;
this->writeLine(&start); //Sending 's' to start scanning sequence
for(int i = 0; i < 170; i++)
{
this->readLine(&angle); //Reading Int for angle
this->readLine(&range); //Reading float for range
if(range > 1000)
{
x_coord = 0;
y_coord = 0;
}
else
{
x_coord = cos(angle*dergeesToRadians)*range; //Converting range and angle to coordinates
y_coord = -sin(angle*dergeesToRadians)*range;
}
array.push_back(pair<float, float>(x_coord, y_coord)); //Filling array
}
}
pointList Serial::getData()
{
return array;
}