Commit e4fb673b authored by Pavel Vainerman's avatar Pavel Vainerman

(ComPort): добавил функцию reopen(), для возможности "реинициализации" соединения "на ходу".

parent c940d756
......@@ -67,7 +67,7 @@ public:
TwoBits=3
};
ComPort( std::string comDevice, bool nocreate=false );
ComPort( const std::string comDevice, bool nocreate=false );
virtual ~ComPort();
void setSpeed( Speed s );
......@@ -95,7 +95,11 @@ public:
void cleanupChannel();
virtual void reopen();
protected:
void openPort();
static const int BufSize=8192;
unsigned char buf[BufSize];
int curSym;
......@@ -104,6 +108,7 @@ protected:
int uTimeout;
bool waiting;
Speed speed;
std::string dev;
virtual unsigned char m_receiveByte( bool wait );
......
......@@ -27,23 +27,29 @@ using namespace std;
ComPort::~ComPort()
{
// printf("Destructor\n");
if( fd > 0 )
{
tcsetattr(fd, TCSAFLUSH, &oldTermios);
close(fd);
}
}
// --------------------------------------------------------------------------------
ComPort::ComPort( string comDevice, bool nocreate ):
curSym(0), bufLength(0), uTimeout(10000),
waiting(true)
ComPort::ComPort( const string comDevice, bool nocreate ):
curSym(0), bufLength(0),fd(-1),uTimeout(10000),
waiting(true),dev(comDevice)
{
if( !nocreate )
{
openPort();
}
// --------------------------------------------------------------------------------
void ComPort::openPort()
{
struct termios options;
fd = open(comDevice.c_str(), O_RDWR | O_NOCTTY /*| O_NDELAY*/);
if (fd == -1)
fd = open(dev.c_str(), O_RDWR | O_NOCTTY /*| O_NDELAY*/);
if( fd == -1 )
{
string strErr="Unable to open "+comDevice+" [Error: "+strerror(errno)+"]";
string strErr="Unable to open "+dev+" [Error: "+strerror(errno)+"]";
throw UniSetTypes::SystemError(strErr.c_str());
}
......@@ -73,6 +79,19 @@ ComPort::ComPort( string comDevice, bool nocreate ):
options.c_cflag |= (CLOCAL|CREAD);
tcsetattr(fd, TCSAFLUSH, &options);
}
// --------------------------------------------------------------------------------
void ComPort::reopen()
{
if( fd > 0 )
{
struct termios options;
tcgetattr(fd, &options);
tcsetattr(fd, TCSAFLUSH, &oldTermios);
close(fd);
openPort();
if( fd > 0 )
tcsetattr(fd, TCSAFLUSH, &options);
}
}
// --------------------------------------------------------------------------------
......@@ -87,7 +106,6 @@ void ComPort::setSpeed( Speed s )
cfsetospeed(&options, speed);
tcsetattr(fd, TCSADRAIN, &options);
}
// --------------------------------------------------------------------------------
void ComPort::setParity(Parity parity)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment