Commit e4fb673b authored by Pavel Vainerman's avatar Pavel Vainerman

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

parent c940d756
...@@ -67,7 +67,7 @@ public: ...@@ -67,7 +67,7 @@ public:
TwoBits=3 TwoBits=3
}; };
ComPort( std::string comDevice, bool nocreate=false ); ComPort( const std::string comDevice, bool nocreate=false );
virtual ~ComPort(); virtual ~ComPort();
void setSpeed( Speed s ); void setSpeed( Speed s );
...@@ -95,7 +95,11 @@ public: ...@@ -95,7 +95,11 @@ public:
void cleanupChannel(); void cleanupChannel();
virtual void reopen();
protected: protected:
void openPort();
static const int BufSize=8192; static const int BufSize=8192;
unsigned char buf[BufSize]; unsigned char buf[BufSize];
int curSym; int curSym;
...@@ -104,6 +108,7 @@ protected: ...@@ -104,6 +108,7 @@ protected:
int uTimeout; int uTimeout;
bool waiting; bool waiting;
Speed speed; Speed speed;
std::string dev;
virtual unsigned char m_receiveByte( bool wait ); virtual unsigned char m_receiveByte( bool wait );
......
...@@ -27,23 +27,29 @@ using namespace std; ...@@ -27,23 +27,29 @@ using namespace std;
ComPort::~ComPort() ComPort::~ComPort()
{ {
// printf("Destructor\n"); // printf("Destructor\n");
if( fd > 0 )
{
tcsetattr(fd, TCSAFLUSH, &oldTermios); tcsetattr(fd, TCSAFLUSH, &oldTermios);
close(fd); close(fd);
}
} }
// -------------------------------------------------------------------------------- // --------------------------------------------------------------------------------
ComPort::ComPort( string comDevice, bool nocreate ): ComPort::ComPort( const string comDevice, bool nocreate ):
curSym(0), bufLength(0), uTimeout(10000), curSym(0), bufLength(0),fd(-1),uTimeout(10000),
waiting(true) waiting(true),dev(comDevice)
{ {
if( !nocreate ) if( !nocreate )
{ openPort();
}
// --------------------------------------------------------------------------------
void ComPort::openPort()
{
struct termios options; struct termios options;
fd = open(comDevice.c_str(), O_RDWR | O_NOCTTY /*| O_NDELAY*/); fd = open(dev.c_str(), O_RDWR | O_NOCTTY /*| O_NDELAY*/);
if (fd == -1) 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()); throw UniSetTypes::SystemError(strErr.c_str());
} }
...@@ -73,6 +79,19 @@ ComPort::ComPort( string comDevice, bool nocreate ): ...@@ -73,6 +79,19 @@ ComPort::ComPort( string comDevice, bool nocreate ):
options.c_cflag |= (CLOCAL|CREAD); options.c_cflag |= (CLOCAL|CREAD);
tcsetattr(fd, TCSAFLUSH, &options); 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 ) ...@@ -87,7 +106,6 @@ void ComPort::setSpeed( Speed s )
cfsetospeed(&options, speed); cfsetospeed(&options, speed);
tcsetattr(fd, TCSADRAIN, &options); tcsetattr(fd, TCSADRAIN, &options);
} }
// -------------------------------------------------------------------------------- // --------------------------------------------------------------------------------
void ComPort::setParity(Parity parity) 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