📄 posix_qextserialport.cpp
字号:
/*!
\class Posix_QextSerialPort
\version 0.70 (pre-alpha)
\author Wayne Roth
A cross-platform serial port class.
This class encapsulates the POSIX portion of QextSerialPort. The user will be notified of errors
and possible portability conflicts at run-time by default - this behavior can be turned off by
defining _TTY_NOWARN_ (to turn off all warnings) or _TTY_NOWARN_PORT_ (to turn off portability
warnings) in the project. Note that _TTY_NOWARN_ will also turn off portability warnings.
*/
#include <stdio.h>
#include "posix_qextserialport.h"
/*!
\fn Posix_QextSerialPort::Posix_QextSerialPort()
Default constructor. Note that the name of the device used by a QextSerialPort constructed with
this constructor will be determined by #defined constants, or lack thereof - the default behavior
is the same as _TTY_LINUX_. Possible naming conventions and their associated constants are:
\verbatim
Constant Used By Naming Convention
---------- ------------- ------------------------
_TTY_WIN_ Windows COM1, COM2
_TTY_IRIX_ SGI/IRIX /dev/ttyf1, /dev/ttyf2
_TTY_HPUX_ HP-UX /dev/tty1p0, /dev/tty2p0
_TTY_SUN_ SunOS/Solaris /dev/ttya, /dev/ttyb
_TTY_DIGITAL_ Digital UNIX /dev/tty01, /dev/tty02
_TTY_LINUX_ Linux /dev/ttyS0, /dev/ttyS1
<none> Linux /dev/ttyS0, /dev/ttyS1
\endverbatim
This constructor assigns the device name to the name of the first port on the specified system.
See the other constructors if you need to open a different port.
*/
Posix_QextSerialPort::Posix_QextSerialPort():QextSerialBase() {
construct();
}
/*!
\fn Posix_QextSerialPort::Posix_QextSerialPort(const Posix_QextSerialPort&)
Copy constructor.
*/
Posix_QextSerialPort::Posix_QextSerialPort(const Posix_QextSerialPort& s)
:QextSerialBase(s.portName) {
construct();
portOpen=s.portOpen;
lastErr=s.lastErr;
memcpy(portName, s.portName, sizeof(portName));
Settings.FlowControl=s.Settings.FlowControl;
Settings.Parity=s.Settings.Parity;
Settings.DataBits=s.Settings.DataBits;
Settings.StopBits=s.Settings.StopBits;
Settings.BaudRate=s.Settings.BaudRate;
Posix_File=s.Posix_File;
memcpy(&Posix_Timeout, &s.Posix_Timeout, sizeof(struct timeval));
memcpy(&Posix_Copy_Timeout, &s.Posix_Copy_Timeout, sizeof(struct timeval));
memcpy(&Posix_CommConfig, &s.Posix_CommConfig, sizeof(struct termios));
}
/*!
\fn Posix_QextSerialPort::Posix_QextSerialPort(const char* name)
Constructs a serial port attached to the port specified by name.
name is the name of the device, which is windowsystem-specific,
e.g."COM2" or "/dev/ttyS0".
*/
Posix_QextSerialPort::Posix_QextSerialPort(const char* name):QextSerialBase(name) {
construct();
}
/*!
\fn Posix_QextSerialPort::Posix_QextSerialPort(const PortSettings& settings)
Constructs a port with default name and specified settings.
*/
Posix_QextSerialPort::Posix_QextSerialPort(const PortSettings& settings)
:QextSerialBase() {
construct();
setBaudRate(settings.BaudRate);
setDataBits(settings.DataBits);
setStopBits(settings.StopBits);
setParity(settings.Parity);
setFlowControl(settings.FlowControl);
setTimeout(settings.Timeout_Sec, settings.Timeout_Millisec);
}
/*!
\fn Posix_QextSerialPort::Posix_QextSerialPort(const char* name, const PortSettings& settings)
Constructs a port with specified name and settings.
*/
Posix_QextSerialPort::Posix_QextSerialPort(const char* name, const PortSettings& settings)
:QextSerialBase(name) {
construct();
setBaudRate(settings.BaudRate);
setDataBits(settings.DataBits);
setStopBits(settings.StopBits);
setParity(settings.Parity);
setFlowControl(settings.FlowControl);
setTimeout(settings.Timeout_Sec, settings.Timeout_Millisec);
}
/*!
\fn Posix_QextSerialPort::~Posix_QextSerialPort()
Standard destructor.
*/
Posix_QextSerialPort::~Posix_QextSerialPort() {
if (portOpen) {
close();
}
Posix_File->close();
delete Posix_File;
}
/*!
\fn Posix_QextSerialPort& Posix_QextSerialPort::operator=(const Posix_QextSerialPort& s)
Override the = operator.
*/
Posix_QextSerialPort& Posix_QextSerialPort::operator=(const Posix_QextSerialPort& s) {
portOpen=s.portOpen;
lastErr=s.lastErr;
memcpy(portName, s.portName, sizeof(portName));
Settings.FlowControl=s.Settings.FlowControl;
Settings.Parity=s.Settings.Parity;
Settings.DataBits=s.Settings.DataBits;
Settings.StopBits=s.Settings.StopBits;
Settings.BaudRate=s.Settings.BaudRate;
Posix_File=s.Posix_File;
memcpy(&Posix_Timeout, &(s.Posix_Timeout), sizeof(struct timeval));
memcpy(&Posix_Copy_Timeout, &(s.Posix_Copy_Timeout), sizeof(struct timeval));
memcpy(&Posix_CommConfig, &(s.Posix_CommConfig), sizeof(struct termios));
return *this;
}
/*!
\fn Posix_QextSerialPort::construct(void)
Common constructor function, called by all versions of
Posix_QextSerialPort::Posix_QextSerialPort(). Sets up default port settings (115200 8N1
Hardware flow control where supported, otherwise no flow control, and 500 ms timeout).
*/
void Posix_QextSerialPort::construct(void) {
QextSerialBase::construct();
setBaudRate(BAUD115200);
setDataBits(DATA_8);
setStopBits(STOP_1);
setParity(PAR_NONE);
setFlowControl(FLOW_HARDWARE);
setTimeout(0, 500);
Posix_File=new QFile();
}
/*!
\fn bool Posix_QextSerialPort::open(int=0)
Opens a serial port. Note that this function does not specify which device to open. If you need
to open a device by name, see Posix_QextSerialPort::open(const char*). This function has no
effect if the port associated with the class is already open. The port is also configured to the
current settings, as stored in the Settings structure.
*/
bool Posix_QextSerialPort::open(int) {
LOCK_MUTEX();
if (!portOpen) {
/*open the port*/
Posix_File->setName(portName);
if (Posix_File->open(IO_Async|IO_Raw|IO_ReadWrite)) {
portOpen=true;
}
/*configure port settings*/
tcgetattr(Posix_File->handle(), &Posix_CommConfig);
/*set up other port settings*/
Posix_CommConfig.c_cflag|=CREAD|CLOCAL;
Posix_CommConfig.c_lflag&=(~(ICANON|ECHO|ECHOE|ECHOK|ECHONL|ISIG));
Posix_CommConfig.c_iflag&=(~(INPCK|IGNPAR|PARMRK|ISTRIP|IXANY));
Posix_CommConfig.c_oflag&=(~OPOST);
Posix_CommConfig.c_cc[VMIN]=0;
Posix_CommConfig.c_cc[VINTR] = _POSIX_VDISABLE;
Posix_CommConfig.c_cc[VQUIT] = _POSIX_VDISABLE;
Posix_CommConfig.c_cc[VSTART] = _POSIX_VDISABLE;
Posix_CommConfig.c_cc[VSTOP] = _POSIX_VDISABLE;
Posix_CommConfig.c_cc[VSUSP] = _POSIX_VDISABLE;
setBaudRate(Settings.BaudRate);
setDataBits(Settings.DataBits);
setStopBits(Settings.StopBits);
setParity(Settings.Parity);
setFlowControl(Settings.FlowControl);
setTimeout(Posix_Copy_Timeout.tv_sec, Posix_Copy_Timeout.tv_usec);
tcsetattr(Posix_File->handle(), TCSAFLUSH, &Posix_CommConfig);
}
UNLOCK_MUTEX();
return portOpen;
}
/*!
\fn void Posix_QextSerialPort::close()
Closes a serial port. This function has no effect if the serial port associated with the class
is not currently open.
*/
void Posix_QextSerialPort::close() {
LOCK_MUTEX();
Posix_File->close();
portOpen=false;
UNLOCK_MUTEX();
}
/*!
\fn void Posix_QextSerialPort::flush()
Flushes all pending I/O to the serial port. This function has no effect if the serial port
associated with the class is not currently open.
*/
void Posix_QextSerialPort::flush() {
LOCK_MUTEX();
if (portOpen) {
Posix_File->flush();
}
UNLOCK_MUTEX();
}
/*!
\fn unsigned int Posix_QextSerialPort::size() const
This function will return the number of bytes waiting in the receive queue of the serial port.
It is included primarily to provide a complete QIODevice interface, and will not record errors
in the lastErr member (because it is const). This function is also not thread-safe - in
multithreading situations, use Posix_QextSerialPort::bytesWaiting() instead.
*/
Offset Posix_QextSerialPort::size() const {
int availBytes;
if (ioctl(Posix_File->handle(), FIONREAD, &availBytes)<0) {
availBytes=0;
}
return (unsigned int)availBytes;
}
/*!
\fn int Posix_QextSerialPort::bytesWaiting()
Returns the number of bytes waiting in the port's receive queue. This function will return 0 if
the port is not currently open, or -1 on error. Error information can be retrieved by calling
Posix_QextSerialPort::getLastError().
*/
int Posix_QextSerialPort::bytesWaiting() {
LOCK_MUTEX();
if (portOpen) {
int bytesQueued;
fd_set fileSet;
FD_ZERO(&fileSet);
FD_SET(Posix_File->handle(), &fileSet);
/*on Linux systems the Posix_Timeout structure will be altered by the select() call.
Make sure we use the right timeout values*/
memcpy(&Posix_Timeout, &Posix_Copy_Timeout, sizeof(struct timeval));
int n=select(Posix_File->handle()+1, &fileSet, NULL, &fileSet, &Posix_Timeout);
if (!n) {
lastErr=E_PORT_TIMEOUT;
UNLOCK_MUTEX();
return -1;
}
if (n==-1 || ioctl(Posix_File->handle(), FIONREAD, &bytesQueued)==-1) {
translateError(errno);
UNLOCK_MUTEX();
return -1;
}
lastErr=E_NO_ERROR;
UNLOCK_MUTEX();
return bytesQueued;
}
UNLOCK_MUTEX();
return 0;
}
/*!
\fn void Posix_QextSerialPort::translateError(unsigned long error)
Translates a system-specific error code to a QextSerialPort error code. Used internally.
*/
void Posix_QextSerialPort::translateError(unsigned long error) {
switch (error) {
case EBADF:
case ENOTTY:
lastErr=E_INVALID_FD;
break;
case EINTR:
lastErr=E_CAUGHT_NON_BLOCKED_SIGNAL;
break;
case ENOMEM:
lastErr=E_NO_MEMORY;
break;
}
}
/*!
\fn Q_LONG Posix_QextSerialPort::readBlock(char *data, uint maxlen)
Reads a block of data from the serial port. This function will read at most maxlen bytes from
the serial port and place them in the buffer pointed to by data. Return value is the number of
bytes actually read, or -1 on error. This function will have no effect if the serial port
associated with the class is not currently open.
*/
Q_LONG Posix_QextSerialPort::readBlock(char *data,
#ifdef QTVER_PRE_30
uint maxlen)
#else
unsigned long maxlen)
#endif
{
LOCK_MUTEX();
int retVal=0;
if (portOpen) {
retVal=Posix_File->readBlock(data, maxlen);
if (retVal==-1) {
lastErr=E_READ_FAILED;
}
}
UNLOCK_MUTEX();
return retVal;
}
/*!
\fn Q_LONG Posix_QextSerialPort::writeBlock(const char *data, uint len)
Writes a block of data to the serial port. This function will write len bytes
from the buffer pointed to by data to the serial port. Return value is the number
of bytes actually written, or -1 on error. This function will have no effect if the serial
port associated with the class is not currently open.
*/
Q_LONG Posix_QextSerialPort::writeBlock(const char *data,
#ifdef QTVER_PRE_30
uint len)
#else
unsigned long len)
#endif
{
LOCK_MUTEX();
int retVal=0;
if (portOpen) {
retVal=Posix_File->writeBlock(data, len);
if (retVal==-1) {
lastErr=E_WRITE_FAILED;
}
}
UNLOCK_MUTEX();
flush();
return retVal;
}
/*!
\fn int Posix_QextSerialPort::getch()
Returns a single character from the serial port, or -1 on error. This function has no effect if
the serial port associated with the class is not currently open.
*/
int Posix_QextSerialPort::getch() {
LOCK_MUTEX();
int retVal=-1;
if (portOpen) {
retVal=Posix_File->getch();
if (retVal==-1) {
lastErr=E_READ_FAILED;
}
}
UNLOCK_MUTEX();
return retVal;
}
/*!
\fn int Posix_QextSerialPort::putch(int ch)
Writes a single character to the serial port. Return value is the byte written, or -1 on
error. This function has no effect if the serial port associated with the class is not
currently open.
*/
int Posix_QextSerialPort::putch(int ch) {
LOCK_MUTEX();
int retVal=0;
if (portOpen) {
retVal=Posix_File->putch(ch);
if (retVal==-1) {
lastErr=E_WRITE_FAILED;
}
}
UNLOCK_MUTEX();
flush();
return retVal;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -