1 |
#include "StdAfx.h" |
#ifndef _MSC_VER //linux |
2 |
|
#include <sys/types.h> |
3 |
|
#include <sys/stat.h> |
4 |
|
|
5 |
|
#include <unistd.h> |
6 |
|
#include <errno.h> |
7 |
|
#include <termios.h> |
8 |
|
#include <fcntl.h> |
9 |
|
#endif |
10 |
|
|
11 |
|
#include "StdAfx.h" |
12 |
#include "Serial.h" |
#include "Serial.h" |
13 |
|
|
14 |
#include <exception> |
#include <stdexcept> |
15 |
#include <string> |
#include <string> |
16 |
#include <sstream> |
#include <sstream> |
17 |
|
#include <iostream> |
18 |
#include <iomanip> |
#include <iomanip> |
19 |
|
|
20 |
|
#define _POSIX_SOURCE 1 /* POSIX compliant source */ |
21 |
|
#define BAUDRATE B9600 |
22 |
|
|
23 |
|
#ifndef _MSC_VER // ugly hack, else will gcc not accept this constant in openLinux() |
24 |
|
const int flags = O_RDWR | O_NOCTTY | O_NONBLOCK; |
25 |
|
#endif |
26 |
|
|
27 |
std::string writeLastError() |
std::string writeLastError() |
28 |
{ |
{ |
29 |
|
#ifdef _MSC_VER |
30 |
LPVOID lpMsgBuf; |
LPVOID lpMsgBuf; |
31 |
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS, |
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS, |
32 |
NULL, |
NULL, |
39 |
std::ostringstream out; |
std::ostringstream out; |
40 |
out << "Error" << lpMsgBuf; |
out << "Error" << lpMsgBuf; |
41 |
return out.str(); |
return out.str(); |
42 |
|
#else //linux |
43 |
|
return std::string( strerror(errno) ); |
44 |
|
#endif |
45 |
} |
} |
46 |
|
|
47 |
|
|
56 |
mBitrate = bitrate; |
mBitrate = bitrate; |
57 |
mIsopen = false; |
mIsopen = false; |
58 |
|
|
59 |
open(); |
#ifdef _MSC_VER |
60 |
|
openWindows(); |
61 |
|
#else |
62 |
|
openLinux(); |
63 |
|
#endif |
64 |
} |
} |
65 |
|
|
66 |
CSerial::~CSerial(void) |
CSerial::~CSerial(void) |
71 |
void CSerial::open(char* port, int bitrate) |
void CSerial::open(char* port, int bitrate) |
72 |
{ |
{ |
73 |
if (mIsopen) |
if (mIsopen) |
74 |
throw std::exception("Port already opened"); |
throw std::runtime_error("Port already opened"); |
75 |
|
|
76 |
mPortstr = port; |
mPortstr = port; |
77 |
mBitrate = bitrate; |
mBitrate = bitrate; |
78 |
|
|
79 |
open(); |
#ifdef _MSC_VER |
80 |
|
openWindows(); |
81 |
|
#else |
82 |
|
openLinux(); |
83 |
|
#endif |
84 |
} |
} |
85 |
|
|
86 |
void CSerial::open() |
#ifdef _MSC_VER |
87 |
|
void CSerial::openWindows() |
88 |
{ |
{ |
89 |
mComport = CreateFile( mPortstr, GENERIC_READ|GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0); |
mComport = CreateFile( mPortstr, GENERIC_READ|GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0); |
90 |
|
|
91 |
if (mComport == INVALID_HANDLE_VALUE) |
if (mComport == INVALID_HANDLE_VALUE) |
92 |
{ |
{ |
93 |
throw std::exception(writeLastError().c_str()); |
throw std::runtime_error(writeLastError().c_str()); |
94 |
} |
} |
95 |
|
|
96 |
DCB dcb; |
DCB dcb; |
117 |
{ |
{ |
118 |
std::string error = writeLastError(); |
std::string error = writeLastError(); |
119 |
CloseHandle(mComport); |
CloseHandle(mComport); |
120 |
throw std::exception(error.c_str()); |
throw std::runtime_error( error ); |
121 |
} |
} |
122 |
|
|
123 |
mIsopen = true; |
mIsopen = true; |
124 |
} |
} |
125 |
|
#endif |
126 |
|
|
127 |
|
|
128 |
|
#ifndef _MSC_VER |
129 |
|
void CSerial::openLinux() |
130 |
|
{ |
131 |
|
termios newtio; |
132 |
|
|
133 |
|
std::cout << "opening port " << std::endl; |
134 |
|
mFiledescriptor = ::open(mPortstr, flags); |
135 |
|
if (mFiledescriptor < 0) |
136 |
|
throw std::runtime_error( writeLastError() ); |
137 |
|
|
138 |
|
std::cout << "port opened" << std::endl; |
139 |
|
bzero(&newtio, sizeof(newtio) ); |
140 |
|
|
141 |
|
// use a std. 8N1 config |
142 |
|
newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD; |
143 |
|
newtio.c_iflag = IGNPAR; |
144 |
|
newtio.c_oflag = 0; |
145 |
|
|
146 |
|
/* set input mode (non-canonical, no echo,...) */ |
147 |
|
newtio.c_lflag = 0; |
148 |
|
|
149 |
|
newtio.c_cc[VTIME] = 0; /* inter-character timer unused */ |
150 |
|
newtio.c_cc[VMIN] = 0; /* blocking read until 1 chars received */ |
151 |
|
|
152 |
|
cfmakeraw(&newtio); |
153 |
|
cfsetospeed(&newtio, BAUDRATE); |
154 |
|
|
155 |
|
|
156 |
|
tcflush(mFiledescriptor, TCIFLUSH); |
157 |
|
tcsetattr(mFiledescriptor, TCSANOW, &newtio); |
158 |
|
|
159 |
|
std::cout << "port configured " << std::endl; |
160 |
|
mIsopen = true; |
161 |
|
} |
162 |
|
#endif |
163 |
|
|
164 |
void CSerial::close() |
void CSerial::close() |
165 |
{ |
{ |
166 |
if (mIsopen) |
if (mIsopen) |
167 |
{ |
{ |
168 |
|
#ifdef _MSC_VER |
169 |
while (getComstat().cbOutQue >0) |
while (getComstat().cbOutQue >0) |
170 |
Sleep(5); |
Sleep(5); |
171 |
SetCommState(mComport,&mDcbRestore); |
SetCommState(mComport,&mDcbRestore); |
172 |
CloseHandle(mComport); |
CloseHandle(mComport); |
173 |
|
#else // linux close() |
174 |
|
tcdrain(mFiledescriptor); |
175 |
|
tcsetattr(mFiledescriptor, TCSADRAIN, &mOldtio); //restore settings, when all data is written |
176 |
|
::close(mFiledescriptor); //close()== system-call |
177 |
|
#endif |
178 |
mIsopen = false; |
mIsopen = false; |
179 |
} |
} |
180 |
} |
} |
181 |
|
|
182 |
|
|
183 |
unsigned char CSerial::readByte() |
int CSerial::readByte() |
184 |
{ |
{ |
185 |
unsigned char out; |
unsigned char out; |
186 |
unsigned long size; |
unsigned long size; |
187 |
|
|
188 |
if (!mIsopen) |
if (!mIsopen) |
189 |
throw std::exception("Port not opened"); |
throw std::runtime_error("Port not opened"); |
190 |
|
#ifdef _MSC_VER |
191 |
ReadFile(mComport,&out,1,&size,0); |
ReadFile(mComport,&out,1,&size,0); |
192 |
if (size != 1) |
if (size != 1) |
193 |
{ |
{ |
195 |
CloseHandle(mComport); |
CloseHandle(mComport); |
196 |
throw std::exception(error.c_str()); |
throw std::exception(error.c_str()); |
197 |
} |
} |
198 |
|
#else //linux readByte() |
199 |
|
size = read(mFiledescriptor, &out, 1); |
200 |
|
if (size != 1) |
201 |
|
{ |
202 |
|
std::cout << writeLastError() << std::endl; |
203 |
|
return -1; |
204 |
|
} |
205 |
|
#endif |
206 |
|
|
207 |
printByte("Read", out); |
printByte("Read", out); |
208 |
return out; |
return out; |
215 |
|
|
216 |
printByte("Write", out); |
printByte("Write", out); |
217 |
if (!mIsopen) |
if (!mIsopen) |
218 |
throw std::exception("Port not opened"); |
throw std::runtime_error("Port not opened"); |
219 |
|
|
220 |
|
#ifdef _MSC_VER |
221 |
while (getComstat().cbOutQue >0) |
while (getComstat().cbOutQue >0) |
222 |
Sleep(2); |
Sleep(2); |
223 |
|
|
228 |
CloseHandle(mComport); |
CloseHandle(mComport); |
229 |
throw std::exception(error.c_str()); |
throw std::exception(error.c_str()); |
230 |
} |
} |
231 |
|
#else //linux writeByte() |
232 |
|
//tcdrain(mFiledescriptor); |
233 |
|
size = write(mFiledescriptor,&out,1); |
234 |
|
Sleep(50); |
235 |
|
//tcdrain(mFiledescriptor); |
236 |
|
if (size != 1) |
237 |
|
throw std::runtime_error(writeLastError() ); |
238 |
|
#endif |
239 |
} |
} |
240 |
|
|
241 |
|
#ifdef _MSC_VER |
242 |
COMSTAT CSerial::getComstat() const |
COMSTAT CSerial::getComstat() const |
243 |
{ |
{ |
244 |
if (!mIsopen) |
if (!mIsopen) |
249 |
ClearCommError(mComport,&x,&stat); |
ClearCommError(mComport,&x,&stat); |
250 |
return stat; |
return stat; |
251 |
} |
} |
252 |
|
#endif |
253 |
|
|
254 |
int CSerial::bytesReady() const |
int CSerial::bytesReady() const |
255 |
{ |
{ |
256 |
|
#ifdef _MSC_VER |
257 |
return getComstat().cbInQue; |
return getComstat().cbInQue; |
258 |
|
#else |
259 |
|
return 0; |
260 |
|
#endif |
261 |
} |
} |
262 |
|
|
263 |
int CSerial::outQueueSize() const |
int CSerial::outQueueSize() const |
264 |
{ |
{ |
265 |
|
#ifdef _MSC_VER |
266 |
return getComstat().cbOutQue; |
return getComstat().cbOutQue; |
267 |
|
#else |
268 |
|
return 0; |
269 |
|
#endif |
270 |
} |
} |
271 |
|
|
272 |
|
// Debug function |
273 |
|
// |
274 |
void CSerial::printByte(char* description, unsigned char byte) |
void CSerial::printByte(char* description, unsigned char byte) |
275 |
{ |
{ |
276 |
std::cout << description << " : " << (int) byte << "/" << std::setw(2) << std::setfill('0') << std::hex << (int) byte << std::endl; |
std::cout << description << " : " << (int) byte << "/" << std::setw(2) << std::setfill('0') << std::hex << (int) byte << std::endl; |
277 |
std::cout << std::dec; |
std::cout << std::dec; |
278 |
} |
} |
279 |
|
|
280 |
|
|
281 |
/* |
/* |
282 |
void CSerial::writeBytes(UCVector out) |
void CSerial::writeBytes(UCVector out) |
283 |
{ |
{ |