~ubuntu-branches/ubuntu/wily/flrig/wily

« back to all changes in this revision

Viewing changes to .pc/0001-License-Declaration.patch/src/serial.cxx

  • Committer: Package Import Robot
  • Author(s): Kamal Mostafa
  • Date: 2014-10-25 11:17:10 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20141025111710-n32skgya3l9u1brw
Tags: 1.3.17-1
* New upstream release (Closes: #761839)
* Debian Standards-Version: 3.9.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//-----------------------------------------------------------------------------
2
 
//
3
 
// Serial i/o class
4
 
//
5
 
// copyright Dave Freese 2006, w1hkj@w1hkj.com
6
 
//
7
 
//-----------------------------------------------------------------------------
8
 
 
9
 
#include <string>
10
 
 
11
 
#include "serial.h"
12
 
#include "debug.h"
13
 
 
14
 
LOG_FILE_SOURCE(debug::LOG_RIGCONTROL);
15
 
 
16
 
 
17
 
#ifndef __WIN32__
18
 
#include <cstdio>
19
 
#include <unistd.h>
20
 
#include <sys/types.h>
21
 
#include <sys/stat.h>
22
 
#include <fcntl.h>
23
 
#include <sys/ioctl.h>
24
 
#include <sys/time.h>
25
 
 
26
 
#include <memory>
27
 
 
28
 
using namespace std;
29
 
 
30
 
Cserial::Cserial() {
31
 
        device = "/dev/ttyS0";
32
 
        baud = 1200;
33
 
        timeout = 50; //msec
34
 
        retries = 5;
35
 
        rts = dtr = false;
36
 
        rtsptt = dtrptt = false;
37
 
        rtscts = false;
38
 
        status = 0;
39
 
        stopbits = 2;
40
 
        fd = -1;
41
 
}
42
 
 
43
 
Cserial::~Cserial() {
44
 
        ClosePort();
45
 
}
46
 
 
47
 
 
48
 
 
49
 
///////////////////////////////////////////////////////
50
 
// Function name        : Cserial::OpenPort
51
 
// Description    : Opens the port specified by strPortName
52
 
// Return type    : bool
53
 
// Argument              : c_string strPortName
54
 
///////////////////////////////////////////////////////
55
 
bool Cserial::CheckPort(string dev)  {
56
 
        int testfd = open( dev.c_str(), O_RDWR | O_NOCTTY | O_NDELAY);
57
 
        if (testfd < 0)
58
 
                return false;
59
 
        close(fd);
60
 
        return true;
61
 
}
62
 
 
63
 
///////////////////////////////////////////////////////
64
 
// Function name        : Cserial::OpenPort
65
 
// Description    : Opens the port specified by strPortName
66
 
// Return type    : bool
67
 
// Argument              : c_string strPortName
68
 
///////////////////////////////////////////////////////
69
 
bool Cserial::OpenPort()  {
70
 
 
71
 
        if ((fd = open( device.c_str(), O_RDWR | O_NOCTTY | O_NDELAY )) < 0)
72
 
                return false;
73
 
// save current port settings
74
 
        tcflush (fd, TCIFLUSH);
75
 
 
76
 
        tcgetattr (fd, &oldtio);
77
 
        newtio = oldtio;
78
 
 
79
 
        // 8 data bits
80
 
        newtio.c_cflag &= ~CSIZE;
81
 
        newtio.c_cflag |= CS8;
82
 
        // enable receiver, set local mode
83
 
        newtio.c_cflag |= (CLOCAL | CREAD);
84
 
        // no parity
85
 
        newtio.c_cflag &= ~PARENB;
86
 
 
87
 
        if (stopbits == 1)
88
 
                // 1 stop bit
89
 
                newtio.c_cflag &= ~CSTOPB;
90
 
        else
91
 
                // 2 stop bit
92
 
                newtio.c_cflag |= CSTOPB;
93
 
 
94
 
        if (rtscts)
95
 
                // h/w handshake
96
 
                newtio.c_cflag |= CRTSCTS;
97
 
        else
98
 
                // no h/w handshake
99
 
                newtio.c_cflag &= ~CRTSCTS;
100
 
 
101
 
        // raw input
102
 
        newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
103
 
        // raw output
104
 
        newtio.c_oflag &= ~OPOST;
105
 
        // software flow control disabled
106
 
        newtio.c_iflag &= ~IXON;
107
 
        // do not translate CR to NL
108
 
        newtio.c_iflag &= ~ICRNL;
109
 
 
110
 
        switch(baud) {
111
 
                case 300:
112
 
                        speed = B300;
113
 
                        break;
114
 
                case 1200:
115
 
                        speed = B1200;
116
 
                        break;
117
 
                case 2400:
118
 
                        speed = B2400;
119
 
                        break;
120
 
                case 4800:
121
 
                        speed = B4800;
122
 
                        break;
123
 
                case 9600:
124
 
                        speed = B9600;
125
 
                        break;
126
 
                case 19200:
127
 
                        speed = B19200;
128
 
                        break;
129
 
                case 38400:
130
 
                        speed = B38400;
131
 
                        break;
132
 
                case 57600:
133
 
                        speed = B57600;
134
 
                        break;
135
 
                case 115200:
136
 
                        speed = B115200;
137
 
                        break;
138
 
                default:
139
 
                        speed = B1200;
140
 
        }
141
 
        cfsetispeed(&newtio, speed);
142
 
        cfsetospeed(&newtio, speed);
143
 
 
144
 
        tcsetattr (fd, TCSANOW, &newtio);
145
 
 
146
 
        ioctl(fd, TIOCMGET, &status);
147
 
        origstatus = status;
148
 
 
149
 
        if (dtr)
150
 
                status |= TIOCM_DTR;            // set the DTR bit
151
 
        else
152
 
                status &= ~TIOCM_DTR;      // clear the DTR bit
153
 
 
154
 
        if (rtscts == false) {            // rts OK for ptt if RTSCTS not used
155
 
                if (rts)
156
 
                        status |= TIOCM_RTS;            // set the RTS bit
157
 
                else
158
 
                        status &= ~TIOCM_RTS;      // clear the RTS bit
159
 
        }
160
 
        ioctl(fd, TIOCMSET, &status);
161
 
 
162
 
        FlushBuffer();
163
 
 
164
 
        return true;
165
 
}
166
 
 
167
 
///////////////////////////////////////////////////////
168
 
// Function name        : Cserial::setPTT
169
 
// Return type    : void
170
 
///////////////////////////////////////////////////////
171
 
void Cserial::SetPTT(bool ON)
172
 
{
173
 
        if (fd < 0) {
174
 
          LOG_DEBUG("ptt fd < 0");
175
 
                return;
176
 
        }
177
 
        if (dtrptt || rtsptt) {
178
 
                ioctl(fd, TIOCMGET, &status);
179
 
                if (ON) {                                                                 // ptt enabled
180
 
                        if (dtrptt && dtr)  status &= ~TIOCM_DTR;        // toggle low
181
 
                        if (dtrptt && !dtr) status |= TIOCM_DTR;          // toggle high
182
 
                        if (!rtscts) {
183
 
                                if (rtsptt && rts)  status &= ~TIOCM_RTS; // toggle low
184
 
                                if (rtsptt && !rts) status |= TIOCM_RTS;  // toggle high
185
 
                        }
186
 
                } else {                                                                                  // ptt disabled
187
 
                        if (dtrptt && dtr)  status |= TIOCM_DTR;          // toggle high
188
 
                        if (dtrptt && !dtr) status &= ~TIOCM_DTR;        // toggle low
189
 
                        if (!rtscts) {
190
 
                                if (rtsptt && rts)  status |= TIOCM_RTS;  // toggle high
191
 
                                if (rtsptt && !rts) status &= ~TIOCM_RTS; // toggle low
192
 
                        }
193
 
                }
194
 
                LOG_INFO("PTT %d, DTRptt %d, DTR %d, RTSptt %d, RTS %d, RTSCTS %d, status %2X",
195
 
                          ON, dtrptt, dtr, rtsptt, rts, rtscts, status);
196
 
                ioctl(fd, TIOCMSET, &status);
197
 
        }
198
 
//      LOG_DEBUG("No ptt specified");
199
 
}
200
 
 
201
 
void Cserial::setRTS(bool b)
202
 
{
203
 
        if (fd < 0)
204
 
                return;
205
 
 
206
 
        ioctl(fd, TIOCMGET, &status);
207
 
        if (b == true) 
208
 
                status |= TIOCM_RTS;  // toggle high
209
 
        else 
210
 
                status &= ~TIOCM_RTS; // toggle low
211
 
        ioctl(fd, TIOCMSET, &status);
212
 
 
213
 
}
214
 
 
215
 
void Cserial::setDTR(bool b)
216
 
{
217
 
        if (fd < 0)
218
 
                return;
219
 
 
220
 
        ioctl(fd, TIOCMGET, &status);
221
 
        if (b == true)
222
 
                status |= TIOCM_DTR;      // toggle high
223
 
        else
224
 
                status &= ~TIOCM_DTR;    // toggle low
225
 
        ioctl(fd, TIOCMSET, &status);
226
 
 
227
 
}
228
 
 
229
 
///////////////////////////////////////////////////////
230
 
// Function name        : Cserial::ClosePort
231
 
// Description    : Closes the Port
232
 
// Return type    : void
233
 
///////////////////////////////////////////////////////
234
 
void Cserial::ClosePort()
235
 
{
236
 
        if (fd < 0) return;
237
 
        int myfd = fd;
238
 
        fd = -1;
239
 
//  LOG_INFO("Serial port closed, fd = %d", myfd);
240
 
        ioctl(myfd, TIOCMSET, &origstatus);
241
 
        tcsetattr (myfd, TCSANOW, &oldtio);
242
 
        close(myfd);
243
 
        fd = -1;
244
 
        return;
245
 
}
246
 
 
247
 
bool  Cserial::IOselect ()
248
 
{
249
 
        fd_set rfds;
250
 
        struct timeval tv;
251
 
        int retval;
252
 
 
253
 
        FD_ZERO (&rfds);
254
 
        FD_SET (fd, &rfds);
255
 
        tv.tv_sec = timeout/1000;
256
 
        tv.tv_usec = (timeout % 1000) * 1000;
257
 
        retval = select (FD_SETSIZE, &rfds, (fd_set *)0, (fd_set *)0, &tv);
258
 
        if (retval <= 0) // no response from serial port or error returned
259
 
                return false;
260
 
        return true;
261
 
}
262
 
 
263
 
 
264
 
///////////////////////////////////////////////////////
265
 
// Function name        : Cserial::ReadBuffer
266
 
// Description    : Reads upto nchars from the selected port
267
 
// Return type    : # characters received
268
 
// Argument              : pointer to buffer; # chars to read
269
 
///////////////////////////////////////////////////////
270
 
int  Cserial::ReadBuffer (char *buf, int nchars)
271
 
{
272
 
        if (fd < 0) return 0;
273
 
        int retnum, nread = 0;
274
 
        while (nchars > 0) {
275
 
                if (!IOselect()) {
276
 
                        return nread;
277
 
                }
278
 
                retnum = read (fd, (char *)(buf + nread), nchars);
279
 
                if (retnum < 0)
280
 
                        return 0;//nread;
281
 
                if (retnum == 0)
282
 
                        return nread;
283
 
                nread += retnum;
284
 
                nchars -= retnum;
285
 
        }
286
 
        return nread;
287
 
}
288
 
 
289
 
///////////////////////////////////////////////////////
290
 
// Function name        : Cserial::WriteBuffer
291
 
// Description    : Writes a string to the selected port
292
 
// Return type    : bool
293
 
// Argument              : BYTE by
294
 
///////////////////////////////////////////////////////
295
 
int Cserial::WriteBuffer(const char *buff, int n)
296
 
{
297
 
        if (fd < 0) return 0;
298
 
        int ret = write (fd, buff, n);
299
 
        return ret;
300
 
}
301
 
 
302
 
///////////////////////////////////////////////////////
303
 
// Function name        : Cserial::WriteByte
304
 
// Description    : Writes a Byte to the selected port
305
 
// Return type    : bool
306
 
// Argument              : BYTE by
307
 
///////////////////////////////////////////////////////
308
 
bool Cserial::WriteByte(char by)
309
 
{
310
 
        if (fd < 0) return false;
311
 
        static char buff[2];
312
 
        buff[0] = by; buff[1] = 0;
313
 
        return (write(fd, buff, 1) == 1);
314
 
}
315
 
 
316
 
///////////////////////////////////////////////////////
317
 
// Function name : Cserial::FlushBuffer
318
 
// Description   : flushes the pending rx chars
319
 
// Return type   : void
320
 
///////////////////////////////////////////////////////
321
 
void Cserial::FlushBuffer()
322
 
{
323
 
        if (fd < 0)
324
 
                return;
325
 
        tcflush (fd, TCIFLUSH);
326
 
}
327
 
 
328
 
//=============================================================================
329
 
// WIN32 serial implementation
330
 
//=============================================================================
331
 
 
332
 
#else // __WIN32__
333
 
 
334
 
using namespace std;
335
 
 
336
 
///////////////////////////////////////////////////////
337
 
// Function name        : Cserial::CheckPort
338
 
// Description    : Checks the port specified by strPortName
339
 
// Return type    : bool
340
 
// Argument              : c_string strPortName
341
 
///////////////////////////////////////////////////////
342
 
bool Cserial::CheckPort(string dev)  {
343
 
        static HANDLE hTest;
344
 
        string COMportname = "//./";
345
 
 
346
 
        COMportname.append(dev);
347
 
 
348
 
        hTest = CreateFile(COMportname.c_str(),
349
 
                          GENERIC_READ | GENERIC_WRITE,
350
 
                          0,
351
 
                          0,
352
 
                          OPEN_EXISTING,
353
 
                          0,
354
 
                          0);
355
 
 
356
 
        if(hTest == INVALID_HANDLE_VALUE)
357
 
                return false;
358
 
        CloseHandle(hTest);
359
 
        return true;
360
 
}
361
 
 
362
 
///////////////////////////////////////////////////////
363
 
// Function name        : Cserial::OpenPort
364
 
// Description    : Opens the port specified by strPortName
365
 
// Return type    : bool
366
 
// Argument              : CString strPortName
367
 
///////////////////////////////////////////////////////
368
 
bool Cserial::OpenPort()
369
 
{
370
 
        string COMportname = "//./";
371
 
        COMportname.append(device);
372
 
 
373
 
        hComm = CreateFile(COMportname.c_str(),
374
 
                          GENERIC_READ | GENERIC_WRITE,
375
 
                          0,
376
 
                          0,
377
 
                          OPEN_EXISTING,
378
 
                          0,
379
 
                          0);
380
 
 
381
 
        if(!hComm)
382
 
                return false;
383
 
 
384
 
        ConfigurePort( baud, 8, false, NOPARITY, stopbits);
385
 
//LOG_INFO("Comm port %s open; handle = %d", device.c_str(), hComm);
386
 
        FlushBuffer();
387
 
        return true;
388
 
}
389
 
 
390
 
 
391
 
///////////////////////////////////////////////////////
392
 
// Function name        : Cserial::ClosePort
393
 
// Description    : Closes the Port
394
 
// Return type    : void
395
 
///////////////////////////////////////////////////////
396
 
void Cserial::ClosePort()
397
 
{
398
 
        if (hComm) {
399
 
                bPortReady = SetCommTimeouts (hComm, &CommTimeoutsSaved);
400
 
                CloseHandle(hComm);
401
 
        }
402
 
        hComm = 0;
403
 
        return;
404
 
}
405
 
 
406
 
bool Cserial::IsOpen()
407
 
{
408
 
        if (!hComm) return false;
409
 
        return true;
410
 
}
411
 
 
412
 
///////////////////////////////////////////////////////
413
 
// Function name        : Cserial::GetBytesRead
414
 
// Description    :
415
 
// Return type    : DWORD
416
 
///////////////////////////////////////////////////////
417
 
DWORD Cserial::GetBytesRead()
418
 
{
419
 
        return nBytesRead;
420
 
}
421
 
 
422
 
///////////////////////////////////////////////////////
423
 
// Function name        : Cserial::GetBytesWritten
424
 
// Description    : returns total number of bytes written to port
425
 
// Return type    : DWORD
426
 
///////////////////////////////////////////////////////
427
 
DWORD Cserial::GetBytesWritten()
428
 
{
429
 
        return nBytesWritten;
430
 
}
431
 
 
432
 
 
433
 
///////////////////////////////////////////////////////
434
 
// Function name        : Cserial::ReadByte
435
 
// Description    : Reads a byte from the selected port
436
 
// Return type    : bool
437
 
// Argument              : BYTE& by
438
 
///////////////////////////////////////////////////////
439
 
bool Cserial::ReadByte(char & by)
440
 
{
441
 
static  BYTE byResByte[1024];
442
 
static  DWORD dwBytesTxD=0;
443
 
 
444
 
        if (!hComm) return false;
445
 
        if (ReadFile (hComm, &byResByte[0], 1, &dwBytesTxD, 0)) {
446
 
                if (dwBytesTxD == 1) {
447
 
                        by = (UCHAR)byResByte[0];
448
 
                        return true;
449
 
                }
450
 
        }
451
 
        by = 0;
452
 
        return false;
453
 
}
454
 
 
455
 
int  Cserial::ReadData (char *buf, int nchars)
456
 
{
457
 
        DWORD dwRead = 0;
458
 
        if (!ReadFile(hComm, buf, nchars, &dwRead, NULL))
459
 
                return 0;
460
 
        return (int) dwRead;
461
 
}
462
 
 
463
 
int Cserial::ReadChars (char *buf, int nchars, int msec)
464
 
{
465
 
        if (msec) Sleep(msec);
466
 
        return ReadData (buf, nchars);
467
 
}
468
 
 
469
 
void Cserial::FlushBuffer()
470
 
{
471
 
#define TX_CLEAR 0x0004L
472
 
#define RX_CLEAR 0x0008L
473
 
        if (!hComm) return;
474
 
        PurgeComm(hComm, RX_CLEAR);
475
 
}
476
 
 
477
 
///////////////////////////////////////////////////////
478
 
// Function name        : Cserial::WriteByte
479
 
// Description    : Writes a Byte to teh selected port
480
 
// Return type    : bool
481
 
// Argument              : BYTE by
482
 
///////////////////////////////////////////////////////
483
 
bool Cserial::WriteByte(char by)
484
 
{
485
 
        if (!hComm) return false;
486
 
        nBytesWritten = 0;
487
 
        if (WriteFile(hComm,&by,1,&nBytesWritten,NULL)==0)
488
 
                return false;
489
 
        return true;
490
 
}
491
 
 
492
 
///////////////////////////////////////////////////////
493
 
// Function name        : Cserial::WriteBuffer
494
 
// Description    : Writes a string to the selected port
495
 
// Return type    : bool
496
 
// Argument              : BYTE by
497
 
///////////////////////////////////////////////////////
498
 
int Cserial::WriteBuffer(const char *buff, int n)
499
 
{
500
 
        if (!hComm)
501
 
                return 0;
502
 
        WriteFile (hComm, buff, n, &nBytesWritten, NULL);
503
 
        return nBytesWritten;
504
 
}
505
 
 
506
 
 
507
 
///////////////////////////////////////////////////////
508
 
// Function name        : Cserial::SetCommunicationTimeouts
509
 
// Description    : Sets the timeout for the selected port
510
 
// Return type    : bool
511
 
// Argument              : DWORD ReadIntervalTimeout
512
 
// Argument              : DWORD ReadTotalTimeoutMultiplier
513
 
// Argument              : DWORD ReadTotalTimeoutConstant
514
 
// Argument              : DWORD WriteTotalTimeoutMultiplier
515
 
// Argument              : DWORD WriteTotalTimeoutConstant
516
 
///////////////////////////////////////////////////////
517
 
bool Cserial::SetCommunicationTimeouts(
518
 
        DWORD ReadIntervalTimeout, // msec
519
 
        DWORD ReadTotalTimeoutMultiplier,
520
 
        DWORD ReadTotalTimeoutConstant,
521
 
        DWORD WriteTotalTimeoutMultiplier,
522
 
        DWORD WriteTotalTimeoutConstant
523
 
)
524
 
{
525
 
        if((bPortReady = GetCommTimeouts (hComm, &CommTimeoutsSaved))==0)   {
526
 
                return false;
527
 
        }
528
 
        LOG_DEBUG("\n\
529
 
Read Interval Timeout............... %ld\n\
530
 
Read Total Timeout Multiplier....... %ld\n\
531
 
Read Total Timeout Constant Timeout. %ld\n\
532
 
Write Total Timeout Constant........ %ld\n\
533
 
Write Total Timeout Multiplier...... %ld",
534
 
        CommTimeoutsSaved.ReadIntervalTimeout,
535
 
        CommTimeoutsSaved.ReadTotalTimeoutMultiplier,
536
 
        CommTimeoutsSaved.ReadTotalTimeoutConstant,
537
 
        CommTimeoutsSaved.WriteTotalTimeoutConstant,
538
 
        CommTimeoutsSaved.WriteTotalTimeoutMultiplier);
539
 
 
540
 
        CommTimeouts.ReadIntervalTimeout = ReadIntervalTimeout;
541
 
        CommTimeouts.ReadTotalTimeoutMultiplier = ReadTotalTimeoutMultiplier;
542
 
        CommTimeouts.ReadTotalTimeoutConstant = ReadTotalTimeoutConstant;
543
 
        CommTimeouts.WriteTotalTimeoutConstant = WriteTotalTimeoutConstant;
544
 
        CommTimeouts.WriteTotalTimeoutMultiplier = WriteTotalTimeoutMultiplier;
545
 
 
546
 
        bPortReady = SetCommTimeouts (hComm, &CommTimeouts);
547
 
 
548
 
        if(bPortReady ==0) {
549
 
                CloseHandle(hComm);
550
 
                return false;
551
 
        }
552
 
 
553
 
        return true;
554
 
}
555
 
 
556
 
/*
557
 
 * Remarks
558
 
 *
559
 
 * WriteTotalTimeoutMultiplier
560
 
 *
561
 
 * The multiplier used to calculate the total time-out period for write
562
 
 * operations, in milliseconds. For each write operation, this value is
563
 
 * multiplied by the number of bytes to be written.
564
 
 *
565
 
 * WriteTotalTimeoutConstant
566
 
 *
567
 
 * A constant used to calculate the total time-out period for write operations,
568
 
 * in milliseconds. For each write operation, this value is added to the product
569
 
 * of the WriteTotalTimeoutMultiplier member and the number of bytes to be
570
 
 * written.
571
 
 *
572
 
 * A value of zero for both the WriteTotalTimeoutMultiplier and
573
 
 * WriteTotalTimeoutConstant members indicates that total time-outs are not
574
 
 * used for write operations.
575
 
 *
576
 
 *
577
 
 * If an application sets ReadIntervalTimeout and ReadTotalTimeoutMultiplier to
578
 
 * MAXDWORD and sets ReadTotalTimeoutConstant to a value greater than zero and
579
 
 * less than MAXDWORD, one of the following occurs when the ReadFile function
580
 
 * is called:
581
 
 *
582
 
 * If there are any bytes in the input buffer, ReadFile returns immediately
583
 
 * with the bytes in the buffer.
584
 
 *
585
 
 * If there are no bytes in the input buffer, ReadFile waits until a byte
586
 
 * arrives and then returns immediately.
587
 
 *
588
 
 * *********************************************************************
589
 
 * 
590
 
 * If no bytes arrive within the time specified by ReadTotalTimeoutConstant,
591
 
 * ReadFile times out.
592
 
 * 
593
 
 * ReadIntervalTimeout
594
 
 *
595
 
 * The maximum time allowed to elapse between the arrival of two bytes on the
596
 
 * communications line, in milliseconds. During a ReadFile operation, the time
597
 
 * period begins when the first byte is received. If the interval between the
598
 
 * arrival of any two bytes exceeds this amount, the ReadFile operation is
599
 
 * completed and any buffered data is returned. A value of zero indicates that
600
 
 * interval time-outs are not used.
601
 
 *
602
 
 * A value of MAXDWORD, combined with zero values for both the
603
 
 * ReadTotalTimeoutConstant and ReadTotalTimeoutMultiplier members, specifies
604
 
 * that the read operation is to return immediately with the bytes that have
605
 
 * already been received, even if no bytes have been received.
606
 
 *
607
 
 * ReadTotalTimeoutMultiplier
608
 
 *
609
 
 * The multiplier used to calculate the total time-out period for read
610
 
 * operations, in milliseconds. For each read operation, this value is
611
 
 * multiplied by the requested number of bytes to be read.
612
 
 *
613
 
 * ReadTotalTimeoutConstant
614
 
 *
615
 
 * A constant used to calculate the total time-out period for read operations,
616
 
 * in milliseconds. For each read operation, this value is added to the product
617
 
 * of the ReadTotalTimeoutMultiplier member and the requested number of bytes.
618
 
 *
619
 
 * A value of zero for both the ReadTotalTimeoutMultiplier and
620
 
 * ReadTotalTimeoutConstant members indicates that total time-outs are not
621
 
 * used for read operations.
622
 
 *
623
 
*/
624
 
 
625
 
bool Cserial::SetCommTimeout() {
626
 
        return SetCommunicationTimeouts (
627
 
                0,              // Read Interval Timeout
628
 
                0,              // Read Total Timeout Multiplier
629
 
                50,             // Read Total Timeout Constant
630
 
                50,             // Write Total Timeout Constant
631
 
                0               // Write Total Timeout Multiplier
632
 
                );
633
 
}
634
 
 
635
 
///////////////////////////////////////////////////////
636
 
// Function name        : ConfigurePort
637
 
// Description    : Configures the Port
638
 
// Return type    : bool
639
 
// Argument              : DWORD BaudRate
640
 
// Argument              : BYTE ByteSize
641
 
// Argument              : DWORD fParity
642
 
// Argument              : BYTE  Parity
643
 
// Argument              : BYTE StopBits
644
 
///////////////////////////////////////////////////////
645
 
bool Cserial::ConfigurePort(
646
 
                DWORD   BaudRate,
647
 
                BYTE    ByteSize,
648
 
                DWORD   dwParity,
649
 
                BYTE    Parity,
650
 
                BYTE    StopBits)
651
 
{
652
 
        if((bPortReady = GetCommState(hComm, &dcb))==0) {
653
 
//        LOG_ERROR("GetCommState Error on %s", device.c_str());
654
 
                CloseHandle(hComm);
655
 
                return false;
656
 
        }
657
 
 
658
 
        dcb.BaudRate                    = BaudRate;
659
 
        dcb.ByteSize                    = ByteSize;
660
 
        dcb.Parity                              = Parity ;
661
 
        dcb.StopBits                    = (StopBits == 1 ? ONESTOPBIT : TWOSTOPBITS);
662
 
        dcb.fBinary                             = true;
663
 
        dcb.fDsrSensitivity             = false;
664
 
        dcb.fParity                             = dwParity;
665
 
        dcb.fOutX                               = false;
666
 
        dcb.fInX                                = false;
667
 
        dcb.fNull                               = false;
668
 
        dcb.fAbortOnError               = true;
669
 
        dcb.fOutxCtsFlow                = false;
670
 
        dcb.fOutxDsrFlow                = false;
671
 
 
672
 
        if (dtr)
673
 
                dcb.fDtrControl = DTR_CONTROL_ENABLE;
674
 
        else
675
 
                dcb.fDtrControl = DTR_CONTROL_DISABLE;
676
 
 
677
 
        dcb.fDsrSensitivity = false;
678
 
 
679
 
        if (rtscts) dcb.fRtsControl = RTS_CONTROL_ENABLE;
680
 
        else {
681
 
                if (rts)
682
 
                        dcb.fRtsControl = RTS_CONTROL_ENABLE;
683
 
                else
684
 
                        dcb.fRtsControl = RTS_CONTROL_DISABLE;
685
 
        }
686
 
 
687
 
        bPortReady = SetCommState(hComm, &dcb);
688
 
        if(bPortReady == 0) {
689
 
                CloseHandle(hComm);
690
 
                return false;
691
 
        }
692
 
  return SetCommTimeout();
693
 
}
694
 
 
695
 
///////////////////////////////////////////////////////
696
 
// Function name        : Cserial::setPTT
697
 
// Return type    : void
698
 
///////////////////////////////////////////////////////
699
 
void Cserial::SetPTT(bool ON)
700
 
{
701
 
        if(!hComm) {
702
 
                LOG_ERROR("Invalid handle");
703
 
                return;
704
 
        }
705
 
        if ( !(dtrptt || rtsptt) )
706
 
                return;
707
 
 
708
 
        if (ON) {
709
 
                if (dtrptt && dtr)
710
 
                        dcb.fDtrControl = DTR_CONTROL_DISABLE;
711
 
                if (dtrptt && !dtr)
712
 
                        dcb.fDtrControl = DTR_CONTROL_ENABLE;
713
 
                if (!rtscts) {
714
 
                        if (rtsptt && rts)
715
 
                                dcb.fRtsControl = RTS_CONTROL_DISABLE;
716
 
                        if (rtsptt && !rts)
717
 
                                dcb.fRtsControl = RTS_CONTROL_ENABLE;
718
 
                }
719
 
        } else {
720
 
                if (dtrptt && dtr)
721
 
                        dcb.fDtrControl = DTR_CONTROL_ENABLE;
722
 
                if (dtrptt && !dtr)
723
 
                        dcb.fDtrControl = DTR_CONTROL_DISABLE;
724
 
                if (!rtscts) {
725
 
                        if (rtsptt && rts)
726
 
                                dcb.fRtsControl = RTS_CONTROL_ENABLE;
727
 
                        if (rtsptt && !rts)
728
 
                                dcb.fRtsControl = RTS_CONTROL_DISABLE;
729
 
                }
730
 
        }
731
 
 
732
 
        LOG_INFO("PTT %d, DTRptt %d, DTR %d, RTSptt %d, RTS %d, RTSCTS %d, %2x %2x",
733
 
                ON, dtrptt, dtr, rtsptt, rts, rtscts, 
734
 
                static_cast<unsigned int>(dcb.fDtrControl), 
735
 
                static_cast<unsigned int>(dcb.fRtsControl) );
736
 
 
737
 
        SetCommState(hComm, &dcb);
738
 
}
739
 
 
740
 
void Cserial::setRTS(bool b)
741
 
{
742
 
        if(!hComm)
743
 
                return;
744
 
 
745
 
        if (b == true)
746
 
                dcb.fRtsControl = RTS_CONTROL_ENABLE;
747
 
        else
748
 
                dcb.fRtsControl = RTS_CONTROL_DISABLE;
749
 
 
750
 
        SetCommState(hComm, &dcb);
751
 
}
752
 
 
753
 
void Cserial::setDTR(bool b)
754
 
{
755
 
        if(!hComm)
756
 
                return;
757
 
 
758
 
        if (b == true)
759
 
                dcb.fDtrControl = DTR_CONTROL_ENABLE;
760
 
        else
761
 
                dcb.fDtrControl = DTR_CONTROL_DISABLE;
762
 
 
763
 
        SetCommState(hComm, &dcb);
764
 
}
765
 
 
766
 
#endif