38 std::string deviceName;
42 strncpy_s(comX,
"COM ", 5);
43 comX[3] = digit(ccd.
port);
45 prtHdl = CreateFile(comX,
46 GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING,
47 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH | FILE_FLAG_NO_BUFFERING, 0
50 if (prtHdl == INVALID_HANDLE_VALUE) {
54 FillMemory(&commDCB,
sizeof(commDCB), 0);
55 commDCB.DCBlength =
sizeof(commDCB);
56 strncpy_s(dcb,
"baud= parity= data= stop= ", 35);
57 for(i=5,d=100000; d>=1; d=d/10) {
59 dcb[i++] = digit((ccd.
baud/d) % 10);
63 dcb[26] = digit(ccd.
data);
64 dcb[33] = digit(ccd.
stop);
65 if (!BuildCommDCB(dcb, &commDCB)) {
70 commDCB.fAbortOnError =
false;
72 commDCB.fOutX =
false;
73 commDCB.fOutxCtsFlow =
false;
74 if (!SetCommState(prtHdl, &commDCB)) {
79 PurgeComm( prtHdl, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);
81 GetCommTimeouts(prtHdl, &oto);
82 nto.ReadIntervalTimeout = MAXDWORD;
83 nto.ReadTotalTimeoutMultiplier = 0;
84 nto.ReadTotalTimeoutConstant = ccd.
rttc;
85 nto.WriteTotalTimeoutMultiplier = 0;
86 nto.WriteTotalTimeoutConstant = ccd.
wttc;
87 if (!SetCommTimeouts(prtHdl, &nto)) {
94 _deviceName = deviceName;
100 if(_prtHdl == INVALID_HANDLE_VALUE)
103 FlushFileBuffers(_prtHdl);
104 SetCommTimeouts(_prtHdl, &_oto);
106 CloseHandle(_prtHdl);
112 if (_prtHdl == INVALID_HANDLE_VALUE) {
116 if(PurgeComm(_prtHdl, PURGE_TXABORT | PURGE_TXCLEAR) == 0) {
120 unsigned long readsz;
121 if (WriteFile(_prtHdl, buf, size, &readsz, 0) == 0) {
125 if(readsz != static_cast<long>(size)) {
134 if (_prtHdl == INVALID_HANDLE_VALUE) {
138 unsigned char* tmp =
static_cast<unsigned char*
>(buf);
139 unsigned long readsz = 0, readsz_temp = 0;
142 while (readsz<(
unsigned long)size && !timeout.Elapsed()) {
143 if(ReadFile(_prtHdl, &tmp[readsz], size, &readsz_temp, 0) == 0) {
146 readsz += readsz_temp;
150 if((
unsigned)size != readsz) {
154 if(PurgeComm(_prtHdl, PURGE_RXABORT | PURGE_RXCLEAR) == 0) {
167 std::string deviceName;
168 struct termios nto, oto;
173 strncpy(name,
"/dev/ttyS ", 11);
175 prtHdl = ::open(name, O_RDWR | O_NOCTTY | O_NDELAY| O_NONBLOCK);
183 tcgetattr(prtHdl, &oto);
184 bzero(&nto,
sizeof(nto));
189 nto.c_cflag = CLOCAL | CREAD;
218 nto.c_cflag |= B1200;
221 nto.c_cflag |= B1800;
224 nto.c_cflag |= B2400;
227 nto.c_cflag |= B4800;
230 nto.c_cflag |= B9600;
233 nto.c_cflag |= B19200;
236 nto.c_cflag |= B38400;
239 nto.c_cflag |= B57600;
242 nto.c_cflag |= B115200;
245 nto.c_cflag |= B230400;
270 nto.c_cflag |= PARENB | PARODD;
274 nto.c_cflag |= PARENB;
282 nto.c_cflag |= CSTOPB;
286 tcflush(prtHdl,TCIFLUSH);
287 if (tcsetattr(prtHdl, TCSANOW, &nto) != 0) {
318 int tcflush_return = tcflush(
_prtHdl,TCIFLUSH);
319 if(tcflush_return < 0)
334 unsigned char* tmp =
static_cast<unsigned char*
>(buf);
335 register int readsz = 0;
345 while (readsz<size && !timeout.
Elapsed()) {
346 read_return = read(
_prtHdl, &tmp[readsz], size-readsz);
347 if(read_return < 0) {
351 readsz += read_return;
358 int tcflush_return = tcflush(
_prtHdl,TCIFLUSH);
359 if(tcflush_return < 0)
368 #endif //WIN32 else LINUX CCdlCOM(TCdlCOMDesc ccd)
Construct a CCdlCOM class.
static char digit(const int _val)
Converts an integer to a char.
virtual int send(const void *buf, int size)
Sends data to the device.
int wttc
write total timeout
int baud
baud rate of port
virtual ~CCdlCOM()
Destructs the class.
int port
serial port number
This structrue stores the attributes for a serial port device.
TCdlCOMDesc _ccd
Stores the attributes of the serial port device.
struct termios _oto
old timeouts
int rttc
read total timeout
virtual int recv(void *buf, int size)
Receives data from the device.