PLC and Python Communication, HELP

Just play around with the functions, and read the documentation.

You can read all different types. I would recommend to pack the data you want to send into a struct of a datablock. Then you can use this struct as parameter for the Tsend function.

On python side you have to convert the data into python types, using unpack. Keep in mind that the S7 uses big endian.

In the attached PDF I've shown how to convert a struct with a Word (2 Bytes), an Int (2 Byte) and a Real (4 Byte) value. Overall 8 bytes you have to send and receive.
 
Hi Thomas_v2
I have to send a value x of my Python script to the PLC and he read this value in a Word or Int?

Thanks for everything so far!! :D
 
Hi Thomas_v2
Its I send a string to the PLC and he assign the value to a variable of type INT or WORD, the envez to be in CHAR?
 
I guess you want to send a char array to the plc, and convert this into an integer value.

At first you have to convert the char-array into a string. There is a library function "Chars_TO_Strg" for this.
Then you can use the Function "STR_VAL" and convert the string into an integer, if the string contains valid chars for an integer.

Using strings inside a plc is much more complicated as in any other language like python. Why don't you use my example and send direct the binary integer values from your python script to the plc?
 
Hi Thomas_v2,
Sorry to leave the topic so much time off, is that I am very involved at the time this project, well, what I need is to send the Python script an integer value to the PLC, send more than one character.
Ex: Python sends to the PLC the value of 123456

Thanks!!
 
Last edited:
Open a socket and send:

sock.send(b"123456")

Then you've got this as char-array in your plc, and could convert it into the datatype you need.

I don't understand why you want to use Python and sockets, when you have neither knowledge in Python programming, nor in the basics of TCP sockets.
 
Hi Thomas_v2,
I have knowledge in Python, just never made a communication between Python and PLC, so my doubt, I was in doubt whether it would have to change something in Python to receive a Int in the PLC, and have to use Python as the current project currently underway only accepts Python!

Thanks!
 
I've used Python 2. Unfortunately Python 2 and Python 3 are not fully compatible.

Change the last two lines of my example to:
Code:
import socket

address = "192.168.1.191"
port = 2000
sock = socket.socket()
sock.connect((address, port))
sock.send(b"hello")




>>
Traceback (most recent call last):
File "D:\BHARGAV PATEL\DOC\Temp\python\6mar_pypl.py", line 6, in <module>
sock.connect((address, port))
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
<<
it shows like this, what is the solution of it buddy..?? (i am using.. TIA v14, LAN cable, siemens CPU 1214C DC/DC/DC, Thonny 3.2.6 IDE for python code):unsure:
 
Last edited:
File "D:\BHARGAV PATEL\DOC\Temp\python\6mar_pypl.py", line 6, in <module>
sock.connect((address, port))
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
<<
it shows like this, what is the solution of it buddy..?? (i am using.. TIA v14, LAN cable, siemens CPU 1214C DC/DC/DC,


Does your CPU has the same IP address as in the example (192.168.1.191)? Can you ping the IP address?


Also check the status value returned from the TCON block in the S7 program.
 
>>
Traceback (most recent call last):
File "D:\BHARGAV PATEL\DOC\Temp\python\6mar_pypl.py", line 6, in <module>
sock.connect((address, port))
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
<<
it shows like this, what is the solution of it buddy..?? (i am using.. TIA v14, LAN cable, siemens CPU 1214C DC/DC/DC, Thonny 3.2.6 IDE for python code):unsure:




That error messages usually* means there is no device on the internet at TCP/IP address 192.168.1.191 listening on port 2000.


It does tell you that the system running Python is able to get to the 192.168.1.x or 192.168.x.x. network via an ethernet interface that thinks is it on that network.


Could you supply some more information, please:


- what is the hardware, software and network configuration on the PC running Python? It looks like it is windows, so a dump of the command [ipconfig /all]** would be useful; if it is Linux then a dump of [/sbin/ifconfig -a] would be useful.


- The output of the command [arp -a] would also be useful.



- what is the network configuration of the PLC to which the python script is trying to connect? You may need to look for the Mac address on the hardware (twelve hexadecimal characters such as 0123456789abc or 01-23-45-67-89-0a-bc or 01:23:...), and then find it's TCP/IP address assignment in the TCP/IP router's DHCP web page (probably at http://192.168.1.1 or similar). A better question might be to find out how the PLC is getting its IP address.




If all of this terminology is confusing to you, then you may need to find your network administrator.




* I say "usually" because there could be a device that is ignoring the source TCP/IP system that is runing the Python script.


** strip the square brackets from those commands and issue them from a command line (e.g. [Win + R] => [cmd], or a shell window in Linux)
 
Last edited:

Similar Topics

Hello Guys, I want to establish profinet communication between siemens plc and my system using python programming. Which python lib can i use to...
Replies
12
Views
300
Hello everybody! I found option for creating communication with Q series via python. But can't find any tips for connecting with PLC FX series...
Replies
3
Views
5,368
Hi, I need to create a Python script to exchange data with a PLC schneider modicon m580 via Ethernet cable, if anyone knows something and can help...
Replies
14
Views
4,556
Good day dear Experts! Is there a way to simulate a communication between a S7-1200 PLC and python script? I know this is possible physically...
Replies
3
Views
3,537
I'm currently a senior in college working on a senior design project involving PLCs and Raspberry Pi. We have data taken using the PLC that we...
Replies
14
Views
4,053
Back
Top Bottom