API
Collaboration diagram for TTY device interfaces:

Modules

 TTY files
 

Classes

struct  MagAOX::tty::telnetConn
 A Telnet connection manager, wrapping libtelnet. More...
 
struct  MagAOX::tty::usbDevice
 A USB device as a TTY device. More...
 

Functions

std::string MagAOX::tty::ttyErrorString (int ec)
 Get a text explanation of a TTY_E_ error code. More...
 
int MagAOX::tty::ttyOpenRaw (int &fileDescrip, std::string &deviceName, speed_t speed)
 Open a file as a raw-mode tty device. More...
 
bool MagAOX::tty::isEndOfTrans (const std::string &strRead, const std::string &eot)
 Check if the end of the buffer contains the end-of-transmission string. More...
 
int MagAOX::tty::ttyWrite (const std::string &buffWrite, int fd, int timeoutWrite)
 Write to the tty console indicated by a file descriptor. More...
 
int MagAOX::tty::ttyReadRaw (std::vector< unsigned char > &vecRead, int &readBytes, int fd, int timeoutRead)
 Read from a tty console indicated by a file-descriptor, up to a given number of bytes. More...
 
int MagAOX::tty::ttyRead (std::string &strRead, int bytes, int fd, int timeoutRead)
 Read from a tty console indicated by a file-descriptor, until a given number of bytes are read. More...
 
int MagAOX::tty::ttyRead (std::string &strRead, const std::string &eot, int fd, int timeoutRead)
 Read from a tty console indicated by a file-descriptor, until an end of transmission string is read. More...
 
int MagAOX::tty::ttyWriteRead (std::string &strRead, const std::string &strWrite, const std::string &eot, bool swallowEcho, int fd, int timeoutWrite, int timeoutRead)
 Write to a tty on an open file descriptor, then get the result. More...
 
int MagAOX::tty::ttyUSBDevName (std::string &devName, const std::string &vendor, const std::string &product, const std::string &serial)
 Get the ttyUSB device name for a specific device. More...
 
int MagAOX::tty::ttyUSBDevNames (std::vector< std::string > &devNames, const std::string &vendor, const std::string &product)
 Get the ttyUSB device name for a set of devices specified by their vendor and product ids. More...
 

Variables

static const telnet_telopt_t MagAOX::tty::telopts []
 libtelnet option table. More...
 

Detailed Description

Using Scren

Using screen to talk to TTY devices.

The screen program needs to be installed. You will need root privileges (sudo works).

$ screen /dev/ttyUSBX 115200

where you replace the dev node with the correct path, and the baud rate with the correct value. See the particular devices specs for the correct baud rate to use. Also See https:// for a list of baud rates.

Note that screen does not echo, so if the device doesn't echo you won't see the characters you type.

Function Documentation

◆ isEndOfTrans()

bool MagAOX::tty::isEndOfTrans ( const std::string &  strRead,
const std::string &  eot 
)

Check if the end of the buffer contains the end-of-transmission string.

Returns
true if the last N chars of buffRead are equal to eot, where N is the length of eot.
false otherwise.
Parameters
[in]strReadThe read buffer to check
[in]eotThe end-of-transmission string

Definition at line 116 of file ttyIOUtils.cpp.

Referenced by MagAOX::tty::telnetConn::read(), MagAOX::tty::ttyRead(), and MagAOX::tty::telnetConn::writeRead().

◆ ttyErrorString()

std::string MagAOX::tty::ttyErrorString ( int  ec)

Get a text explanation of a TTY_E_ error code.

Parameters
[in]ecthe error code

Definition at line 15 of file ttyErrors.cpp.

