1 |
|
#ifdef _WINDOWS |
2 |
#include "StdAfx.h" |
#include "StdAfx.h" |
3 |
|
#else //linux |
4 |
|
#include <errno.h> |
5 |
|
#endif |
6 |
|
|
7 |
#include "Serial.h" |
#include "Serial.h" |
8 |
|
|
9 |
#include <exception> |
#include <stdexcept> |
10 |
#include <string> |
#include <string> |
11 |
#include <sstream> |
#include <sstream> |
12 |
#include <iomanip> |
#include <iomanip> |
13 |
|
|
14 |
std::string writeLastError() |
std::string writeLastError() |
15 |
{ |
{ |
16 |
|
#ifdef _WINDOWS |
17 |
LPVOID lpMsgBuf; |
LPVOID lpMsgBuf; |
18 |
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, |
19 |
NULL, |
NULL, |
26 |
std::ostringstream out; |
std::ostringstream out; |
27 |
out << "Error" << lpMsgBuf; |
out << "Error" << lpMsgBuf; |
28 |
return out.str(); |
return out.str(); |
29 |
|
#else //linux |
30 |
|
char message[256]; |
31 |
|
strerror_r(errno, message, 255); |
32 |
|
return std::string(message); |
33 |
|
#endif |
34 |
} |
} |
35 |
|
|
36 |
|
|
45 |
mBitrate = bitrate; |
mBitrate = bitrate; |
46 |
mIsopen = false; |
mIsopen = false; |
47 |
|
|
48 |
open(); |
#ifdef _WINDOWS |
49 |
|
openWindows(); |
50 |
|
#else |
51 |
|
openLinux(); |
52 |
|
#endif |
53 |
} |
} |
54 |
|
|
55 |
CSerial::~CSerial(void) |
CSerial::~CSerial(void) |
60 |
void CSerial::open(char* port, int bitrate) |
void CSerial::open(char* port, int bitrate) |
61 |
{ |
{ |
62 |
if (mIsopen) |
if (mIsopen) |
63 |
throw std::exception("Port already opened"); |
throw std::runtime_error("Port already opened"); |
64 |
|
|
65 |
mPortstr = port; |
mPortstr = port; |
66 |
mBitrate = bitrate; |
mBitrate = bitrate; |
67 |
|
|
68 |
open(); |
#ifdef _WINDOWS |
69 |
|
openWindows(); |
70 |
|
#else |
71 |
|
openLinux(); |
72 |
|
#endif |
73 |
} |
} |
74 |
|
|
75 |
void CSerial::open() |
#ifdef _WINDOWS |
76 |
|
void CSerial::openWindows() |
77 |
{ |
{ |
78 |
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); |
79 |
|
|
80 |
if (mComport == INVALID_HANDLE_VALUE) |
if (mComport == INVALID_HANDLE_VALUE) |
81 |
{ |
{ |
82 |
throw std::exception(writeLastError().c_str()); |
throw std::runtime_error(writeLastError().c_str()); |
83 |
} |
} |
84 |
|
|
85 |
DCB dcb; |
DCB dcb; |
111 |
|
|
112 |
mIsopen = true; |
mIsopen = true; |
113 |
} |
} |
114 |
|
#endif |
115 |
|
|
116 |
|
|
117 |
|
#ifndef _WINDOWS |
118 |
|
void CSerial::openLinux() |
119 |
|
{ |
120 |
|
} |
121 |
|
#endif |
122 |
|
|
123 |
void CSerial::close() |
void CSerial::close() |
124 |
{ |
{ |
125 |
if (mIsopen) |
if (mIsopen) |
126 |
{ |
{ |
127 |
|
#ifdef _WINDOWS |
128 |
while (getComstat().cbOutQue >0) |
while (getComstat().cbOutQue >0) |
129 |
Sleep(5); |
Sleep(5); |
130 |
SetCommState(mComport,&mDcbRestore); |
SetCommState(mComport,&mDcbRestore); |
131 |
CloseHandle(mComport); |
CloseHandle(mComport); |
132 |
|
#else // linux close() |
133 |
|
#endif |
134 |
mIsopen = false; |
mIsopen = false; |
135 |
} |
} |
136 |
} |
} |
142 |
unsigned long size; |
unsigned long size; |
143 |
|
|
144 |
if (!mIsopen) |
if (!mIsopen) |
145 |
throw std::exception("Port not opened"); |
throw std::runtime_error("Port not opened"); |
146 |
|
#ifdef _WINDOWS |
147 |
ReadFile(mComport,&out,1,&size,0); |
ReadFile(mComport,&out,1,&size,0); |
148 |
if (size != 1) |
if (size != 1) |
149 |
{ |
{ |
151 |
CloseHandle(mComport); |
CloseHandle(mComport); |
152 |
throw std::exception(error.c_str()); |
throw std::exception(error.c_str()); |
153 |
} |
} |
154 |
|
#else //linux readByte() |
155 |
|
#endif |
156 |
|
|
157 |
|
//printByte("Read", out); |
|
printByte("Read", out); |
|
158 |
return out; |
return out; |
159 |
} |
} |
160 |
|
|
163 |
{ |
{ |
164 |
unsigned long size; |
unsigned long size; |
165 |
|
|
166 |
printByte("Write", out); |
//printByte("Write", out); |
167 |
if (!mIsopen) |
if (!mIsopen) |
168 |
throw std::exception("Port not opened"); |
throw std::runtime_error("Port not opened"); |
169 |
|
|
170 |
|
#ifdef _WINDOWS |
171 |
while (getComstat().cbOutQue >0) |
while (getComstat().cbOutQue >0) |
172 |
Sleep(2); |
Sleep(2); |
173 |
|
|
178 |
CloseHandle(mComport); |
CloseHandle(mComport); |
179 |
throw std::exception(error.c_str()); |
throw std::exception(error.c_str()); |
180 |
} |
} |
181 |
|
#else //linux writeByte() |
182 |
|
#endif |
183 |
} |
} |
184 |
|
#ifdef _WINDOWS |
185 |
COMSTAT CSerial::getComstat() const |
COMSTAT CSerial::getComstat() const |
186 |
{ |
{ |
187 |
if (!mIsopen) |
if (!mIsopen) |
192 |
ClearCommError(mComport,&x,&stat); |
ClearCommError(mComport,&x,&stat); |
193 |
return stat; |
return stat; |
194 |
} |
} |
195 |
|
#endif |
196 |
|
|
197 |
int CSerial::bytesReady() const |
int CSerial::bytesReady() const |
198 |
{ |
{ |
199 |
|
#ifdef _WINDOWS |
200 |
return getComstat().cbInQue; |
return getComstat().cbInQue; |
201 |
|
#else |
202 |
|
return 0; |
203 |
|
#endif |
204 |
} |
} |
205 |
|
|
206 |
int CSerial::outQueueSize() const |
int CSerial::outQueueSize() const |
207 |
{ |
{ |
208 |
|
#ifdef _WINDOWS |
209 |
return getComstat().cbOutQue; |
return getComstat().cbOutQue; |
210 |
|
#else |
211 |
|
return 0; |
212 |
|
#endif |
213 |
} |
} |
214 |
|
|
215 |
|
/* Debug function |
216 |
void CSerial::printByte(char* description, unsigned char byte) |
void CSerial::printByte(char* description, unsigned char byte) |
217 |
{ |
{ |
218 |
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; |
219 |
std::cout << std::dec; |
std::cout << std::dec; |
220 |
} |
} |
221 |
|
*/ |
222 |
|
|
223 |
/* |
/* |
224 |
void CSerial::writeBytes(UCVector out) |
void CSerial::writeBytes(UCVector out) |