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