7 |
#include <fcntl.h> |
#include <fcntl.h> |
8 |
#endif |
#endif |
9 |
|
|
|
#include "stdafx.h" |
|
10 |
#include "Serial.h" |
#include "Serial.h" |
11 |
|
|
12 |
#include <stdexcept> |
#include <stdexcept> |
178 |
int CSerial::readByte() |
int CSerial::readByte() |
179 |
{ |
{ |
180 |
unsigned char out; |
unsigned char out; |
181 |
unsigned long size; |
int size = readBytes(&out,1); |
182 |
|
|
183 |
if (!mIsopen) |
if (size == 0) |
184 |
throw std::runtime_error("Port not opened"); |
return 256; |
|
#ifdef _MSC_VER |
|
|
ReadFile(mComport,&out,1,&size,0); |
|
|
if (size != 1) |
|
|
{ |
|
|
std::string error = writeLastError(); |
|
|
CloseHandle(mComport); |
|
|
throw std::exception(error.c_str()); |
|
|
} |
|
|
#else //linux readByte() |
|
|
size = read(mFiledescriptor, &out, 1); |
|
|
if (size != 1) |
|
|
{ |
|
|
return -1; |
|
|
} |
|
|
#endif |
|
185 |
|
|
|
printByte("Read", out); |
|
186 |
return out; |
return out; |
187 |
} |
} |
188 |
|
|
189 |
|
|
190 |
void CSerial::writeByte(unsigned char out) |
void CSerial::writeByte(unsigned char out) |
191 |
{ |
{ |
192 |
unsigned long size; |
writeBytes(&out,1); |
|
|
|
|
printByte("Write", out); |
|
|
if (!mIsopen) |
|
|
throw std::runtime_error("Port not opened"); |
|
|
|
|
|
#ifdef _MSC_VER |
|
|
while (getComstat().cbOutQue >0) |
|
|
Sleep(2); |
|
|
|
|
|
WriteFile(mComport,&out,1,&size, NULL); |
|
|
if (size ==0) |
|
|
{ |
|
|
std::string error = writeLastError(); |
|
|
CloseHandle(mComport); |
|
|
throw std::exception(error.c_str()); |
|
|
} |
|
|
#else //linux writeByte() |
|
|
//tcdrain(mFiledescriptor); |
|
|
size = write(mFiledescriptor,&out,1); |
|
|
Sleep(50); |
|
|
//tcdrain(mFiledescriptor); |
|
|
if (size != 1) |
|
|
throw std::runtime_error(writeLastError() ); |
|
|
#endif |
|
193 |
} |
} |
194 |
|
|
195 |
int CSerial::convertBaudrate(Baudrate rate) |
int CSerial::convertBaudrate(Baudrate rate) |
310 |
} |
} |
311 |
|
|
312 |
|
|
313 |
/* |
|
314 |
void CSerial::writeBytes(UCVector out) |
void CSerial::writeBytes(unsigned char* buf, unsigned int len) |
315 |
{ |
{ |
316 |
unsigned long bytesWritten; |
if (!mIsopen) |
317 |
unsigned int size = out.size(); |
throw std::runtime_error("Port not opened"); |
318 |
unsigned char *buf = new unsigned char[size]; |
#ifdef _MSC_VER |
319 |
|
writeBytesWindows(buf,len); |
320 |
|
#else |
321 |
|
writeBytesLinux(buf,len); |
322 |
|
#endif |
323 |
|
} |
324 |
|
|
325 |
|
|
326 |
for (int i=0; i<size; i++) |
#ifdef _MSC_VER |
327 |
buf[i] = out[i]; |
void CSerial::writeBytesWindows(unsigned char* buf, unsigned int len) |
328 |
|
{ |
329 |
|
unsigned int size; |
330 |
|
while (getComstat().cbOutQue >0) |
331 |
|
Sleep(2); |
332 |
|
|
333 |
WriteFile(mComport,buf,size,&bytesWritten,NULL); |
WriteFile(mComport,buf,len,&size, NULL); |
334 |
if (bytesWritten != size) |
if (size != len) |
335 |
{ |
{ |
336 |
std::string error = writeLastError(); |
std::string error = writeLastError(); |
337 |
CloseHandle(mComport); |
CloseHandle(mComport); |
338 |
throw std::exception(error.c_str()); |
throw std::exception(error.c_str()); |
339 |
} |
} |
|
delete[] buf; |
|
340 |
} |
} |
|
*/ |
|
341 |
|
|
342 |
|
#else |
343 |
|
void CSerial::writeBytesLinux(unsigned char* buf, unsigned int len) |
344 |
|
{ |
345 |
|
int size = write(mFiledescriptor, &buf, len); |
346 |
|
Sleep(50); |
347 |
|
//tcdrain(mFiledescriptor); |
348 |
|
if (size != len) |
349 |
|
throw std::runtime_error(writeLastError() ); |
350 |
|
} |
351 |
|
#endif |
352 |
|
|
353 |
/* |
int CSerial::readBytes(unsigned char *buf, unsigned int maxLen) |
|
UCVector CSerial::readBytes(int maxcount) |
|
354 |
{ |
{ |
355 |
UCVector buf; |
if (!mIsopen) |
356 |
|
throw std::runtime_error("Port not opened"); |
357 |
|
#ifdef _MSC_VER |
358 |
|
return readBytesWindows(buf,maxLen); |
359 |
|
#else |
360 |
|
return readBytesLinux(buf,maxLen); |
361 |
|
#endif |
362 |
|
} |
363 |
|
|
364 |
|
|
365 |
|
#ifdef _MSC_VER |
366 |
|
int CSerial::readBytesWindows(unsigned char* buf, unsigned int maxLen) |
367 |
|
{ |
368 |
|
unsigned long size; |
369 |
|
ReadFile(mComport, buf, maxLen, &size, 0); |
370 |
|
return size; |
371 |
|
} |
372 |
|
|
373 |
|
#else |
374 |
|
|
375 |
return buf; |
int CSerial::readBytesLinux(unsigned char* buf, unsigned int maxLen) |
376 |
|
{ |
377 |
|
return read(mFiledescriptor, buf, maxLen); |
378 |
} |
} |
379 |
*/ |
|
380 |
|
#endif |