diff --git a/silecs-communication-cpp/src/silecs-communication/interface/communication/MBConnection.cpp b/silecs-communication-cpp/src/silecs-communication/interface/communication/MBConnection.cpp index 830200ad72024843caf3a4cd31bf2efa1ede1e88..d95766135cac0a77a1da49fbeedc3b5660c28e63 100644 --- a/silecs-communication-cpp/src/silecs-communication/interface/communication/MBConnection.cpp +++ b/silecs-communication-cpp/src/silecs-communication/interface/communication/MBConnection.cpp @@ -143,8 +143,14 @@ namespace Silecs int MBConnection::readData(PLC* thePLC, long address, unsigned long offset, unsigned long size, unsigned char* pBuffer) { - int rc = 0; - int error = 0; + int rc = 0; + int error = -1; + + if (address < 0) + { + LOG(COMM) << "Invalid address: " << address; + throw SilecsException(__FILE__, __LINE__, PARAM_INCORRECT_BLOCK_ADDRESS, StringUtilities::toString(address)); + } //Schneider uses 16bit alignment memory. Block address is expressed in bytes, must be an even value! if (address % 2) throw SilecsException(__FILE__, __LINE__, PARAM_INCORRECT_BLOCK_ADDRESS, StringUtilities::toString(address)); @@ -180,8 +186,14 @@ namespace Silecs int MBConnection::writeData(PLC* thePLC, long address, unsigned long offset, unsigned long size, unsigned char* pBuffer) { - int rc = 0; - int error = 0; + int rc = 0; + int error = -1; + + if (address < 0) + { + LOG(COMM) << "Invalid address: " << address; + throw SilecsException(__FILE__, __LINE__, PARAM_INCORRECT_BLOCK_ADDRESS, StringUtilities::toString(address)); + } //Schneider uses 16bit alignment memory. Block address is expressed in bytes, must be an even value! if (address % 2) throw SilecsException(__FILE__, __LINE__, PARAM_INCORRECT_BLOCK_ADDRESS, StringUtilities::toString(address)); diff --git a/silecs-communication-cpp/src/silecs-communication/interface/communication/SNAP7Connection.cpp b/silecs-communication-cpp/src/silecs-communication/interface/communication/SNAP7Connection.cpp index b31c9fab12ebac9b3ade105c67c2ebf8ea1796be..aff5c1505b785977e6a5390a8c94ec018ac993ef 100644 --- a/silecs-communication-cpp/src/silecs-communication/interface/communication/SNAP7Connection.cpp +++ b/silecs-communication-cpp/src/silecs-communication/interface/communication/SNAP7Connection.cpp @@ -201,7 +201,14 @@ bool SNAP7Connection::isRunning(PLC* thePLC) } int SNAP7Connection::readMemory(PLC* thePLC, long DBn, unsigned long offset, unsigned long size, unsigned char* pBuffer) { - int err = 0; + int err = 1; + + if (DBn < 0) + { + LOG(COMM) << "Invalid DB number: " << DBn; + throw SilecsException(__FILE__, __LINE__, PARAM_INCORRECT_BLOCK_ADDRESS, StringUtilities::toString(DBn)); + } + //(re)connect the PLC if needed and (re)synchronize the retentive registers if (doOpen(thePLC)) { @@ -217,7 +224,14 @@ int SNAP7Connection::readMemory(PLC* thePLC, long DBn, unsigned long offset, uns } int SNAP7Connection::writeMemory(PLC* thePLC, long DBn, unsigned long offset, unsigned long size, unsigned char* pBuffer) { - int err = 0; + int err = 1; + + if (DBn < 0) + { + LOG(COMM) << "Invalid DB number: " << DBn; + throw SilecsException(__FILE__, __LINE__, PARAM_INCORRECT_BLOCK_ADDRESS, StringUtilities::toString(DBn)); + } + //(re)connect the PLC if needed and (re)synchronize the retentive registers if (doOpen(thePLC)) { @@ -234,7 +248,13 @@ int SNAP7Connection::writeMemory(PLC* thePLC, long DBn, unsigned long offset, un int SNAP7Connection::readIO(PLC* thePLC, long address, unsigned long offset, unsigned long size, unsigned char* pBuffer) { - int err = 0; + int err = 1; + + if (address < 0) + { + LOG(COMM) << "Invalid address: " << address; + throw SilecsException(__FILE__, __LINE__, PARAM_INCORRECT_BLOCK_ADDRESS, StringUtilities::toString(address)); + } //(re)connect the PLC if needed and (re)synchronize the retentive registers if (doOpen(thePLC)) { @@ -252,7 +272,13 @@ int SNAP7Connection::readIO(PLC* thePLC, long address, unsigned long offset, uns int SNAP7Connection::writeIO(PLC* thePLC, long address, unsigned long offset, unsigned long size, unsigned char* pBuffer) { - int err = 0; + int err = 1; + + if (address < 0) + { + LOG(COMM) << "Invalid address: " << address; + throw SilecsException(__FILE__, __LINE__, PARAM_INCORRECT_BLOCK_ADDRESS, StringUtilities::toString(address)); + } //(re)connect the PLC if needed and (re)synchronize the retentive registers if (doOpen(thePLC)) {