| 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"cxx . . $ `<file>/ccomport.h`/ |
| 22 | |
| 23 | CCOMPortcxx . . $ CCOMPort#::CCOMPortcxx . . $ CCOMPort#CCOMPort(49f6e7a06ebc5aa8).() |
| 24 | { |
| 25 | m_fdcxx . . $ CCOMPort#m_fd. = -1; |
| 26 | memset(&m_trmOptionscxx . . $ CCOMPort#m_trmOptions., 0, sizeof(termios)); |
| 27 | } |
| 28 | |
| 29 | CCOMPortcxx . . $ CCOMPort#::~cxx . . $ CCOMPort#`~CCOMPort`(ced63f7c635d850d).CCOMPortcxx . . $ CCOMPort#() |
| 30 | { |
| 31 | |
| 32 | } |
| 33 | |
| 34 | |
| 35 | /* open the port */ |
| 36 | int CCOMPortcxx . . $ CCOMPort#::openPortcxx . . $ CCOMPort#openPort(5c4306d8553b3696).(const char* pszDev) |
| 37 | { |
| 38 | assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!( pszDev ); |
| 39 | |
| 40 | /* open the port */ |
| 41 | m_fdcxx . . $ CCOMPort#m_fd. = open(pszDev, O_RDWRcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:45:9`! | O_NOCTTYcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:53:10`! | O_NDELAYcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:65:10`!); |
| 42 | |
| 43 | if( m_fdcxx . . $ CCOMPort#m_fd. == -1 ) { |
| 44 | perror("\nCCOMPort::openPort: Unable to open specified port."); |
| 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | /* read is blocking */ |
| 49 | fcntl(m_fdcxx . . $ CCOMPort#m_fd., F_SETFLcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:169:9`!, 0); |
| 50 | |
| 51 | |
| 52 | return 1; |
| 53 | } |
| 54 | |
| 55 | /* close the port */ |
| 56 | int CCOMPortcxx . . $ CCOMPort#::closePortcxx . . $ CCOMPort#closePort(b126dc7c1de90089).() |
| 57 | { |
| 58 | assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!( m_fd != -1 ); |
| 59 | |
| 60 | if( close(m_fdcxx . . $ CCOMPort#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 CCOMPortcxx . . $ CCOMPort#::setPortOptionscxx . . $ CCOMPort#setPortOptions(4c679297bf94b9eb).(speed_t spdBaud) |
| 70 | { |
| 71 | assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!( m_fd != -1); |
| 72 | |
| 73 | /* retrieve the current options */ |
| 74 | tcgetattr(m_fdcxx . . $ CCOMPort#m_fd., &m_trmOptionscxx . . $ CCOMPort#m_trmOptions.); |
| 75 | |
| 76 | /* set baudrate */ |
| 77 | cfsetispeed(&m_trmOptionscxx . . $ CCOMPort#m_trmOptions., spdBaud); |
| 78 | cfsetospeed(&m_trmOptionscxx . . $ CCOMPort#m_trmOptions., spdBaud); |
| 79 | |
| 80 | /* set 8 bit, no parity, 1 stop bit */ |
| 81 | m_trmOptionscxx . . $ CCOMPort#m_trmOptions..c_cflag &= ~PARENBcxx . . $ `<external>/934398879100599903/termios-c_cflag.h:31:9`!; |
| 82 | m_trmOptionscxx . . $ CCOMPort#m_trmOptions..c_cflag &= ~CSTOPBcxx . . $ `<external>/934398879100599903/termios-c_cflag.h:29:9`!; |
| 83 | m_trmOptionscxx . . $ CCOMPort#m_trmOptions..c_cflag &= ~CSIZEcxx . . $ `<external>/934398879100599903/termios-c_cflag.h:24:9`!; |
| 84 | m_trmOptionscxx . . $ CCOMPort#m_trmOptions..c_cflag |= CS8cxx . . $ `<external>/934398879100599903/termios-c_cflag.h:28:11`!; |
| 85 | |
| 86 | /* enable software flow control */ |
| 87 | m_trmOptionscxx . . $ CCOMPort#m_trmOptions..c_iflag |= (IXONcxx . . $ `<external>/8770703639284775376/termios-c_iflag.h:35:9`! | IXOFFcxx . . $ `<external>/8770703639284775376/termios-c_iflag.h:37:9`! | IXANYcxx . . $ `<external>/8770703639284775376/termios-c_iflag.h:36:9`!); |
| 88 | |
| 89 | /* chose raw input */ |
| 90 | m_trmOptionscxx . . $ CCOMPort#m_trmOptions..c_lflag &= ~(ICANONcxx . . $ `<external>/2220421410900157355/termios-c_lflag.h:25:9`! | ECHOcxx . . $ `<external>/2220421410900157355/termios-c_lflag.h:29:9`! | ECHOEcxx . . $ `<external>/2220421410900157355/termios-c_lflag.h:30:9`! | ISIGcxx . . $ `<external>/2220421410900157355/termios-c_lflag.h:24:9`!); |
| 91 | |
| 92 | /* choose raw output */ |
| 93 | m_trmOptionscxx . . $ CCOMPort#m_trmOptions..c_oflag &= ~OPOSTcxx . . $ `<external>/8246223416874974685/termios-c_oflag.h:24:9`!; |
| 94 | |
| 95 | /* apply the options back to the port */ |
| 96 | tcsetattr(m_fdcxx . . $ CCOMPort#m_fd., TCSANOWcxx . . $ `<external>/7740429199341890421/termios-tcflow.h:24:9`!, &m_trmOptionscxx . . $ CCOMPort#m_trmOptions.); |
| 97 | |
| 98 | return 1; |
| 99 | } |
| 100 | |
| 101 | /* send data */ |
| 102 | int CCOMPortcxx . . $ CCOMPort#::sendBytescxx . . $ CCOMPort#sendBytes(87fdc3b2e2b212a8).(const unsigned char* pBuffer, int nBytes) |
| 103 | { |
| 104 | assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!( pBuffer ); |
| 105 | assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!( nBytes > 0 ); |
| 106 | assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!( m_fd ); |
| 107 | |
| 108 | /* write the data */ |
| 109 | if( write(m_fdcxx . . $ CCOMPort#m_fd., pBuffer, nBytes) != nBytes ) { |
| 110 | perror("\nCCOMPort::sendBytes::write(...) failed."); |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | return 1; |
| 115 | } |
| 116 | |
| 117 | #define SSIZE_MAXcxx . . $ `ccomport.cxx:117:9`! 20 |
| 118 | |
| 119 | /* read back some data */ |
| 120 | int CCOMPortcxx . . $ CCOMPort#::readBytescxx . . $ CCOMPort#readBytes(7405b416d0535413).(unsigned char* pBuffer, int nBytes) |
| 121 | { |
| 122 | assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!( pBuffer ); |
| 123 | assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!( 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_fdcxx . . $ CCOMPort#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 CCOMPortcxx . . $ CCOMPort#::flushcxx . . $ CCOMPort#flush(b126dc7c1de90089).() |
| 154 | { |
| 155 | assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!( m_fd ); |
| 156 | |
| 157 | tcflush(m_fdcxx . . $ CCOMPort#m_fd., TCIOFLUSHcxx . . $ `<external>/17989485081294502337/termios.h:78:9`!); |
| 158 | |
| 159 | return 1; |
| 160 | } |
| 161 |