chwiede / pyads

Beckhoff ADS implementation for python
MIT License
49 stars 14 forks source link

Reading Values from a CX1100 #2

Closed stevienull closed 9 years ago

stevienull commented 9 years ago

Hello,

im trying to read values from a CX1100 using your library.

I allready got a response ( "TCatPlcCtrl (Version 2.11.1810)" ) from the CX by excuting this code:

#!/usr/bin/python2
from pyads import *  
with AdsDevice(amsTarget="192.168.1.20.1.1:801", amsSource="192.168.1.25.0.0:800") as device:
    info = device.ReadDeviceInfo()
    print(info)

For reading values from the PLC i tried to modify the io example:

#!/usr/bin/python2
from pyads import *  

with AdsDevice(amsTarget="192.168.1.20.1.1:801", amsSource="192.168.1.25.0.0:800") as device:

    # create ads io device, register some variables and initialize
    io = AdsIO(device)    
    io.Register(SymbolInfo("Switch", AdsClient.AdsIndexGroupIn, 8 , AdsDatatype.Bool, 0))
    io.Initialize()
    print io.Get("Switch")   

I always get a "False" from this script. Which parameters do i have to pass with "io.Register" ?

Do I get an error if the if the name of the variable is not found on the PLC?

Do I have to configure anything else?

Best Regards Stefan

chwiede commented 9 years ago

Hello Stefan,

your call of register method seems to be correct for me. You have to enter a name, index group, index offset and data type for the symbolinfo.

Have you tried to read and write the mapped values in a loop?

    # map input "switch" cyclic to output "light" 
    while 1:
        io.ReadAll()
        io.Set("light", io.Get("switch"))       
        io.WriteAll()  

That's also done in iosample.py

Good luck, Christoph

stevienull commented 9 years ago

Hello Christoph,

Thanks for your reply. I now can read some of my PLC values using this peace of code:

#!/usr/bin/python2

from pyads import *  

with AdsDevice(amsTarget="192.168.1.20.1.1:27906", amsSource="192.168.1.25.0.0:800") as device:

    # create ads io device, register some variables and initialize
    io = AdsIO(device)    
    io.Register(SymbolInfo("Temp_Oven", AdsClient.AdsIndexGroupOut,  0xA , AdsDatatype.Int16, 0))

    while 1:  
    io.Initialize()
    io.ReadAll()
    print io.Get("Temp_Oven")
    io.WriteAll()    

I found out, that I was using the wrong port. The right one for this variable, I found with the Twicat System Manager -> navigate to the inputs -> ADS Info.

But I still have some questions: 1.: I had to call the Initialize method for each run in the loop. If I don't Initialize each time, the values do not update. Why is that different from the iosample?

2.: After I I finally got some values from the PLC I tried to read boolean variables. I took the ADS Infos from the System Manager ( Port 27906, IGrp: 0xF31, IOffs: 0x79, Len 1 ). But from this code, I just get a "None" as return value:

#!/usr/bin/python2

from pyads import *  

with AdsDevice(amsTarget="192.168.1.20.1.1:27906", amsSource="192.168.1.25.0.0:800") as device:

    # create ads io device, register some variables and initialize
    io = AdsIO(device)    
    io.Register(SymbolInfo("Taster_Kueche_Mehrfach_3", 0xF031, 0x79, AdsDatatype.Bool, 0))
   while 1:  
    io.Initialize()
    io.ReadAll()
    print io.Get("Taster_Kueche_Mehrfach_3")
    io.WriteAll()   

Do you have any hints how I can read boolean values?

Stefan

chwiede commented 9 years ago

Hi Stefan,

there's definitly no need to call io.Initialize() in each loop. Look into the source of adsio.py, the method just bundles ReadAll() and WriteAll().

Maybe you could debug your code with Eclipse or something similar.

I wonder about your port 27906, this isn't usual as far as i know. Commonly used ports are 800 for bus couplers or 801/811/etc. for TC2 devices, repsective 851/861/etc. for TC2 devices due to choosen runtime.

However, you can read a Variable directly without the io-stuff:

with AdsDevice(amsTarget="192.168.1.20.1.1:801", amsSource="192.168.1.25.0.0:800") as device:
    variableValue = device.ReadByName(variableName, adsDatatype)

ReadByName and WriteByName are always slower than using SymbolHandle or index-offset.

Please check the other available methods in AdsDevice.py - i'd been to lazy to write a documentation... ;-)

Maybe you don't need something like iosample at all, it just shows a way to handle states and values with a state-machine like architecture.

stevienull commented 9 years ago

Hello Christoph,

sorry for my late reply.

I am finally able to read and write data. With your last hint it works now! Thank you for your help!