-
-
Notifications
You must be signed in to change notification settings - Fork 404
Expand file tree
/
Copy pathos_serial.cpp
More file actions
211 lines (184 loc) · 5.41 KB
/
Copy pathos_serial.cpp
File metadata and controls
211 lines (184 loc) · 5.41 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include <stdlib.h>
#include <string.h>
#include <string>
#include "os_serial.h"
#include "serial.h"
#ifdef _WIN32
OSSerial::OSSerial (const char *port_name)
{
std::string port_name_string (port_name);
// add winapi specific prefix for port name if not provided
if (port_name_string.find (std::string ("COM")) == 0)
{
port_name_string = std::string ("\\\\.\\") + port_name_string;
}
strcpy (this->port_name, port_name_string.c_str ());
port_descriptor = NULL;
}
bool OSSerial::is_port_open ()
{
return (this->port_descriptor != NULL);
}
int OSSerial::open_serial_port ()
{
this->port_descriptor =
CreateFile (this->port_name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if (this->port_descriptor == INVALID_HANDLE_VALUE)
{
return SerialExitCodes::OPEN_PORT_ERROR;
}
return SerialExitCodes::OK;
}
int OSSerial::set_serial_port_settings (int ms_timeout, bool timeout_only)
{
if (!timeout_only)
{
DCB dcb_serial_params = {0};
dcb_serial_params.DCBlength = sizeof (dcb_serial_params);
if (GetCommState (this->port_descriptor, &dcb_serial_params) == 0)
{
CloseHandle (this->port_descriptor);
return SerialExitCodes::GET_PORT_STATE_ERROR;
}
dcb_serial_params.BaudRate = CBR_115200;
dcb_serial_params.ByteSize = 8;
dcb_serial_params.StopBits = ONESTOPBIT;
dcb_serial_params.Parity = NOPARITY;
if (SetCommState (this->port_descriptor, &dcb_serial_params) == 0)
{
CloseHandle (this->port_descriptor);
return SerialExitCodes::SET_PORT_STATE_ERROR;
}
}
COMMTIMEOUTS timeouts = {0};
timeouts.ReadIntervalTimeout = ms_timeout;
timeouts.ReadTotalTimeoutConstant = ms_timeout;
timeouts.ReadTotalTimeoutMultiplier = 100;
timeouts.WriteTotalTimeoutConstant = 50;
timeouts.WriteTotalTimeoutMultiplier = 10;
if (SetCommTimeouts (this->port_descriptor, &timeouts) == 0)
{
CloseHandle (this->port_descriptor);
return SerialExitCodes::SET_TIMEOUT_ERROR;
}
return SerialExitCodes::OK;
}
int OSSerial::flush_buffer ()
{
PurgeComm (this->port_descriptor, PURGE_TXCLEAR | PURGE_RXCLEAR);
return SerialExitCodes::OK;
}
int OSSerial::read_from_serial_port (void *bytes_to_read, int size)
{
DWORD readed;
if (!ReadFile (this->port_descriptor, bytes_to_read, size, &readed, NULL))
{
return 0;
}
return (int)readed;
}
int OSSerial::send_to_serial_port (const void *message, int length)
{
DWORD bytes_written;
if (!WriteFile (this->port_descriptor, message, length, &bytes_written, NULL))
{
return 0;
}
return bytes_written;
}
int OSSerial::close_serial_port ()
{
if (this->is_port_open ())
{
CloseHandle (this->port_descriptor);
this->port_descriptor = NULL;
}
return SerialExitCodes::OK;
}
/////////////////////////////////////////////////
//////////////////// Linux //////////////////////
/////////////////////////////////////////////////
#else
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
OSSerial::OSSerial (const char *port_name)
{
strcpy (this->port_name, port_name);
port_descriptor = 0;
}
bool OSSerial::is_port_open ()
{
return (this->port_descriptor > 0);
}
int OSSerial::open_serial_port ()
{
int flags = O_RDWR | O_NOCTTY;
this->port_descriptor = open (this->port_name, flags);
if (this->port_descriptor < 0)
{
return SerialExitCodes::OPEN_PORT_ERROR;
}
return SerialExitCodes::OK;
}
int OSSerial::set_serial_port_settings (int ms_timeout, bool timeout_only)
{
struct termios port_settings;
memset (&port_settings, 0, sizeof (port_settings));
tcgetattr (this->port_descriptor, &port_settings);
if (!timeout_only)
{
cfsetispeed (&port_settings, B115200);
cfsetospeed (&port_settings, B115200);
port_settings.c_cflag &= ~PARENB;
port_settings.c_cflag &= ~CSTOPB;
port_settings.c_cflag &= ~CSIZE;
port_settings.c_cflag |= CS8;
port_settings.c_cflag |= CREAD;
port_settings.c_cflag |= CLOCAL;
port_settings.c_cflag |= HUPCL;
port_settings.c_iflag = IGNPAR;
port_settings.c_iflag &= ~(ICANON | IXOFF | IXON | IXANY);
port_settings.c_oflag = 0;
port_settings.c_lflag = 0;
}
port_settings.c_cc[VMIN] = 0;
port_settings.c_cc[VTIME] = ms_timeout / 100; // vtime is in tenths of a second
if (tcsetattr (this->port_descriptor, TCSANOW, &port_settings) != 0)
return SerialExitCodes::SET_PORT_STATE_ERROR;
tcflush (this->port_descriptor, TCIOFLUSH);
return SerialExitCodes::OK;
}
int OSSerial::read_from_serial_port (void *bytes_to_read, int size)
{
int res = read (this->port_descriptor, bytes_to_read, size);
if (res < 0)
{
return 0;
}
return res;
}
int OSSerial::flush_buffer ()
{
tcflush (this->port_descriptor, TCIOFLUSH);
return SerialExitCodes::OK;
}
int OSSerial::send_to_serial_port (const void *message, int length)
{
int res = write (this->port_descriptor, message, length);
return res;
}
int OSSerial::close_serial_port ()
{
if (this->is_port_open ())
{
int res = close (port_descriptor);
port_descriptor = 0;
if (res < 0)
{
return SerialExitCodes::CLOSE_ERROR;
}
}
return SerialExitCodes::OK;
}
#endif