2 Copyright 2006 Johnathan Corgan.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License version 2
6 as published by the Free Software Foundation.
8 This software is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with GNU Radio; see the file COPYING. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street,
16 Boston, MA 02110-1301, USA.
29 #include <sys/ioctl.h>
32 wxArrayString EnumerateSerialPorts()
38 for (int i = 1; i <= 8; i++) {
39 port.Printf("COM%i", i);
43 result.Add(_T("/dev/ttyS0"));
44 result.Add(_T("/dev/ttyS1"));
45 result.Add(_T("/dev/ttyS2"));
46 result.Add(_T("/dev/ttyS3"));
47 result.Add(_T("/dev/ttyUSB0"));
48 result.Add(_T("/dev/ttyUSB1"));
54 SerialPort::SerialPort(wxString &port)
56 wxLogDebug(_T("SerialPort::SerialPort(): %s"), port.c_str());
60 m_handle = INVALID_HANDLE_VALUE;
66 bool SerialPort::Open(int speed)
68 wxLogDebug(_T("SerialPort::Open: %i baud"), speed);
70 wxLogWarning(_T("SerialPort::Open: called on already opened object."));
75 m_handle = CreateFile(m_port.c_str(),
76 GENERIC_READ | GENERIC_WRITE,
82 if (m_handle == INVALID_HANDLE_VALUE) {
83 wxLogError("SerialPort::Open: CreateFile() failed");
88 if (!GetCommState(m_handle, &dcb)) {
89 wxLogError("SerialPort::Open: GetCommState failed.");
90 CloseHandle(m_handle);
96 dcb.StopBits = ONESTOPBIT;
97 dcb.Parity = NOPARITY;
101 if (!SetCommState(m_handle, &dcb)) {
102 wxLogError("SerialPort::Open: SetCommState failed.");
103 CloseHandle(m_handle);
107 COMMTIMEOUTS timeouts;
108 if (!GetCommTimeouts(m_handle, &timeouts)) {
109 wxLogError("SerialPort::Open: GetCommTimeouts failed.");
110 CloseHandle(m_handle);
114 timeouts.ReadIntervalTimeout = MAXDWORD;
115 timeouts.ReadTotalTimeoutMultiplier = MAXDWORD;
116 timeouts.ReadTotalTimeoutConstant = 100;
118 if (!SetCommTimeouts(m_handle, &timeouts)) {
119 wxLogError("SerialPort::Open: SetCommTimeouts failed.");
120 CloseHandle(m_handle);
126 // Fixed at first USB port until string bug fixed
127 m_fd = open("/dev/ttyUSB0", O_RDWR|O_NONBLOCK);
129 wxLogError(_T("SerialPort::Open: open(): %i"), errno);
134 wxLogError(_T("SerialPort::Open: device %s is not a tty"), m_port.c_str());
139 if (tcgetattr(m_fd, &m_ttyset_old) != 0) {
140 wxLogError(_T("SerialPort::Open: failed to get port attributes"));
145 memcpy(&m_ttyset_new, &m_ttyset_old, sizeof(m_ttyset_new));
146 cfsetispeed(&m_ttyset_new, (speed_t)speed);
147 cfsetospeed(&m_ttyset_new, (speed_t)speed);
148 m_ttyset_new.c_cflag &= ~(PARENB|CRTSCTS); // Disable parity and flowcontrol
149 m_ttyset_new.c_cflag |= CS8|CREAD|CLOCAL; // 8 bits, read, no modem status lines
150 m_ttyset_new.c_iflag = 0;
151 m_ttyset_new.c_oflag = ONLCR; // Convert LF to CRLF on receive
152 m_ttyset_new.c_lflag = 0;
154 if (tcsetattr(m_fd, TCSANOW, &m_ttyset_new) != 0) {
155 wxLogError(_T("SerialPort::Open: failed to set port attributes"));
165 void SerialPort::Close()
167 wxLogDebug(_T("SerialPort::Close()"));
169 CloseHandle(m_handle);
172 m_ttyset_old.c_cflag |= HUPCL;
173 tcsetattr(m_fd, TCSANOW, &m_ttyset_old);
181 SerialPort::~SerialPort()
183 wxLogDebug(_T("SerialPort::~SerialPort()"));
189 int SerialPort::RxReady()
193 return 1; // No equivalent Win32 call, use read timeouts instead
195 if (m_fd < 0 || ioctl(m_fd, FIONREAD, &count) < 0)
201 int SerialPort::Read(char *buffer, int len)
211 if (ReadFile(m_handle, buffer, (DWORD)len, &num, NULL))
216 return read(m_fd, buffer, len);