Commit bbcf1069 authored by Pavel Vainerman's avatar Pavel Vainerman Committed by Pavel Vainerman

(modbus): supported parity

parent 384da8d8
...@@ -1078,6 +1078,11 @@ namespace uniset ...@@ -1078,6 +1078,11 @@ namespace uniset
} }
} }
auto p = it.getProp("parity");
if( !p.empty() )
d->second->parity = ComPort::getParity(p);
return true; return true;
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
......
...@@ -200,6 +200,7 @@ namespace uniset ...@@ -200,6 +200,7 @@ namespace uniset
// специфические поля для RS // специфические поля для RS
ComPort::Speed speed = { ComPort::ComSpeed38400 }; ComPort::Speed speed = { ComPort::ComSpeed38400 };
std::shared_ptr<RTUStorage> rtu188; std::shared_ptr<RTUStorage> rtu188;
ComPort::Parity parity = { ComPort::NoParity };
std::string getShortInfo() const; std::string getShortInfo() const;
}; };
......
...@@ -76,6 +76,10 @@ RTUExchange::RTUExchange(uniset::ObjectId objId, uniset::ObjectId shmId, const s ...@@ -76,6 +76,10 @@ RTUExchange::RTUExchange(uniset::ObjectId objId, uniset::ObjectId shmId, const s
use485F = conf->getArgInt("--" + mbconf->prefix + "-use485F", it.getProp("use485F")); use485F = conf->getArgInt("--" + mbconf->prefix + "-use485F", it.getProp("use485F"));
transmitCtl = conf->getArgInt("--" + mbconf->prefix + "-transmit-ctl", it.getProp("transmitCtl")); transmitCtl = conf->getArgInt("--" + mbconf->prefix + "-transmit-ctl", it.getProp("transmitCtl"));
defSpeed = ComPort::getSpeed(speed); defSpeed = ComPort::getSpeed(speed);
auto p = conf->getArgParam("--" + mbconf->prefix + "-parity", it.getProp("parity"));
if( !p.empty() )
parity = ComPort::getParity(p);
mbconf->sleepPause_msec = conf->getArgPInt("--" + mbconf->prefix + "-sleepPause-usec", it.getProp("slepePause"), 100); mbconf->sleepPause_msec = conf->getArgPInt("--" + mbconf->prefix + "-sleepPause-usec", it.getProp("slepePause"), 100);
...@@ -227,6 +231,7 @@ bool RTUExchange::poll() ...@@ -227,6 +231,7 @@ bool RTUExchange::poll()
ncycle++; ncycle++;
bool allNotRespond = true; bool allNotRespond = true;
ComPort::Speed s = mbrtu->getSpeed(); ComPort::Speed s = mbrtu->getSpeed();
ComPort::Parity p = mbrtu->getParity();
for( auto it1 : mbconf->devices ) for( auto it1 : mbconf->devices )
{ {
...@@ -241,6 +246,12 @@ bool RTUExchange::poll() ...@@ -241,6 +246,12 @@ bool RTUExchange::poll()
mbrtu->setSpeed(d->speed); mbrtu->setSpeed(d->speed);
} }
if( d->parity != p )
{
p = d->parity;
mbrtu->setParity(p);
}
d->prev_numreply.store(d->numreply); d->prev_numreply.store(d->numreply);
if( d->dtype == MBConfig::dtRTU188 ) if( d->dtype == MBConfig::dtRTU188 )
......
...@@ -48,6 +48,7 @@ namespace uniset ...@@ -48,6 +48,7 @@ namespace uniset
std::mutex mbMutex; std::mutex mbMutex;
std::string devname; std::string devname;
ComPort::Speed defSpeed; ComPort::Speed defSpeed;
ComPort::Parity parity;
bool use485F; bool use485F;
bool transmitCtl; bool transmitCtl;
......
...@@ -34,8 +34,8 @@ int main( int argc, const char* argv[] ) ...@@ -34,8 +34,8 @@ int main( int argc, const char* argv[] )
int returnCode = session.applyCommandLine( argc, argv ); int returnCode = session.applyCommandLine( argc, argv );
// if( returnCode != 0 ) // Indicates a command line error // if( returnCode != 0 ) // Indicates a command line error
// return returnCode; // return returnCode;
auto conf = uniset_init(argc, argv); auto conf = uniset_init(argc, argv);
dlog()->logFile("./smtest.log"); dlog()->logFile("./smtest.log");
......
...@@ -32,8 +32,8 @@ int main(int argc, const char* argv[] ) ...@@ -32,8 +32,8 @@ int main(int argc, const char* argv[] )
int returnCode = session.applyCommandLine( argc, argv ); int returnCode = session.applyCommandLine( argc, argv );
// if( returnCode != 0 ) // Indicates a command line error // if( returnCode != 0 ) // Indicates a command line error
// return returnCode; // return returnCode;
auto conf = uniset_init(argc, argv); auto conf = uniset_init(argc, argv);
dlog()->logFile("./smtest.log"); dlog()->logFile("./smtest.log");
......
...@@ -170,11 +170,14 @@ namespace uniset ...@@ -170,11 +170,14 @@ namespace uniset
if( speed.empty() ) if( speed.empty() )
speed = "38400"; speed = "38400";
string parity = conf->getArgParam("--" + prefix + "-parity", it.getProp("parity"));
bool use485F = conf->getArgInt("--" + prefix + "-use485F", it.getProp("use485F")); bool use485F = conf->getArgInt("--" + prefix + "-use485F", it.getProp("use485F"));
bool transmitCtl = conf->getArgInt("--" + prefix + "-transmit-ctl", it.getProp("transmitCtl")); bool transmitCtl = conf->getArgInt("--" + prefix + "-transmit-ctl", it.getProp("transmitCtl"));
auto rs = make_shared<ModbusRTUSlaveSlot>(dev, use485F, transmitCtl); auto rs = make_shared<ModbusRTUSlaveSlot>(dev, use485F, transmitCtl);
rs->setSpeed(speed); rs->setSpeed(speed);
if( !parity.empty() )
rs->setParity(parity);
rs->setRecvTimeout(2000); rs->setRecvTimeout(2000);
rs->setAfterSendPause(aftersend_pause); rs->setAfterSendPause(aftersend_pause);
rs->setReplyTimeout(reply_tout); rs->setReplyTimeout(reply_tout);
...@@ -1580,6 +1583,7 @@ namespace uniset ...@@ -1580,6 +1583,7 @@ namespace uniset
cout << " Настройки протокола RTU: " << endl; cout << " Настройки протокола RTU: " << endl;
cout << "--prefix-dev devname - файл устройства" << endl; cout << "--prefix-dev devname - файл устройства" << endl;
cout << "--prefix-speed - Скорость обмена (9600,19920,38400,57600,115200)." << endl; cout << "--prefix-speed - Скорость обмена (9600,19920,38400,57600,115200)." << endl;
cout << "--prefix-parity - Контроль чётности (odd,even,noparity,space,mark)." << endl;
cout << " Настройки протокола TCP: " << endl; cout << " Настройки протокола TCP: " << endl;
cout << "--prefix-inet-addr [xxx.xxx.xxx.xxx | hostname ] - this modbus server address" << endl; cout << "--prefix-inet-addr [xxx.xxx.xxx.xxx | hostname ] - this modbus server address" << endl;
......
...@@ -99,8 +99,12 @@ namespace uniset ...@@ -99,8 +99,12 @@ namespace uniset
static Speed getSpeed( const std::string& s ); static Speed getSpeed( const std::string& s );
static std::string getSpeed( Speed s ); static std::string getSpeed( Speed s );
static Parity getParity( const std::string& s );
void setParity(Parity); void setParity(Parity);
void setParity(const std::string& s);
Parity getParity();
void setCharacterSize(CharacterSize); void setCharacterSize(CharacterSize);
void setStopBits(StopBits sBit); void setStopBits(StopBits sBit);
...@@ -132,6 +136,7 @@ namespace uniset ...@@ -132,6 +136,7 @@ namespace uniset
bool waiting = { false }; bool waiting = { false };
Speed speed = ComSpeed38400; Speed speed = ComSpeed38400;
std::string dev = { "" }; std::string dev = { "" };
Parity parity = NoParity;
virtual unsigned char m_receiveByte( bool wait ); virtual unsigned char m_receiveByte( bool wait );
......
...@@ -33,6 +33,7 @@ namespace uniset ...@@ -33,6 +33,7 @@ namespace uniset
ComPort::Speed getSpeed(); ComPort::Speed getSpeed();
void setParity( ComPort::Parity parity ); void setParity( ComPort::Parity parity );
ComPort::Parity getParity();
void setCharacterSize( ComPort::CharacterSize csize ); void setCharacterSize( ComPort::CharacterSize csize );
void setStopBits( ComPort::StopBits sBit ); void setStopBits( ComPort::StopBits sBit );
......
...@@ -35,6 +35,9 @@ namespace uniset ...@@ -35,6 +35,9 @@ namespace uniset
void setSpeed( const std::string& s ); void setSpeed( const std::string& s );
ComPort::Speed getSpeed(); ComPort::Speed getSpeed();
void setParity( ComPort::Parity p );
void setParity( const std::string& p );
virtual void cleanupChannel() override virtual void cleanupChannel() override
{ {
if(port) port->cleanupChannel(); if(port) port->cleanupChannel();
......
...@@ -128,6 +128,41 @@ void ComPort::setSpeed( Speed s ) ...@@ -128,6 +128,41 @@ void ComPort::setSpeed( Speed s )
tcsetattr(fd, TCSADRAIN, &options); tcsetattr(fd, TCSADRAIN, &options);
} }
// -------------------------------------------------------------------------------- // --------------------------------------------------------------------------------
ComPort::Parity ComPort::getParity( const std::string& s )
{
if( s == "odd" )
return Odd;
if( s == "even" )
return Even;
if( s == "noparity" )
return NoParity;
if( s == "space" )
return Space;
if( s == "mark" )
return Mark;
return NoParity;
}
// --------------------------------------------------------------------------------
ComPort::Parity ComPort::getParity()
{
return parity;
}
// --------------------------------------------------------------------------------
void ComPort::setParity( const std::string& s )
{
if( s == "odd" )
setParity(Odd);
else if( s == "even" )
setParity(Even);
else if( s == "noparity" )
setParity(NoParity);
else if( s == "space" )
setParity(Space);
else if( s == "mark" )
setParity(Mark);
}
// --------------------------------------------------------------------------------
void ComPort::setParity(Parity parity) void ComPort::setParity(Parity parity)
{ {
struct termios options; struct termios options;
...@@ -137,28 +172,33 @@ void ComPort::setParity(Parity parity) ...@@ -137,28 +172,33 @@ void ComPort::setParity(Parity parity)
switch(parity) switch(parity)
{ {
case Odd: case Odd:
parity = Odd;
options.c_cflag |= PARENB; options.c_cflag |= PARENB;
options.c_cflag &= ~CMSPAR; options.c_cflag &= ~CMSPAR;
options.c_cflag |= PARODD; options.c_cflag |= PARODD;
break; break;
case Even: case Even:
parity = Even;
options.c_cflag |= PARENB; options.c_cflag |= PARENB;
options.c_cflag &= ~CMSPAR; options.c_cflag &= ~CMSPAR;
options.c_cflag &= ~PARODD; options.c_cflag &= ~PARODD;
break; break;
case NoParity: case NoParity:
parity = NoParity;
options.c_cflag &= ~PARENB; options.c_cflag &= ~PARENB;
break; break;
case Space: case Space:
parity = Space;
options.c_cflag |= PARENB; options.c_cflag |= PARENB;
options.c_cflag |= CMSPAR; options.c_cflag |= CMSPAR;
options.c_cflag |= PARODD; options.c_cflag |= PARODD;
break; break;
case Mark: case Mark:
parity = Mark;
options.c_cflag |= PARENB; options.c_cflag |= PARENB;
options.c_cflag |= CMSPAR; options.c_cflag |= CMSPAR;
options.c_cflag &= ~PARODD; options.c_cflag &= ~PARODD;
......
...@@ -111,6 +111,14 @@ namespace uniset ...@@ -111,6 +111,14 @@ namespace uniset
return port->getSpeed(); return port->getSpeed();
} }
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
ComPort::Parity ModbusRTUMaster::getParity()
{
if( port != NULL)
port->getParity();
return ComPort::NoParity;
}
// -------------------------------------------------------------------------
void ModbusRTUMaster::setParity( ComPort::Parity parity ) void ModbusRTUMaster::setParity( ComPort::Parity parity )
{ {
if( port != NULL) if( port != NULL)
......
...@@ -94,7 +94,17 @@ namespace uniset ...@@ -94,7 +94,17 @@ namespace uniset
return port->getSpeed(); return port->getSpeed();
} }
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
void ModbusRTUSlave::setParity( ComPort::Parity p )
{
if( port != NULL )
port->setParity(p);
}
// -------------------------------------------------------------------------
void ModbusRTUSlave::setParity( const std::string& p )
{
setParity(ComPort::getParity(p));
}
// -------------------------------------------------------------------------
void ModbusRTUSlave::setSpeed( ComPort::Speed s ) void ModbusRTUSlave::setSpeed( ComPort::Speed s )
{ {
if( port != NULL ) if( port != NULL )
......
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