Skip to content

Commit

Permalink
Avoid dead Polling/Writing on driver exit
Browse files Browse the repository at this point in the history
Connection logic update
Some cleanup
  • Loading branch information
alexsavulescu committed Oct 30, 2024
1 parent 7e00b80 commit 0bad1fb
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 57 deletions.
42 changes: 2 additions & 40 deletions Common/Logger.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,7 @@ namespace Common {
*/
class Logger{
public:
Logger() : creationTime(0), logNum(0), devNum(0), prefix(""){
};

Logger(int devNum):creationTime(0), logNum(0), devNum(devNum){
updatePrefix();
};
Logger(){};

~Logger();

Expand All @@ -52,11 +47,6 @@ public:
*/
static void setLogLvl(int16_t lvl);

/*!
* Setting number of device to which logger is binded
* \param device number
*/
void setDevNum(int num);

static void globalInfo(int lvl, const char *note1 = NULL, const char* note2 = NULL, const char* note3 = NULL);

Expand All @@ -72,26 +62,8 @@ public:

static const int getLogLevel();
private:

std::fstream& getStream();

void closeStream();

void updatePrefix();

static int16_t loggingLevel;

std::fstream f;
long creationTime;
int logNum;

int devNum;

std::string prefix;

static const char * timestrformat;

mutex localVariableDataAccess;
static const char* timestrformat;
};


Expand All @@ -104,15 +76,5 @@ inline const int Logger::getLogLevel(){
return loggingLevel;
}

inline void Logger::setDevNum(int num){
lock_guard<mutex> raiiLock(localVariableDataAccess);
devNum = num;
updatePrefix();
}

inline void Logger::updatePrefix(){
prefix = devNum? (char *)("[MS " + CharString(devNum) + "] "): "";
}

}//namespace
#endif /* DEBUGMETHODS_HXX_ */
33 changes: 25 additions & 8 deletions RAMS7200LibFacade.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ RAMS7200LibFacade::RAMS7200LibFacade(RAMS7200MS& ms, queueToDPCallback cb)
void RAMS7200LibFacade::EnsureConnection(bool reduSwitch) {

if(reduSwitch) {
Common::Logger::globalInfo(Common::Logger::L1,__PRETTY_FUNCTION__, "Setting connection status on Redu Switch for PLC IP:" + CharString(ms._ip.c_str()));
RAMS7200MarkDeviceConnectionError(!_client->Connected());
}
if(_client->Connected() && ioFailures < Common::Constants::getMaxIoFailures()){
if(_client->Connected() && _wasConnected && ioFailures < Common::Constants::getMaxIoFailures()){
return;
} else {
if (_wasConnected) {
Expand All @@ -54,7 +55,7 @@ void RAMS7200LibFacade::EnsureConnection(bool reduSwitch) {
sleep_for(std::chrono::seconds(5));
}
} while(ms._run && !_wasConnected);
RAMS7200MarkDeviceConnectionError(false);
RAMS7200MarkDeviceConnectionError(!_wasConnected);
}
}

Expand All @@ -65,10 +66,11 @@ void RAMS7200LibFacade::Connect()
_client.reset(new TS7Client());

_client->SetConnectionParams(ms._ip.c_str(), Common::Constants::getLocalTsapPort(), Common::Constants::getRemoteTsapPort());
if(_client->Connect() == 0) {
if(_client->Connect() == 0 && _client->Connected()) {
Common::Logger::globalInfo(Common::Logger::L1,__PRETTY_FUNCTION__, "Snap7: Connected to '", ms._ip.c_str());
_wasConnected = true;
ioFailures = 0;
}
RAMS7200MarkDeviceConnectionError(!_client->Connected());
}


Expand All @@ -83,6 +85,10 @@ void RAMS7200LibFacade::Disconnect()

void RAMS7200LibFacade::Poll()
{
if(!_wasConnected){
Common::Logger::globalWarning(__PRETTY_FUNCTION__, "Not connected to PLC IP:", ms._ip.c_str());
return;
}
if(ms.vars.empty()){
Common::Logger::globalWarning(__PRETTY_FUNCTION__, "No addresses for PLC IP:", ms._ip.c_str());
return;
Expand Down Expand Up @@ -119,6 +125,10 @@ void RAMS7200LibFacade::Poll()
}

void RAMS7200LibFacade::WriteToPLC() {
if(!_wasConnected){
Common::Logger::globalWarning(__PRETTY_FUNCTION__, "Not connected to PLC IP:", ms._ip.c_str());
return;
}
std::vector<DPInfo> addresses;
std::vector<TS7DataItem> items;
{
Expand Down Expand Up @@ -242,6 +252,7 @@ void RAMS7200LibFacade::queueAll(std::vector<DPInfo>&& dpItems, std::vector<TS7D
} else {
failed << dpItems[i].dpAddress.c_str() << " ";
delete[] static_cast<char*>(s7items[i].pdata);
s7items[i].pdata = nullptr;
}
}

Expand All @@ -263,24 +274,30 @@ void RAMS7200LibFacade::doSmoothing(std::vector<DPInfo>&& dpItems, std::vector<T
std::lock_guard lock(ms._rwmutex);

for(uint i = 0; i < s7items.size(); i++) {
if(auto item = s7items[i]; item.Result == 0) {
auto& item = s7items[i];
if(item.Result == 0 && item.pdata != nullptr) {
const auto& DPInfo = dpItems[i];
Common::Logger::globalInfo(Common::Logger::L4, DPInfo.dpAddress.c_str(), Common::S7Utils::DisplayTS7DataItem(&item, Common::S7Utils::Operation::READ).c_str());
auto it = ms.vars.find(DPInfo.plcAddress);
if(it != ms.vars.end()) {
auto& var = it->second;
const auto dataSize = var._toDP.Amount * Common::S7Utils::DataSizeByte(var._toDP.WordLen);
if(var._toDP.pdata == nullptr) {
var._toDP.pdata = new char[dataSize];
}
if (std::memcmp(var._toDP.pdata, item.pdata, dataSize) != 0) {
std::memcpy(var._toDP.pdata, item.pdata, dataSize);
toDPItems.emplace_back(DPInfo.dpAddress.c_str(), dataSize, static_cast<char*>(item.pdata));
Common::Logger::globalInfo(Common::Logger::L4, DPInfo.dpAddress.c_str(), "--> Smoothing updated");
continue;
} else {
delete[] static_cast<char*>(item.pdata);
}
}
}
} else {
failed << dpItems[i].dpAddress.c_str() << " ";
delete[] static_cast<char*>(item.pdata);
}
delete[] static_cast<char*>(s7items[i].pdata);
item.pdata = nullptr;
}
}

Expand Down
17 changes: 8 additions & 9 deletions RAMS7200MS.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,12 @@ void RAMS7200MS::removeVar(std::string varName)

void RAMS7200MS::queuePLCItem(const std::string& varName, void* item)
{
try
{
std::lock_guard lock{_rwmutex};
vars.at(varName)._toPlc.pdata = item;
}
catch(const std::out_of_range& e)
{
Common::Logger::globalWarning(__PRETTY_FUNCTION__, "Undefined address", e.what());
}
std::lock_guard lock{_rwmutex};

if (vars.count(varName) == 0) {
Common::Logger::globalWarning(__PRETTY_FUNCTION__, "Undefined variable:", varName.c_str());
return;
}

vars.at(varName)._toPlc.pdata = item;
}

0 comments on commit 0bad1fb

Please sign in to comment.