gijzelaerr / python-snap7

A Python wrapper for the snap7 PLC communication library
http://python-snap7.readthedocs.org/
MIT License
643 stars 245 forks source link

client.db_read() command blocking #293

Closed Dhruva0707 closed 3 years ago

Dhruva0707 commented 3 years ago

Hello I have a S7-1500 PLC, a code snippet is as follows:

from threading import Lock()

client = Client()
client.connect(ip, rack, slot)

lock = Lock()

def collect_data(db_num, start_address, size):
    try:
        with lock:
            result = client.db_read(db_num, start_address, size)
        return result
    except Exception as e:
        logger.error("Unable to collect data: "+str(e)
        return None

The above function is called via various threads. As one can notice, if the db_read function fails, an error is logged. Intermittently, this stops the data collection for all threads. This can mean that the db_read has not returned any data and not created any exception, which can be caught by the try/except block.

My questions are the following: 1> Is the db_read command blocking? 2> If yes, is there any way to get around this issue?

swamper123 commented 3 years ago

@Dhruva0707 I guess there is something missing ;)

Dhruva0707 commented 3 years ago

@Dhruva0707 I guess there is something missing ;)

Updated

swamper123 commented 3 years ago

1) db_read is not async, so it is blocking. 2) You may want to use as_db_read and waiting until a result exists with wait_as_completion or checking if now a result exist with check_as_completion (last one can be used in a loop).

Dhruva0707 commented 3 years ago

Thanks a lot for your response!

I will check it out.

As I am reading the documentation I see that a 'buffer' is required in the as_db_read function. Any tutorial or example code I see to get the datatype of the buffer and using 'wait_as_completion' or 'check_as_completion' ?

Dhruva0707 commented 3 years ago

Found it in the tests

Dhruva0707 commented 3 years ago

We can close this issue, as the async call resolved the same.