19 |
#include <iomanip> |
#include <iomanip> |
20 |
|
|
21 |
#define _POSIX_SOURCE 1 /* POSIX compliant source */ |
#define _POSIX_SOURCE 1 /* POSIX compliant source */ |
22 |
#define BAUDRATE B38400 |
#define BAUDRATE B9600 |
23 |
|
|
24 |
#ifndef _WINDOWS // ugly hack, else will gcc not accept this constant in openLinux() |
#ifndef _WINDOWS // ugly hack, else will gcc not accept this constant in openLinux() |
25 |
const int flags = O_RDWR | O_NOCTTY | O_NONBLOCK; |
const int flags = O_RDWR | O_NOCTTY ; //| O_NONBLOCK; |
26 |
#endif |
#endif |
27 |
|
|
28 |
std::string writeLastError() |
std::string writeLastError() |
148 |
newtio.c_lflag = 0; |
newtio.c_lflag = 0; |
149 |
|
|
150 |
newtio.c_cc[VTIME] = 0; /* inter-character timer unused */ |
newtio.c_cc[VTIME] = 0; /* inter-character timer unused */ |
151 |
newtio.c_cc[VMIN] = 1; /* blocking read until 1 chars received */ |
newtio.c_cc[VMIN] = 0; /* blocking read until 1 chars received */ |
152 |
|
|
153 |
|
cfmakeraw(&newtio); |
154 |
|
cfsetospeed(&newtio, BAUDRATE); |
155 |
|
|
156 |
|
|
157 |
tcflush(mFiledescriptor, TCIFLUSH); |
tcflush(mFiledescriptor, TCIFLUSH); |
158 |
tcsetattr(mFiledescriptor, TCSANOW, &newtio); |
tcsetattr(mFiledescriptor, TCSANOW, &newtio); |
181 |
} |
} |
182 |
|
|
183 |
|
|
184 |
unsigned char CSerial::readByte() |
int CSerial::readByte() |
185 |
{ |
{ |
186 |
unsigned char out; |
unsigned char out; |
187 |
unsigned long size; |
unsigned long size; |
198 |
} |
} |
199 |
#else //linux readByte() |
#else //linux readByte() |
200 |
size = read(mFiledescriptor, &out, 1); |
size = read(mFiledescriptor, &out, 1); |
201 |
if (size != 1) |
if (size != 1) |
202 |
throw std::runtime_error( writeLastError() ); |
{ |
203 |
|
std::cout << writeLastError() << std::endl; |
204 |
|
return -1; |
205 |
|
} |
206 |
#endif |
#endif |
207 |
|
|
208 |
printByte("Read", out); |
printByte("Read", out); |
214 |
{ |
{ |
215 |
unsigned long size; |
unsigned long size; |
216 |
|
|
217 |
//printByte("Write", out); |
printByte("Write", out); |
218 |
if (!mIsopen) |
if (!mIsopen) |
219 |
throw std::runtime_error("Port not opened"); |
throw std::runtime_error("Port not opened"); |
220 |
|
|
230 |
throw std::exception(error.c_str()); |
throw std::exception(error.c_str()); |
231 |
} |
} |
232 |
#else //linux writeByte() |
#else //linux writeByte() |
233 |
|
//tcdrain(mFiledescriptor); |
234 |
size = write(mFiledescriptor,&out,1); |
size = write(mFiledescriptor,&out,1); |
235 |
|
Sleep(50); |
236 |
|
//tcdrain(mFiledescriptor); |
237 |
if (size != 1) |
if (size != 1) |
238 |
throw std::runtime_error(writeLastError() ); |
throw std::runtime_error(writeLastError() ); |
239 |
#endif |
#endif |