Debian Codegraph @

ccomport.cxx

raw symbols SCIP
1 /***************************************************************************
2 ccomport.cxx - Class for handling serial comms.
3
4 begin : Wed Sep 25 18:58:35 BST 2002
5 copyright : (C) 2002 by Greg Reynolds, Jim Hawkridge
6 email : [email protected]
7
8 2005-11-17: changes by Thomas Fischl <[email protected]>
9 - added method flash()
10
11 ***************************************************************************/
12
13 /***************************************************************************
14 * *
15 * This program is free software; you can redistribute it and/or modify *
16 * it under the terms of the GNU General Public License as published by *
17 * the Free Software Foundation; either version 2 of the License, or *
18 * (at your option) any later version. *
19 * *
20 ***************************************************************************/
21 #include "ccomport.h"
22
23 CCOMPort::CCOMPort()
24 {
25 m_fd = -1;
26 memset(&m_trmOptions, 0, sizeof(termios));
27 }
28
29 CCOMPort::~CCOMPort()
30 {
31
32 }
33
34
35 /* open the port */
36 int CCOMPort::openPort(const char* pszDev)
37 {
38 assert( pszDev );
39
40 /* open the port */
41 m_fd = open(pszDev, O_RDWR | O_NOCTTY | O_NDELAY);
42
43 if( m_fd == -1 ) {
44 perror("\nCCOMPort::openPort: Unable to open specified port.");
45 return 0;
46 }
47
48 /* read is blocking */
49 fcntl(m_fd, F_SETFL, 0);
50
51
52 return 1;
53 }
54
55 /* close the port */
56 int CCOMPort::closePort()
57 {
58 assert( m_fd != -1 );
59
60 if( close(m_fd) == -1 ) {
61 perror("\nCCOMPort::close:: Couldn't close the open port.");
62 return 0;
63 }
64
65 return 1;
66 }
67
68 /* set baud rate */
69 int CCOMPort::setPortOptions(speed_t spdBaud)
70 {
71 assert( m_fd != -1);
72
73 /* retrieve the current options */
74 tcgetattr(m_fd, &m_trmOptions);
75
76 /* set baudrate */
77 cfsetispeed(&m_trmOptions, spdBaud);
78 cfsetospeed(&m_trmOptions, spdBaud);
79
80 /* set 8 bit, no parity, 1 stop bit */
81 m_trmOptions.c_cflag &= ~PARENB;
82 m_trmOptions.c_cflag &= ~CSTOPB;
83 m_trmOptions.c_cflag &= ~CSIZE;
84 m_trmOptions.c_cflag |= CS8;
85
86 /* enable software flow control */
87 m_trmOptions.c_iflag |= (IXON | IXOFF | IXANY);
88
89 /* chose raw input */
90 m_trmOptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
91
92 /* choose raw output */
93 m_trmOptions.c_oflag &= ~OPOST;
94
95 /* apply the options back to the port */
96 tcsetattr(m_fd, TCSANOW, &m_trmOptions);
97
98 return 1;
99 }
100
101 /* send data */
102 int CCOMPort::sendBytes(const unsigned char* pBuffer, int nBytes)
103 {
104 assert( pBuffer );
105 assert( nBytes > 0 );
106 assert( m_fd );
107
108 /* write the data */
109 if( write(m_fd, pBuffer, nBytes) != nBytes ) {
110 perror("\nCCOMPort::sendBytes::write(...) failed.");
111 return 0;
112 }
113
114 return 1;
115 }
116
117 #define SSIZE_MAX 20
118
119 /* read back some data */
120 int CCOMPort::readBytes(unsigned char* pBuffer, int nBytes)
121 {
122 assert( pBuffer );
123 assert( m_fd );
124 int n;
125
126 // /* do it efficiently if necessary */
127 // if( nBytes < SSIZE_MAX ) {
128
129 // /* get back nBytes */
130 // if( read(m_fd, pBuffer, nBytes) != nBytes ) {
131 // perror("\nCCOMPort::readBytes::read failed to return correct byte count.");
132 // return 0;
133 // }
134 // }
135
136 /* read them back one at time */
137 // else {
138 {
139
140 for( n = 0; n < nBytes; n++ ) {
141
142 /* read the byte */
143 if( read(m_fd, &pBuffer[n], 1) == -1 ) {
144 perror("\nCCOMPort::readBytes::read failed in biger than SSIZE_MAX.");
145 return 0;
146 }
147 }
148 }
149
150 return 1;
151 }
152
153 int CCOMPort::flush()
154 {
155 assert( m_fd );
156
157 tcflush(m_fd, TCIOFLUSH);
158
159 return 1;
160 }
161