Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added dtr/sdr management #70

Open
wants to merge 1 commit into
base: v4.2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions include/CSerialPort/SerialPortUnixBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -270,14 +270,12 @@ class CSerialPortUnixBase : public CSerialPortBase

/**
* @brief Set the Dtr object 设置DTR
* @todo Not implemented 未实现
*
* @param set [in]
*/
virtual void setDtr(bool set = true);
/**
* @brief Set the Rts object 设置RTS
* @todo Not implemented 未实现
*
* @param set [in]
*/
Expand Down Expand Up @@ -315,7 +313,9 @@ class CSerialPortUnixBase : public CSerialPortBase
itas109::Parity parity = itas109::ParityNone,
itas109::DataBits dataBits = itas109::DataBits8,
itas109::StopBits stopbits = itas109::StopOne,
itas109::FlowControl flowControl = itas109::FlowNone);
itas109::FlowControl flowControl = itas109::FlowNone,
bool setDtr = false,
bool setRts = false);

/**
* @brief thread monitor 多线程监视器
Expand Down Expand Up @@ -359,6 +359,7 @@ class CSerialPortUnixBase : public CSerialPortBase
itas109::StopBits m_stopbits;
itas109::FlowControl m_flowControl;
unsigned int m_readBufferSize;
bool m_setDtr, m_setRts;

int fd; /* File descriptor for the port */

Expand Down
3 changes: 1 addition & 2 deletions include/CSerialPort/SerialPortWinBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,14 +272,12 @@ class CSerialPortWinBase : public CSerialPortBase

/**
* @brief Set the Dtr object 设置DTR
* @todo Not implemented 未实现
*
* @param set [in]
*/
virtual void setDtr(bool set = true);
/**
* @brief Set the Rts object 设置RTS
* @todo Not implemented 未实现
*
* @param set [in]
*/
Expand Down Expand Up @@ -349,6 +347,7 @@ class CSerialPortWinBase : public CSerialPortBase
itas109::StopBits m_stopbits;
enum itas109::FlowControl m_flowControl;
unsigned int m_readBufferSize;
bool m_setDtr, m_setRts;

private:
HANDLE m_handle;
Expand Down
35 changes: 31 additions & 4 deletions src/SerialPortUnixBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ CSerialPortUnixBase::CSerialPortUnixBase()
, m_readBufferSize(4096)
, m_isThreadRunning(false)
, p_buffer(new itas109::RingBuffer<char>(m_readBufferSize))
, m_setDtr(false)
, m_setRts(false)
{
}

Expand All @@ -76,6 +78,8 @@ CSerialPortUnixBase::CSerialPortUnixBase(const std::string &portName)
, m_readBufferSize(4096)
, m_isThreadRunning(false)
, p_buffer(new itas109::RingBuffer<char>(m_readBufferSize))
, m_setDtr(false)
, m_setRts(false)
{
}

Expand Down Expand Up @@ -112,7 +116,7 @@ void CSerialPortUnixBase::init(std::string portName,
p_buffer = new itas109::RingBuffer<char>(m_readBufferSize);
}

int CSerialPortUnixBase::uartSet(int fd, int baudRate, itas109::Parity parity, itas109::DataBits dataBits, itas109::StopBits stopbits, itas109::FlowControl flowControl)
int CSerialPortUnixBase::uartSet(int fd, int baudRate, itas109::Parity parity, itas109::DataBits dataBits, itas109::StopBits stopbits, itas109::FlowControl flowControl, bool setDtr, bool setRts)
{
struct termios options;

Expand Down Expand Up @@ -291,6 +295,23 @@ int CSerialPortUnixBase::uartSet(int fd, int baudRate, itas109::Parity parity, i
return -1;
}

int flags = 0;
if (setDtr) flags |= TIOCM_DTR;
if (setRts) flags |= TIOCM_RTS;
if (ioctl(fd, TIOCMBIS, &flags) < 0)
{
perror("set flags failed");
return -1;
}
flags = 0;
if (!setDtr) flags |= TIOCM_DTR;
if (!setRts) flags |= TIOCM_RTS;
if (ioctl(fd, TIOCMBIC, &flags) < 0)
{
perror("clear flags failed");
return -1;
}

return 0;
}

Expand Down Expand Up @@ -415,7 +436,7 @@ bool CSerialPortUnixBase::openPort()
if (fcntl(fd, F_SETFL, 0) >= 0) // 阻塞,即使前面在open串口设备时设置的是非阻塞的,这里设为阻塞后,以此为准
{
// set param
if (uartSet(fd, m_baudRate, m_parity, m_dataBits, m_stopbits, m_flowControl) == -1)
if (uartSet(fd, m_baudRate, m_parity, m_dataBits, m_stopbits, m_flowControl, m_setDtr, m_setRts) == -1)
{
fprintf(stderr, "uart set failed\n");

Expand Down Expand Up @@ -692,9 +713,15 @@ unsigned int CSerialPortUnixBase::getReadBufferSize() const
return m_readBufferSize;
}

void CSerialPortUnixBase::setDtr(bool set /*= true*/) {}
void CSerialPortUnixBase::setDtr(bool set)
{
m_setDtr = set;
}

void CSerialPortUnixBase::setRts(bool set /*= true*/) {}
void CSerialPortUnixBase::setRts(bool set)
{
m_setRts = set;
}

bool CSerialPortUnixBase::isThreadRunning()
{
Expand Down
10 changes: 8 additions & 2 deletions src/SerialPortWinBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ CSerialPortWinBase::CSerialPortWinBase()
, m_communicationMutex()
, m_isThreadRunning(false)
, p_buffer(new itas109::RingBuffer<char>(m_readBufferSize))
, m_setDtr(false)
, m_setRts(false)
{
overlapMonitor.Internal = 0;
overlapMonitor.InternalHigh = 0;
Expand All @@ -78,6 +80,8 @@ CSerialPortWinBase::CSerialPortWinBase(const std::string &portName)
, m_communicationMutex()
, m_isThreadRunning(false)
, p_buffer(new itas109::RingBuffer<char>(m_readBufferSize))
, m_setDtr(false)
, m_setRts(false)
{
overlapMonitor.Internal = 0;
overlapMonitor.InternalHigh = 0;
Expand Down Expand Up @@ -170,8 +174,8 @@ bool CSerialPortWinBase::openPort()
m_comConfigure.dcb.ByteSize = m_dataBits;
m_comConfigure.dcb.Parity = m_parity;
m_comConfigure.dcb.StopBits = m_stopbits;
// m_comConfigure.dcb.fDtrControl;
// m_comConfigure.dcb.fRtsControl;
m_comConfigure.dcb.fDtrControl = m_setDtr ? DTR_CONTROL_ENABLE : DTR_CONTROL_DISABLE;
m_comConfigure.dcb.fRtsControl = m_setRts ? RTS_CONTROL_ENABLE : RTS_CONTROL_DISABLE;

m_comConfigure.dcb.fBinary = true;
m_comConfigure.dcb.fInX = false;
Expand Down Expand Up @@ -883,6 +887,7 @@ void CSerialPortWinBase::setDtr(bool set /*= true*/)
EscapeCommFunction(m_handle, CLRDTR);
}
}
m_setDtr = set;
}

void CSerialPortWinBase::setRts(bool set /*= true*/)
Expand All @@ -899,6 +904,7 @@ void CSerialPortWinBase::setRts(bool set /*= true*/)
EscapeCommFunction(m_handle, CLRRTS);
}
}
m_setRts = set;
}

OVERLAPPED CSerialPortWinBase::getOverlapMonitor()
Expand Down