Referenced by MagAOX::app::filterWheelCtrl::appLogic(), MagAOX::app::flipperCtrl::appLogic(), MagAOX::app::koolanceCtrl::appLogic(), MagAOX::app::pi335Ctrl::appLogic(), MagAOX::app::picoMotorCtrl::appLogic(), MagAOX::app::smc100ccCtrl::appLogic(), MagAOX::app::tcsInterface::appLogic(), MagAOX::app::trippLitePDU::appLogic(), MagAOX::app::zaberLowLevel::appLogic(), MagAOX::app::picoMotorCtrl::channelThreadExec(), MagAOX::app::rhusbMon::connect(), MagAOX::app::trippLitePDU::devStatus(), MagAOX::app::pi335Ctrl::finishInit(), MagAOX::app::pi335Ctrl::getCom(), MagAOX::app::smc100ccCtrl::getCtrlState(), MagAOX::app::smc100ccCtrl::getLastError(), MagAOX::app::smc100ccCtrl::getPosition(), MagAOX::app::filterWheelCtrl::home(), MagAOX::app::pi335Ctrl::home_1(), MagAOX::app::pi335Ctrl::home_2(), MagAOX::app::pi335Ctrl::home_3(), MagAOX::app::pi335Ctrl::initDM(), MagAOX::app::filterWheelCtrl::loadConfig(), MagAOX::app::smc100ccCtrl::loadConfig(), MagAOX::app::zaberLowLevel::loadConfig(), MagAOX::app::flipperCtrl::loadConfigImpl(), MagAOX::app::koolanceCtrl::loadConfigImpl(), MagAOX::app::pi335Ctrl::loadConfigImpl(), MagAOX::app::pi335Ctrl::move_1(), MagAOX::app::pi335Ctrl::move_2(), MagAOX::app::pi335Ctrl::move_3(), MagAOX::app::smc100ccCtrl::moveTo(), MagAOX::app::filterWheelCtrl::moveToRaw(), MagAOX::app::filterWheelCtrl::moveToRawRelative(), MagAOX::app::filterWheelCtrl::onPowerOnConnect(), MagAOX::app::rhusbMon::readProbe(), MagAOX::app::pi335Ctrl::releaseDM(), MagAOX::app::smc100ccCtrl::startHoming(), MagAOX::app::filterWheelCtrl::stop(), MagAOX::app::smc100ccCtrl::stop(), MagAOX::app::pi335Ctrl::testConnection(), MagAOX::app::smc100ccCtrl::testConnection(), MagAOX::app::siglentSDG::writeCommand(), and MagAOX::app::siglentSDG::writeRead().

◆ ttyOpenRaw()

int MagAOX::tty::ttyOpenRaw ( int &  fileDescrip,
std::string &  deviceName,
speed_t  speed 
)

Open a file as a raw-mode tty device.

Returns
TTY_E_NOERROR on success.
TTY_E_TCGETATTR on a error from tcgetattr.
TTY_E_TCSETATTR on an error from tcsetattr.
TTY_E_SETISPEED on a cfsetispeed error.
TTY_E_SETOSPEED on a cfsetospeed error.
Parameters
[out]fileDescripthe file descriptor. Set to 0 on an error.
[in]deviceNamethe device path name, e.g. /dev/ttyUSB0
[in]speedindicates the baud rate (see http://pubs.opengroup.org/onlinepubs/7908799/xsh/termios.h.html)

Definition at line 72 of file ttyIOUtils.cpp.

Referenced by MagAOX::tty::usbDevice::connect().

◆ ttyRead() [1/2]

int MagAOX::tty::ttyRead ( std::string &  strRead,
const std::string &  eot,
int  fd,
int  timeoutRead 
)

Read from a tty console indicated by a file-descriptor, until an end of transmission string is read.

Returns
TTY_E_NOERROR on success
TTY_E_TIMEOUTONREADPOLL if the poll times out.
TTY_E_ERRORONREADPOLL if an error is returned by poll.
TTY_E_TIMEOUTONREAD if a timeout occurs during the read.
TTY_E_ERRORONREAD if an error occurs reading from the file.
Parameters
[out]strReadThe string in which to store the output.
[in]eotA sequence of characters which indicates the end of transmission.
[in]fdThe file descriptor of the open tty.
[in]timeoutReadThe timeout in milliseconds.

Definition at line 271 of file ttyIOUtils.cpp.

◆ ttyRead() [2/2]

int MagAOX::tty::ttyRead ( std::string &  strRead,
int  bytes,
int  fd,
int  timeoutRead 
)

Read from a tty console indicated by a file-descriptor, until a given number of bytes are read.

Returns
TTY_E_NOERROR on success
TTY_E_TIMEOUTONREADPOLL if the poll times out.
TTY_E_ERRORONREADPOLL if an error is returned by poll.
TTY_E_TIMEOUTONREAD if a timeout occurs during the read.
TTY_E_ERRORONREAD if an error occurs reading from the file.
Parameters
[out]strReadThe string in which to store the output.
[in]bytesthe number of bytes to read
[in]fdThe file descriptor of the open tty.
[in]timeoutReadThe timeout in milliseconds.

Definition at line 206 of file ttyIOUtils.cpp.

Referenced by MagAOX::app::filterWheelCtrl::appLogic(), MagAOX::app::flipperCtrl::getPos(), MagAOX::app::koolanceCtrl::getStatus(), and MagAOX::tty::ttyWriteRead().

◆ ttyReadRaw()

int MagAOX::tty::ttyReadRaw ( std::vector< unsigned char > &  vecRead,
int &  readBytes,
int  fd,
int  timeoutRead 
)

Read from a tty console indicated by a file-descriptor, up to a given number of bytes.

Polls before attempting to read, but does not wait for all bytes to be ready.

Returns
TTY_E_NOERROR on success
TTY_E_TIMEOUTONREADPOLL if the poll times out.
TTY_E_ERRORONREADPOLL if an error is returned by poll.
TTY_E_ERRORONREAD if an error occurs reading from the file.
Parameters
[out]vecReadThe buffer in which to store the output.
[out]readBytesThe number of bytes read.
[in]fdThe file descriptor of the open tty.
[in]timeoutReadThe timeout in milliseconds.

Definition at line 172 of file ttyIOUtils.cpp.

Referenced by MagAOX::app::koolanceCtrl::initialConnect().

◆ ttyUSBDevName()

int MagAOX::tty::ttyUSBDevName ( std::string &  devName,
const std::string &  vendor,
const std::string &  product,
const std::string &  serial 
)

Get the ttyUSB device name for a specific device.

Returns
TTY_E_NOERROR on success
TTY_E_NODEVNAMES if no device names found in sys
TTY_E_UDEVNEWFAILED if initializing libudev failed.
TTY_E_DEVNOTFOUND if no matching device found.
Parameters
[out]devNamethe /dev/ttyUSBX device name.
[in]vendorthe 4-digit vendor identifier.
[in]productthe 4-digit product identifier.
[in]serialthe serial number. Can be "".

Definition at line 30 of file ttyUSB.cpp.

Referenced by MagAOX::tty::usbDevice::getDeviceName().

◆ ttyUSBDevNames()

int MagAOX::tty::ttyUSBDevNames ( std::vector< std::string > &  devNames,
const std::string &  vendor,
const std::string &  product 
)

Get the ttyUSB device name for a set of devices specified by their vendor and product ids.

Returns
TTY_E_NOERROR on success
TTY_E_NODEVNAMES if no device names found in sys
TTY_E_UDEVNEWFAILED if initializing libudev failed.
TTY_E_DEVNOTFOUND if no matching device found.
Parameters
[out]devNamesthe /dev/ttyUSBX device names for all matching devices.
[in]vendorthe 4-digit vendor identifier.
[in]productthe 4-digit product identifier.

Definition at line 137 of file ttyUSB.cpp.

Referenced by MagAOX::app::usbtempMon::checkConnections().

◆ ttyWrite()

int MagAOX::tty::ttyWrite ( const std::string &  buffWrite,
int  fd,
int  timeoutWrite 
)

Write to the tty console indicated by a file descriptor.

Returns
TTY_E_NOERROR on success
TTY_E_TIMEOUTONWRITEPOLL if the poll times out.
TTY_E_ERRORONWRITEPOLL if an error is returned by poll.
TTY_E_TIMEOUTONWRITE if a timeout occurs during the write.
TTY_E_ERRORONWRITE if an error occurs writing to the file.
Parameters
[in]buffWriteThe characters to write to the tty.
[in]fdThe file descriptor of the open tty.
[in]timeoutWriteThe timeout in milliseconds.

Definition at line 132 of file ttyIOUtils.cpp.

Referenced by MagAOX::app::smc100ccCtrl::appLogic(), MagAOX::app::pi335Ctrl::finishInit(), MagAOX::app::flipperCtrl::getPos(), MagAOX::app::filterWheelCtrl::home(), MagAOX::app::pi335Ctrl::home_1(), MagAOX::app::pi335Ctrl::home_2(), MagAOX::app::pi335Ctrl::home_3(), MagAOX::app::pi335Ctrl::initDM(), MagAOX::app::pi335Ctrl::move_1(), MagAOX::app::pi335Ctrl::move_2(), MagAOX::app::pi335Ctrl::move_3(), MagAOX::app::smc100ccCtrl::moveTo(), MagAOX::app::flipperCtrl::moveTo(), MagAOX::app::filterWheelCtrl::moveToRaw(), MagAOX::app::filterWheelCtrl::moveToRawRelative(), MagAOX::app::filterWheelCtrl::onPowerOnConnect(), MagAOX::app::pi335Ctrl::releaseDM(), MagAOX::app::smc100ccCtrl::startHoming(), MagAOX::app::filterWheelCtrl::stop(), MagAOX::app::smc100ccCtrl::stop(), and MagAOX::tty::ttyWriteRead().

◆ ttyWriteRead()

int MagAOX::tty::ttyWriteRead ( std::string &  strRead,
const std::string &  strWrite,
const std::string &  eot,
bool  swallowEcho,
int  fd,
int  timeoutWrite,
int  timeoutRead 
)

Write to a tty on an open file descriptor, then get the result.

The read is conducted until an end-of-transmission string is received. Echo characters are swallowed if desired.

Returns
TTY_E_NOERROR on success
TTY_E_TIMEOUTONWRITEPOLL if the poll times out.
TTY_E_ERRORONWRITEPOLL if an error is returned by poll.
TTY_E_TIMEOUTONWRITE if a timeout occurs during the write.
TTY_E_ERRORONWRITE if an error occurs writing to the file.
TTY_E_TIMEOUTONREADPOLL if the poll times out.
TTY_E_ERRORONREADPOLL if an error is returned by poll.
TTY_E_TIMEOUTONREAD if a timeout occurs during the read.
TTY_E_ERRORONREAD if an error occurs reading from the file.
Parameters
[out]strReadThe string in which to store the output.
[in]strWriteThe characters to write to the tty.
[in]eotA sequence of characters which indicates the end of transmission.
[in]swallowEchoIf true, strWrite.size() characters are read after the write
[in]fdThe file descriptor of the open tty.
[in]timeoutWriteThe write timeout in milliseconds.
[in]timeoutReadThe read timeout in milliseconds.

Definition at line 332 of file ttyIOUtils.cpp.

Referenced by MagAOX::app::pi335Ctrl::finishInit(), MagAOX::app::pi335Ctrl::getCom(), MagAOX::app::smc100ccCtrl::getCtrlState(), MagAOX::app::smc100ccCtrl::getLastError(), MagAOX::app::filterWheelCtrl::getMoving(), MagAOX::app::filterWheelCtrl::getPos(), MagAOX::app::smc100ccCtrl::getPosition(), MagAOX::app::filterWheelCtrl::getSwitch(), MagAOX::app::pi335Ctrl::initDM(), MagAOX::app::rhusbMon::readProbe(), MagAOX::app::pi335Ctrl::testConnection(), and MagAOX::app::smc100ccCtrl::testConnection().

Variable Documentation

◆ telopts

const telnet_telopt_t MagAOX::tty::telopts[]
static
Initial value:
= {
{ TELNET_TELOPT_ECHO, TELNET_WONT, TELNET_DO },
{ TELNET_TELOPT_TTYPE, TELNET_WILL, TELNET_DONT },
{ TELNET_TELOPT_COMPRESS2, TELNET_WONT, TELNET_DO },
{ TELNET_TELOPT_MSSP, TELNET_WONT, TELNET_DO },
{ -1, 0, 0 } }

libtelnet option table.

Definition at line 49 of file telnetConn.hpp.

Referenced by MagAOX::tty::telnetConn::connect().