stlehmann / pyads

Python wrapper for TwinCAT ADS
MIT License
252 stars 93 forks source link

How can i check the current connection status to the PLC ? #310

Closed FranciscoSantiago15 closed 2 years ago

FranciscoSantiago15 commented 2 years ago

Hi,

I m programming a Beckhoff CX7000 running with TwinCAT v3.1, and I m using the pyads library to stablish a connection.

Is it possible to know the state of the connection between the plc and the script? that is, I would like to know if it is possible to monitor the connection to know when it is broken or signal failures may have occurred.

Schwarz-XU commented 2 years ago

Hi, maybe you can use plc.read_state() to monitor the connection state

FranciscoSantiago15 commented 2 years ago

Hi, maybe you can use plc.read_state() to monitor the connection state

Yes i m using that but i dont understand what are the differences between '(5, 0)' and '(6, 0)'. And another case is when i disconnect the plc and try do the plc.read_state() i m always getting the '(5, 0)'

chrisbeardy commented 2 years ago

read state corresponds to these https://infosys.beckhoff.com/english.php?content=../content/1033/tcadsdll2/html/tcadsdll_enumadsstate.htm&id=7003723284846371174

you can add a notifcation property for this using

self._plc.add_device_notification(
                (int("0xF100", 16), int("0x0000", 16)),  # ADSIGRP_DEVICE_DATA, ADSIOFFS_DEVDATA_ADSSTATE
                pyads.NotificationAttrib(ctypes.sizeof(pyads.PLCTYPE_INT)),
                self._on_plc_status_change,
            )

not usre what happens to this though if you physically lose connection to the PLC, i.e. the cable is pulled out
chrisbeardy commented 2 years ago

this are also defined in the constants section of pyads, e.g.

pyads.ADSSTATE_RUN