1 |
#ifdef _WINDOWS |
#ifdef _WINDOWS |
2 |
#include "StdAfx.h" |
#include "StdAfx.h" |
3 |
#else //linux |
#else //linux |
4 |
|
#include <sys/types.h> |
5 |
|
#include <sys/stat.h> |
6 |
|
|
7 |
|
#include <unistd.h> |
8 |
#include <errno.h> |
#include <errno.h> |
9 |
|
#include <termios.h> |
10 |
|
#include <fcntl.h> |
11 |
#endif |
#endif |
12 |
|
|
13 |
#include "Serial.h" |
#include "Serial.h" |
15 |
#include <stdexcept> |
#include <stdexcept> |
16 |
#include <string> |
#include <string> |
17 |
#include <sstream> |
#include <sstream> |
18 |
|
#include <iostream> |
19 |
#include <iomanip> |
#include <iomanip> |
20 |
|
|
21 |
|
#define _POSIX_SOURCE 1 /* POSIX compliant source */ |
22 |
|
#define BAUDRATE B38400 |
23 |
|
|
24 |
|
#ifndef _WINDOWS // ugly hack, else will gcc not accept this constant in openLinux() |
25 |
|
const int flags = O_RDWR | O_NOCTTY | O_NONBLOCK; |
26 |
|
#endif |
27 |
|
|
28 |
std::string writeLastError() |
std::string writeLastError() |
29 |
{ |
{ |
30 |
#ifdef _WINDOWS |
#ifdef _WINDOWS |
41 |
out << "Error" << lpMsgBuf; |
out << "Error" << lpMsgBuf; |
42 |
return out.str(); |
return out.str(); |
43 |
#else //linux |
#else //linux |
44 |
char message[256]; |
return std::string( strerror(errno) ); |
|
strerror_r(errno, message, 255); |
|
|
return std::string(message); |
|
45 |
#endif |
#endif |
46 |
} |
} |
47 |
|
|
118 |
{ |
{ |
119 |
std::string error = writeLastError(); |
std::string error = writeLastError(); |
120 |
CloseHandle(mComport); |
CloseHandle(mComport); |
121 |
throw std::exception(error.c_str()); |
throw std::exception( error ); |
122 |
} |
} |
123 |
|
|
124 |
mIsopen = true; |
mIsopen = true; |
129 |
#ifndef _WINDOWS |
#ifndef _WINDOWS |
130 |
void CSerial::openLinux() |
void CSerial::openLinux() |
131 |
{ |
{ |
132 |
|
termios newtio; |
133 |
|
|
134 |
|
std::cout << "opening port " << std::endl; |
135 |
|
mFiledescriptor = ::open(mPortstr, flags); |
136 |
|
if (mFiledescriptor < 0) |
137 |
|
throw std::runtime_error( writeLastError() ); |
138 |
|
|
139 |
|
std::cout << "port opened" << std::endl; |
140 |
|
bzero(&newtio, sizeof(newtio) ); |
141 |
|
|
142 |
|
// use a std. 8N1 config |
143 |
|
newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD; |
144 |
|
newtio.c_iflag = IGNPAR; |
145 |
|
newtio.c_oflag = 0; |
146 |
|
|
147 |
|
/* set input mode (non-canonical, no echo,...) */ |
148 |
|
newtio.c_lflag = 0; |
149 |
|
|
150 |
|
newtio.c_cc[VTIME] = 0; /* inter-character timer unused */ |
151 |
|
newtio.c_cc[VMIN] = 1; /* blocking read until 1 chars received */ |
152 |
|
|
153 |
|
tcflush(mFiledescriptor, TCIFLUSH); |
154 |
|
tcsetattr(mFiledescriptor, TCSANOW, &newtio); |
155 |
|
|
156 |
|
std::cout << "port configured " << std::endl; |
157 |
|
mIsopen = true; |
158 |
} |
} |
159 |
#endif |
#endif |
160 |
|
|
168 |
SetCommState(mComport,&mDcbRestore); |
SetCommState(mComport,&mDcbRestore); |
169 |
CloseHandle(mComport); |
CloseHandle(mComport); |
170 |
#else // linux close() |
#else // linux close() |
171 |
|
tcdrain(mFiledescriptor); |
172 |
|
tcsetattr(mFiledescriptor, TCSADRAIN, &mOldtio); //restore settings, when all data is written |
173 |
|
::close(mFiledescriptor); //close()== system-call |
174 |
#endif |
#endif |
175 |
mIsopen = false; |
mIsopen = false; |
176 |
} |
} |
193 |
throw std::exception(error.c_str()); |
throw std::exception(error.c_str()); |
194 |
} |
} |
195 |
#else //linux readByte() |
#else //linux readByte() |
196 |
|
size = read(mFiledescriptor, &out, 1); |
197 |
|
if (size != 1) |
198 |
|
throw std::runtime_error( writeLastError() ); |
199 |
#endif |
#endif |
200 |
|
|
201 |
//printByte("Read", out); |
printByte("Read", out); |
202 |
return out; |
return out; |
203 |
} |
} |
204 |
|
|
223 |
throw std::exception(error.c_str()); |
throw std::exception(error.c_str()); |
224 |
} |
} |
225 |
#else //linux writeByte() |
#else //linux writeByte() |
226 |
|
size = write(mFiledescriptor,&out,1); |
227 |
|
if (size != 1) |
228 |
|
throw std::runtime_error(writeLastError() ); |
229 |
#endif |
#endif |
230 |
} |
} |
231 |
|
|
232 |
#ifdef _WINDOWS |
#ifdef _WINDOWS |
233 |
COMSTAT CSerial::getComstat() const |
COMSTAT CSerial::getComstat() const |
234 |
{ |
{ |
260 |
#endif |
#endif |
261 |
} |
} |
262 |
|
|
263 |
/* Debug function |
// Debug function |
264 |
|
// |
265 |
void CSerial::printByte(char* description, unsigned char byte) |
void CSerial::printByte(char* description, unsigned char byte) |
266 |
{ |
{ |
267 |
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; |
268 |
std::cout << std::dec; |
std::cout << std::dec; |
269 |
} |
} |
270 |
*/ |
|
271 |
|
|
272 |
/* |
/* |
273 |
void CSerial::writeBytes(UCVector out) |
void CSerial::writeBytes(UCVector out) |