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

« back to all changes in this revision

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