1 |
#ifdef _WINDOWS |
#ifndef _MSC_VER //linux |
|
#include "StdAfx.h" |
|
|
#else //linux |
|
2 |
#include <sys/types.h> |
#include <sys/types.h> |
3 |
#include <sys/stat.h> |
#include <sys/stat.h> |
4 |
|
|
8 |
#include <fcntl.h> |
#include <fcntl.h> |
9 |
#endif |
#endif |
10 |
|
|
11 |
|
#include "StdAfx.h" |
12 |
#include "Serial.h" |
#include "Serial.h" |
13 |
|
|
14 |
#include <stdexcept> |
#include <stdexcept> |
20 |
#define _POSIX_SOURCE 1 /* POSIX compliant source */ |
#define _POSIX_SOURCE 1 /* POSIX compliant source */ |
21 |
#define BAUDRATE B9600 |
#define BAUDRATE B9600 |
22 |
|
|
23 |
#ifndef _WINDOWS // ugly hack, else will gcc not accept this constant in openLinux() |
#ifndef _MSC_VER // ugly hack, else will gcc not accept this constant in openLinux() |
24 |
const int flags = O_RDWR | O_NOCTTY ; //| O_NONBLOCK; |
const int flags = O_RDWR | O_NOCTTY ; //| O_NONBLOCK; |
25 |
#endif |
#endif |
26 |
|
|
27 |
std::string writeLastError() |
std::string writeLastError() |
28 |
{ |
{ |
29 |
#ifdef _WINDOWS |
#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, |
56 |
mBitrate = bitrate; |
mBitrate = bitrate; |
57 |
mIsopen = false; |
mIsopen = false; |
58 |
|
|
59 |
#ifdef _WINDOWS |
#ifdef _MSC_VER |
60 |
openWindows(); |
openWindows(); |
61 |
#else |
#else |
62 |
openLinux(); |
openLinux(); |
76 |
mPortstr = port; |
mPortstr = port; |
77 |
mBitrate = bitrate; |
mBitrate = bitrate; |
78 |
|
|
79 |
#ifdef _WINDOWS |
#ifdef _MSC_VER |
80 |
openWindows(); |
openWindows(); |
81 |
#else |
#else |
82 |
openLinux(); |
openLinux(); |
83 |
#endif |
#endif |
84 |
} |
} |
85 |
|
|
86 |
#ifdef _WINDOWS |
#ifdef _MSC_VER |
87 |
void CSerial::openWindows() |
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); |
117 |
{ |
{ |
118 |
std::string error = writeLastError(); |
std::string error = writeLastError(); |
119 |
CloseHandle(mComport); |
CloseHandle(mComport); |
120 |
throw std::exception( error ); |
throw std::runtime_error( error ); |
121 |
} |
} |
122 |
|
|
123 |
mIsopen = true; |
mIsopen = true; |
125 |
#endif |
#endif |
126 |
|
|
127 |
|
|
128 |
#ifndef _WINDOWS |
#ifndef _MSC_VER |
129 |
void CSerial::openLinux() |
void CSerial::openLinux() |
130 |
{ |
{ |
131 |
termios newtio; |
termios newtio; |
165 |
{ |
{ |
166 |
if (mIsopen) |
if (mIsopen) |
167 |
{ |
{ |
168 |
#ifdef _WINDOWS |
#ifdef _MSC_VER |
169 |
while (getComstat().cbOutQue >0) |
while (getComstat().cbOutQue >0) |
170 |
Sleep(5); |
Sleep(5); |
171 |
SetCommState(mComport,&mDcbRestore); |
SetCommState(mComport,&mDcbRestore); |
187 |
|
|
188 |
if (!mIsopen) |
if (!mIsopen) |
189 |
throw std::runtime_error("Port not opened"); |
throw std::runtime_error("Port not opened"); |
190 |
#ifdef _WINDOWS |
#ifdef _MSC_VER |
191 |
ReadFile(mComport,&out,1,&size,0); |
ReadFile(mComport,&out,1,&size,0); |
192 |
if (size != 1) |
if (size != 1) |
193 |
{ |
{ |
217 |
if (!mIsopen) |
if (!mIsopen) |
218 |
throw std::runtime_error("Port not opened"); |
throw std::runtime_error("Port not opened"); |
219 |
|
|
220 |
#ifdef _WINDOWS |
#ifdef _MSC_VER |
221 |
while (getComstat().cbOutQue >0) |
while (getComstat().cbOutQue >0) |
222 |
Sleep(2); |
Sleep(2); |
223 |
|
|
238 |
#endif |
#endif |
239 |
} |
} |
240 |
|
|
241 |
#ifdef _WINDOWS |
#ifdef _MSC_VER |
242 |
COMSTAT CSerial::getComstat() const |
COMSTAT CSerial::getComstat() const |
243 |
{ |
{ |
244 |
if (!mIsopen) |
if (!mIsopen) |
253 |
|
|
254 |
int CSerial::bytesReady() const |
int CSerial::bytesReady() const |
255 |
{ |
{ |
256 |
#ifdef _WINDOWS |
#ifdef _MSC_VER |
257 |
return getComstat().cbInQue; |
return getComstat().cbInQue; |
258 |
#else |
#else |
259 |
return 0; |
return 0; |
262 |
|
|
263 |
int CSerial::outQueueSize() const |
int CSerial::outQueueSize() const |
264 |
{ |
{ |
265 |
#ifdef _WINDOWS |
#ifdef _MSC_VER |
266 |
return getComstat().cbOutQue; |
return getComstat().cbOutQue; |
267 |
#else |
#else |
268 |
return 0; |
return 0; |