Python OPC UA client communication with Siemens PLC TIA Portal

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi hello everyone today i'm gonna show you how to connect to the plc s7 1500 through the opc ua using the opc ua client for python so uh first of all let's let's create the project and connect the virtual plc using plc sim advanced so as you can he see i have a plc defined in here cpu 1513 as i said at the beginning we are gonna use opc ua protocol so what is important is that we have to create server on the plc site so we have to click this box and later on in the settings we also have to choose the license type so we're gonna go for large i'm gonna compile the project okay everything's fine i have a plc sim here here we have to use the plc sim virtual ethernet adapter because we need an a we need to connect it to it for the python later on i'm gonna create an instance of opc ua python i already have a have one here let's start it nice it's running because i already had some programming there but nevertheless we can download the software let's just download everything choose the proper interface okay here we got a notification that opc ui server will be restarted if it was started before okay should be everything's fine we are online plc is running we don't have any blocks so let's create some data block let's call it as an example top secret secret we would like to access some top secret data let's say we would like to see how many nintendo switches have been sold so let's call it number of salt nintendo switches okay we would like to have it as a number so let's put it as an integer and just for the sake of test let's have another value here called empty as an example do we still have some more or in the warehouse of those nintendo switches or not okay i'm gonna save the project download to the plc okay there we go go online we see zero and false that's fine now let's go to the uh python so i have created a virtual environment and installed all of the necessary packages i'm gonna show you how to do it as well yeah in order to be able to run opc ua client for python we have to download those two packages i'm gonna link it in the description as well so first one is opcway client gui here we have a comment for it pip install opc ua client so i'm gonna just type it in tip install opc u a client okay and the the other one is python opc ua so we have the same command here almost the same install opc way yeah now i'm gonna start the opc ua client there we go here in the top bar we have to put the corresponding ip address and the corresponding part of the server we can check it here if we go to the plc settings here properties and opc ua a little scroll a little bit down down here we got the address so we can copy it yes now press connect and yeah we are connected nice it means that now we are able to modify those values uh for the python opc ua client so let's go to the database we have created which is top secret here we have top secret yeah we have the empty one and number of sold nintendo switches empty one should be false value is false and the number of sold nintendo switches is zero so now if i'm gonna change here for 15 let's say hit refresh here yeah we got 15. nice so next thing what we can do is actually change those parameters using python in order to do it i've created a small script which i'm gonna share in the link in the description so basically what i'm doing here is from opc ua importing client and ua okay so basically what i'm doing here is from opc way importing client i have created three functions read input value write value integer and write valuable it's uh it's really simple these are really simple functions first of all we have to read the client id note yeah then get the value and then just print it out in the console the right value we have to do the same but we also have to override the value with the one that we would like to have in the main uh main part we are creating a client instance connecting for the same address trying to connect just for the debugging purposes printing out the root node that's how it looks now let's uh let's see how this if the script works so we can go python uh read and write opc a yeah we were connected and we got the object root note is 84 that's the print out here so if we go to the gui because it's easier to read it there we go object root note is 84 okay nice so basically that's what we need so if we would like to check the value of empty we need a node id basically that's how we are communicating so i'm gonna copy the node id here i'm gonna use the function we would the node id is a boolean empty empty is a boolean value it's false currently so so let's read the input value so we're gonna go red read input value and the node id the one that we have just copied okay let's run the script see what happens yeah value of the note we've put it's false great now let's change the value to true to see if it actually changes okay let's run it again value is true nice so let's do the same for the other one so we would like to read the input value but from the other node which is the number of salt nintendo switches so i'm just gonna copy the node id here paste it here okay run the script yeah we have 15 nice so let's try to override those values we're gonna use the right value build function same id but we also need to put the value that we would like to write in there so as we have it false now we would like to have it true let's see just double check yeah it's true so we're gonna go for false now we're gonna run the script just to see everything ua we need to import the ui as well that's the thing i've forgot ua okay true yeah so we see before we had false we had 15 and here we have changed the value of empty bit to true and now let's say we would like to change the value of salt nintendo switches so we will make some uh inconsistency in the warehousing systems so i'm gonna use the right value integer in here use the same node id as we did before and we'll put let's say that we've sold more than it's currently in the stock let's say 20 okay there we go this is not gonna change anymore same as this one okay let me show you this one here okay 15 we have 15 yeah now we got 20. nice so basically that's how you you can communicate through the opc ua client uh from python from the free opc ua and how you can manipulate values what is important that if we deselect the value accessible from hdmi web api we won't be able to access those values anymore let me download we're gonna have the restart of the server takes a while i'm gonna disconnect connect again go to objects plc data blocks global yeah it's empty nothing's dark it was here before if you try to run the script again let me change the values so for example 25 we're gonna get an error yeah no it requires no the node id refers to a node that does not exist in the server address space yeah it's because it's not accessible anymore so this is a really important thing this feature gets ticked as we are creating new values so if you would not like anyone to mess with your values like this it's important that you have to untick it but if you are using for example scada communicating through opc ui it has to be checked otherwise scada you won't be able to see those values on scada scada side you see i've just created a new one test i will quickly download it yes i would like to reinitialize the block disconnect connect again because we have the opc ui server restart and i would like to go not to the tiles but to the object go to the data box yeah we have top secret again and we have test what's the value of test it's false yeah so it's working okay thank you guys i hope you have enjoyed this quick video if you have any questions or remarks please let me know see you next time bye bye
Info
Channel: plcwars
Views: 21,238
Rating: undefined out of 5
Keywords: python, opcua, tiaportal, siemens
Id: BqaXtVe9z3s
Channel Id: undefined
Length: 14min 7sec (847 seconds)
Published: Sat Feb 19 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.