You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
451 lines
9.1 KiB
451 lines
9.1 KiB
/*
|
|
* Copyright (C) 2002-2004,2007-2011,2013,2015 by Jonathan Naylor G4KLX
|
|
* Copyright (C) 1999-2001 by Thomas Sailor HB9JNX
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program; if not, write to the Free Software
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
*/
|
|
|
|
#include "SerialLineController.h"
|
|
|
|
#if !defined(_WIN32)
|
|
#include <sys/types.h>
|
|
#include <sys/ioctl.h>
|
|
#include <sys/stat.h>
|
|
#include <cerrno>
|
|
#include <fcntl.h>
|
|
#include <unistd.h>
|
|
#include <termios.h>
|
|
#endif
|
|
|
|
|
|
#if !defined(_WIN32)
|
|
|
|
CSerialLineController::CSerialLineController(const std::string& device, unsigned int config) :
|
|
m_device(device),
|
|
m_config(config),
|
|
m_rts(false),
|
|
m_dtr(false),
|
|
m_fd(-1)
|
|
{
|
|
assert(!device.empty());
|
|
assert(config == 1U || config == 2U || config == 3U);
|
|
}
|
|
|
|
CSerialLineController::~CSerialLineController()
|
|
{
|
|
}
|
|
|
|
bool CSerialLineController::open()
|
|
{
|
|
assert(m_fd == -1);
|
|
|
|
m_fd = ::open(m_device.c_str(), O_RDWR | O_NOCTTY | O_NDELAY, 0);
|
|
if (m_fd < 0) {
|
|
::fprintf(stderr, "Cannot open device - %s\n", m_device.c_str());
|
|
return false;
|
|
}
|
|
|
|
if (::isatty(m_fd) == 0) {
|
|
::fprintf(stderr, "%s is not a TTY device\n", m_device.c_str());
|
|
::close(m_fd);
|
|
return false;
|
|
}
|
|
|
|
termios termios;
|
|
if (::tcgetattr(m_fd, &termios) < 0) {
|
|
::fprintf(stderr, "Cannot get the attributes for %s\n", m_device.c_str());
|
|
::close(m_fd);
|
|
return false;
|
|
}
|
|
|
|
// FIXME XXX unfinished
|
|
termios.c_cflag |= (CLOCAL | CREAD);
|
|
termios.c_cflag &= ~CRTSCTS;
|
|
termios.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
|
|
termios.c_oflag &= ~OPOST;
|
|
termios.c_cc[VMIN] = 0;
|
|
termios.c_cc[VTIME] = 10;
|
|
|
|
if (::tcsetattr(m_fd, TCSANOW, &termios) < 0) {
|
|
::fprintf(stderr, "Cannot set the attributes for %s\n", m_device.c_str());
|
|
::close(m_fd);
|
|
return false;
|
|
}
|
|
|
|
unsigned int y;
|
|
if (::ioctl(m_fd, TIOCMGET, &y) < 0) {
|
|
::fprintf(stderr, "Cannot get the modem status bits for %s\n", m_device.c_str());
|
|
::close(m_fd);
|
|
return false;
|
|
}
|
|
|
|
y &= ~TIOCM_DTR;
|
|
y &= ~TIOCM_RTS;
|
|
|
|
if (::ioctl(m_fd, TIOCMSET, &y) < 0) {
|
|
::fprintf(stderr, "Cannot set the modem status bits for %s\n", m_device.c_str());
|
|
::close(m_fd);
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
bool CSerialLineController::getCD() const
|
|
{
|
|
assert(m_fd != -1);
|
|
|
|
unsigned int y;
|
|
if (::ioctl(m_fd, TIOCMGET, &y) < 0)
|
|
return false;
|
|
|
|
return (y & TIOCM_CD) == TIOCM_CD;
|
|
}
|
|
|
|
bool CSerialLineController::getCTS() const
|
|
{
|
|
assert(m_fd != -1);
|
|
|
|
unsigned int y;
|
|
if (::ioctl(m_fd, TIOCMGET, &y) < 0)
|
|
return false;
|
|
|
|
return (y & TIOCM_CTS) == TIOCM_CTS;
|
|
}
|
|
|
|
bool CSerialLineController::getDSR() const
|
|
{
|
|
assert(m_fd != -1);
|
|
|
|
unsigned int y;
|
|
if (::ioctl(m_fd, TIOCMGET, &y) < 0)
|
|
return false;
|
|
|
|
return (y & TIOCM_DSR) == TIOCM_DSR;
|
|
}
|
|
|
|
bool CSerialLineController::setRTS(bool set)
|
|
{
|
|
assert(m_fd != -1);
|
|
|
|
if (set == m_rts)
|
|
return true;
|
|
|
|
unsigned int y;
|
|
if (::ioctl(m_fd, TIOCMGET, &y) < 0)
|
|
return false;
|
|
|
|
if (set)
|
|
y |= TIOCM_RTS;
|
|
else
|
|
y &= ~TIOCM_RTS;
|
|
|
|
if (::ioctl(m_fd, TIOCMSET, &y) < 0)
|
|
return false;
|
|
|
|
m_rts = set;
|
|
|
|
return true;
|
|
}
|
|
|
|
bool CSerialLineController::setDTR(bool set)
|
|
{
|
|
assert(m_fd != -1);
|
|
|
|
if (set == m_dtr)
|
|
return true;
|
|
|
|
unsigned int y;
|
|
if (::ioctl(m_fd, TIOCMGET, &y) < 0)
|
|
return false;
|
|
|
|
if (set)
|
|
y |= TIOCM_DTR;
|
|
else
|
|
y &= ~TIOCM_DTR;
|
|
|
|
if (::ioctl(m_fd, TIOCMSET, &y) < 0)
|
|
return false;
|
|
|
|
m_dtr = set;
|
|
|
|
return true;
|
|
}
|
|
|
|
void CSerialLineController::getDigitalInputs(bool& inp1, bool& inp2, bool& inp3, bool& inp4, bool& inp5)
|
|
{
|
|
assert(m_fd != -1);
|
|
|
|
inp1 = inp2 = inp3 = inp4 = inp5 = false;
|
|
|
|
unsigned int y;
|
|
if (::ioctl(m_fd, TIOCMGET, &y) < 0)
|
|
return;
|
|
|
|
switch (m_config) {
|
|
case 1U:
|
|
inp1 = (y & TIOCM_DSR) == TIOCM_DSR;
|
|
inp2 = (y & TIOCM_CTS) == TIOCM_CTS;
|
|
break;
|
|
case 2U:
|
|
inp1 = (y & TIOCM_CD) == TIOCM_CD;
|
|
inp2 = (y & TIOCM_CD) == TIOCM_CD;
|
|
break;
|
|
case 3U:
|
|
inp1 = (y & TIOCM_CD) == TIOCM_CD;
|
|
break;
|
|
default:
|
|
::fprintf(stderr, "Unknown serial config - %u\n", m_config);
|
|
break;
|
|
}
|
|
}
|
|
|
|
void CSerialLineController::setDigitalOutputs(bool outp1, bool, bool outp3, bool, bool, bool, bool, bool)
|
|
{
|
|
assert(m_fd != -1);
|
|
|
|
if (m_config == 1U) {
|
|
if (outp1 == m_dtr && outp3 == m_rts)
|
|
return;
|
|
|
|
unsigned int y;
|
|
if (::ioctl(m_fd, TIOCMGET, &y) < 0)
|
|
return;
|
|
|
|
if (outp1)
|
|
y |= TIOCM_DTR;
|
|
else
|
|
y &= ~TIOCM_DTR;
|
|
|
|
if (outp3)
|
|
y |= TIOCM_RTS;
|
|
else
|
|
y &= ~TIOCM_RTS;
|
|
|
|
if (::ioctl(m_fd, TIOCMSET, &y) < 0)
|
|
return;
|
|
|
|
m_dtr = outp1;
|
|
m_rts = outp3;
|
|
} else if (m_config == 2U || m_config == 3U) {
|
|
if (outp1 == m_rts && outp3 == m_dtr)
|
|
return;
|
|
|
|
unsigned int y;
|
|
if (::ioctl(m_fd, TIOCMGET, &y) < 0)
|
|
return;
|
|
|
|
if (outp1)
|
|
y |= TIOCM_RTS;
|
|
else
|
|
y &= ~TIOCM_RTS;
|
|
|
|
if (outp3)
|
|
y |= TIOCM_DTR;
|
|
else
|
|
y &= ~TIOCM_DTR;
|
|
|
|
if (::ioctl(m_fd, TIOCMSET, &y) < 0)
|
|
return;
|
|
|
|
m_rts = outp1;
|
|
m_dtr = outp3;
|
|
} else {
|
|
::fprintf(stderr, "Unknown serial config - %u\n", m_config);
|
|
}
|
|
}
|
|
|
|
void CSerialLineController::close()
|
|
{
|
|
assert(m_fd != -1);
|
|
|
|
::close(m_fd);
|
|
m_fd = -1;
|
|
}
|
|
|
|
#else // _WIN32
|
|
|
|
CSerialLineController::CSerialLineController(const std::string& device, unsigned int config) :
|
|
m_device(device),
|
|
m_config(config),
|
|
m_rts(false),
|
|
m_dtr(false),
|
|
m_handle(INVALID_HANDLE_VALUE)
|
|
{
|
|
assert(!device.empty());
|
|
assert(config == 1U || config == 2U || config == 3U);
|
|
}
|
|
|
|
CSerialLineController::~CSerialLineController()
|
|
{
|
|
}
|
|
|
|
bool CSerialLineController::open()
|
|
{
|
|
assert(m_handle == INVALID_HANDLE_VALUE);
|
|
|
|
// On Windows, ports above COM9 require the \\.\COMn prefix.
|
|
std::string path = "\\\\.\\" + m_device;
|
|
|
|
m_handle = ::CreateFileA(path.c_str(),
|
|
GENERIC_READ | GENERIC_WRITE,
|
|
0,
|
|
nullptr,
|
|
OPEN_EXISTING,
|
|
FILE_ATTRIBUTE_NORMAL,
|
|
nullptr);
|
|
if (m_handle == INVALID_HANDLE_VALUE) {
|
|
::fprintf(stderr, "Cannot open device - %s\n", m_device.c_str());
|
|
return false;
|
|
}
|
|
|
|
// Deassert both DTR and RTS on open, matching the POSIX behaviour.
|
|
::EscapeCommFunction(m_handle, CLRDTR);
|
|
::EscapeCommFunction(m_handle, CLRRTS);
|
|
|
|
m_rts = false;
|
|
m_dtr = false;
|
|
|
|
return true;
|
|
}
|
|
|
|
bool CSerialLineController::getCD() const
|
|
{
|
|
assert(m_handle != INVALID_HANDLE_VALUE);
|
|
|
|
DWORD status = 0U;
|
|
if (!::GetCommModemStatus(m_handle, &status))
|
|
return false;
|
|
|
|
return (status & MS_RLSD_ON) != 0U;
|
|
}
|
|
|
|
bool CSerialLineController::getCTS() const
|
|
{
|
|
assert(m_handle != INVALID_HANDLE_VALUE);
|
|
|
|
DWORD status = 0U;
|
|
if (!::GetCommModemStatus(m_handle, &status))
|
|
return false;
|
|
|
|
return (status & MS_CTS_ON) != 0U;
|
|
}
|
|
|
|
bool CSerialLineController::getDSR() const
|
|
{
|
|
assert(m_handle != INVALID_HANDLE_VALUE);
|
|
|
|
DWORD status = 0U;
|
|
if (!::GetCommModemStatus(m_handle, &status))
|
|
return false;
|
|
|
|
return (status & MS_DSR_ON) != 0U;
|
|
}
|
|
|
|
bool CSerialLineController::setRTS(bool set)
|
|
{
|
|
assert(m_handle != INVALID_HANDLE_VALUE);
|
|
|
|
if (set == m_rts)
|
|
return true;
|
|
|
|
if (!::EscapeCommFunction(m_handle, set ? SETRTS : CLRRTS))
|
|
return false;
|
|
|
|
m_rts = set;
|
|
|
|
return true;
|
|
}
|
|
|
|
bool CSerialLineController::setDTR(bool set)
|
|
{
|
|
assert(m_handle != INVALID_HANDLE_VALUE);
|
|
|
|
if (set == m_dtr)
|
|
return true;
|
|
|
|
if (!::EscapeCommFunction(m_handle, set ? SETDTR : CLRDTR))
|
|
return false;
|
|
|
|
m_dtr = set;
|
|
|
|
return true;
|
|
}
|
|
|
|
void CSerialLineController::getDigitalInputs(bool& inp1, bool& inp2, bool& inp3, bool& inp4, bool& inp5)
|
|
{
|
|
assert(m_handle != INVALID_HANDLE_VALUE);
|
|
|
|
inp1 = inp2 = inp3 = inp4 = inp5 = false;
|
|
|
|
DWORD status = 0U;
|
|
if (!::GetCommModemStatus(m_handle, &status))
|
|
return;
|
|
|
|
switch (m_config) {
|
|
case 1U:
|
|
inp1 = (status & MS_DSR_ON) != 0U;
|
|
inp2 = (status & MS_CTS_ON) != 0U;
|
|
break;
|
|
case 2U:
|
|
inp1 = (status & MS_RLSD_ON) != 0U;
|
|
inp2 = (status & MS_RLSD_ON) != 0U;
|
|
break;
|
|
case 3U:
|
|
inp1 = (status & MS_RLSD_ON) != 0U;
|
|
break;
|
|
default:
|
|
::fprintf(stderr, "Unknown serial config - %u\n", m_config);
|
|
break;
|
|
}
|
|
}
|
|
|
|
void CSerialLineController::setDigitalOutputs(bool outp1, bool, bool outp3, bool, bool, bool, bool, bool)
|
|
{
|
|
assert(m_handle != INVALID_HANDLE_VALUE);
|
|
|
|
if (m_config == 1U) {
|
|
if (outp1 == m_dtr && outp3 == m_rts)
|
|
return;
|
|
|
|
::EscapeCommFunction(m_handle, outp1 ? SETDTR : CLRDTR);
|
|
::EscapeCommFunction(m_handle, outp3 ? SETRTS : CLRRTS);
|
|
|
|
m_dtr = outp1;
|
|
m_rts = outp3;
|
|
} else if (m_config == 2U || m_config == 3U) {
|
|
if (outp1 == m_rts && outp3 == m_dtr)
|
|
return;
|
|
|
|
::EscapeCommFunction(m_handle, outp1 ? SETRTS : CLRRTS);
|
|
::EscapeCommFunction(m_handle, outp3 ? SETDTR : CLRDTR);
|
|
|
|
m_rts = outp1;
|
|
m_dtr = outp3;
|
|
} else {
|
|
::fprintf(stderr, "Unknown serial config - %u\n", m_config);
|
|
}
|
|
}
|
|
|
|
void CSerialLineController::close()
|
|
{
|
|
assert(m_handle != INVALID_HANDLE_VALUE);
|
|
|
|
::CloseHandle(m_handle);
|
|
m_handle = INVALID_HANDLE_VALUE;
|
|
}
|
|
|
|
#endif // _WIN32
|