1. Home
    1. Dashboard
    2. Search
  2. Forum
    1. Unresolved Threads
    2. Members
      1. Recent Activities
      2. Users Online
      3. Team Members
      4. Search Members
      5. Trophys
  3. Articles
  4. Blog
  5. Videos
  6. Jobs
  7. Shop
    1. Orders
  • Login or register
  • Search
This Thread
  • Everywhere
  • This Thread
  • This Forum
  • Articles
  • Pages
  • Forum
  • Blog Articles
  • Products
  • More Options
  1. Robotforum - Support and discussion community for industrial robots and cobots
  2. Forum
  3. Industrial Robot Support and Discussion Center
  4. KUKA Robot Forum
Your browser does not support videos RoboDK Software for simulation and programming
Visit our Mainsponsor
IRBCAM
Robotics Channel
Robotics Training
Advertise in robotics
Sponsored Ads

Communication EthernetKRL-Python

  • Andrea
  • July 3, 2024 at 10:23 AM
  • Thread is Unresolved
  • Andrea
    Reactions Received
    1
    Posts
    4
    • July 3, 2024 at 10:23 AM
    • #1

    Hi, I am trying to establish communication between the robot and a Python script. I have purchased the EthernetKRL add-on package. When I execute the communication between the robot and Hercules, I can both receive and send data, and the variable Nmb is set. However, when I use my Python script, I can receive data from the robot, but I cannot set the variable; the robot shows the error: EKI00512. I have changed the encoding in Python, the libraries for TCP/IP communication, and tried using Java as well. Thank you in advance.

    Python script:

    Code
    import socket
     
    def start_server(server_ip, server_port):
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
            s.bind((server_ip, server_port))
            s.listen()
            print(f"Server listening on {server_ip}:{server_port}")
           
            while True:
                conn, addr = s.accept()
                with conn:
                    print(f"Connected by {addr}")
                    try:
                        request = "<Sensor><Nmb>10</Nmb></Sensor>"
                        bytesToSend = str.encode(request)
                        conn.sendall(bytesToSend)
                        print(f"Request sent to robot: {request}")
                       
                        response = conn.recv(4096).decode('utf-8')
                        print(f"Received response from robot:\n{response}")
                   
                    except Exception as e:
                        print(f"Error: {e}")
                    finally:
                        conn.close()
     
    if __name__ == "__main__":
        server_ip = '192.168.10.100'
        server_port = 59152
        start_server(server_ip, server_port)
    Display More

    Kuka program:

    Code
    &ACCESS RVP
    &REL 16
    &PARAM SensorITMASK = *
    &PARAM TEMPLATE = C:\KRC\Roboter\Template\vorgabe
    &PARAM DISKPATH = KRC:\R1\Program
    DEF XmlTransmit( )
    ;FOLD Declaration
      INT i
      INT valueInt
      DECL EKI_STATUS RET
      CHAR valueChar[256]
      REAL valueReal
      BOOL valueBOOL
      FRAME valueFrame
    ;ENDFOLD (Declaration)
    ;FOLD Communicated data
    ;FOLD receive from external program
    ; <Sensor>
      ; <Message>Example message</Message>
      ; <Positions>
        ; <Current X="4645.2" />
        ; <Before>
          ; <X>0.9842</X>
        ; </Before>
      ; </Positions>
      ; <Nmb>8</Nmb>
      ; <Status>
        ; <IsActive>1</IsActive>
      ; </Status>
      ; <Read>
        ; <xyzabc X="210.3" Y="825.3" Z="234.3" A="84.2" B="12.3" C="43.5" />
      ; </Read>
      ; <Show error="0" temp="9929">Taginfo in attributes</Show>
      ; <Free>2912</Free>
    ; </Sensor>
    ;ENDFOLD (Receive from external program)
    ;FOLD Send to external program
    ; <Robot>
      ; <Data>
        ; <ActPos X="1000.12">
        ; </ActPos>
           ; <LastPos A="..." B="..." C="..." X="..." Y="..." Z="...">
        ; </LastPos>
      ; </Data>
      ; <Mode>ConnectSensor</Mode>
      ; <RobotLamp>
        ; <GrenLamp>
          ; <LightOn>1</LightOn>
        ; </GrenLamp>
      ; </RobotLamp>
      ; <Status>12345678</Status>
    ; </Robot>
    ;ENDFOLD (Send to external program)
    ;ENDFOLD (Communicated data)
    ;FOLD INI  
      ;FOLD BASISTECH INI  
      BAS (#INITMOV,0 )
      ;ENDFOLD (BASISTECH INI)
      ;FOLD USER INI
        ;Make your modifications here
      ;ENDFOLD (USER INI)
    ;ENDFOLD (INI)
    ;FOLD Initialize sample data
      FOR i=(1) TO (256)  
        valueChar[i]=0
      ENDFOR 
      valueInt=0
      valueReal=0.0
      valueFrame={X 0.0,Y 0.0,Z 0.0,A 0.0,B 0.0,C 0.0}
      valueBOOL=FALSE
    ;ENDFOLD (Initialize sample data)
    
    RET=EKI_Init("XmlTransmit")
    RET=EKI_Open("XmlTransmit")
    
    ;FOLD Write data to connection
      ; Write frame to <LastPos X="" Y="" Z="" A="" B="" C="" />
      ;RET=EKI_SetFrame("XmlTransmit","Robot/Data/LastPos", TOOL_DATA[1])
      ; Write real to <ActPos X="" />
      ;RET=EKI_SetReal("XmlTransmit","Robot/Data/ActPos/@X", 1000.12)
      ; Write int to <Status></Status>
      ;RET=EKI_SetInt("XmlTransmit","Robot/Status", 12345678)
      ; Write string to <Mode></Mode>
      ;RET=EKI_SetString("XmlTransmit","Robot/Mode","ConnectSensor")
      ; Write bool to <LightOn></LightOn>
      ;RET=EKI_SetBool("XmlTransmit","Robot/RobotLamp/GrenLamp/LightOn",true)
    ;ENDFOLD (Write data to connection)
    ;FOLD Send data to external program
      ;RET = EKI_Send("XmlTransmit","Robot")
      WAIT SEC 1
    ;ENDFOLD (Send data to external program)
    
    ;FOLD Get received sensor data
      ; Get string in <Message>Example message</Message>
      ;RET=EKI_GetString("XmlTransmit","Sensor/Message",valueChar[])
      ; Get real value in <Current X="4645.2" />
      ;RET=EKI_GetReal("XmlTransmit","Sensor/Positions/Current/@X",valueReal)
      ; Get int value in <Nmb>8</Nmb>
      RET=EKI_GetInt("XmlTransmit","Sensor/Nmb",valueInt)
      ; Get bool value in textnode <IsActive>1</IsActive>
      ;RET=EKI_GetBool("XmlTransmit","Sensor/Status/IsActive" ,valueBOOL)
      ; Get bool value in attribute <Show error="0" />
      ;RET=EKI_GetBool("XmlTransmit","Sensor/Show/@error" ,valueBOOL)
      ; Get frame in <xyzabc X="210.3" Y="825.3" Z="234.3" A="84.2" B="12.3" C="43.5" />
      ;RET=EKI_GetFrame("XmlTransmit","Sensor/Read/xyzabc",valueFrame)
    ;ENDFOLD (Get received sensor data)
    CPosition=valueInt
    RET=EKI_Close("XmlTransmit")
    RET=EKI_Clear("XmlTransmit")
    END
    Display More

    Kuka xml file:

    Code
    <ETHERNETKRL>
    	<CONFIGURATION>
    		<EXTERNAL>
    			<IP>192.168.10.100</IP>
    			<PORT>59152</PORT>
    		</EXTERNAL>
    	</CONFIGURATION>
    	<RECEIVE>
    		<XML>
    			<ELEMENT Tag="Sensor/Nmb" Type="INT" EOS="13"/>
    		</XML>
    	</RECEIVE>
    	<SEND>
    		<XML>
    			<ELEMENT Tag="Robot/Data/LastPos/@X"/>
    			<ELEMENT Tag="Robot/Data/LastPos/@Y"/>
    			<ELEMENT Tag="Robot/Data/LastPos/@Z"/>
    			<ELEMENT Tag="Robot/Data/LastPos/@A"/>
    			<ELEMENT Tag="Robot/Data/LastPos/@B"/>
    			<ELEMENT Tag="Robot/Data/LastPos/@C"/>
    			<ELEMENT Tag="Robot/Data/ActPos/@X"/>
    			<ELEMENT Tag="Robot/Status"/>
    			<ELEMENT Tag="Robot/Mode"/>
    			<ELEMENT Tag="Robot/RobotLamp/GrenLamp/LightOn"/>			
    		</XML>
    	</SEND>
    </ETHERNETKRL>
    Display More
  • MOM July 3, 2024 at 11:01 AM

    Approved the thread.
  • panic mode
    Reactions Received
    1,297
    Trophies
    11
    Posts
    13,141
    • July 3, 2024 at 2:32 PM
    • #2

    you have a software related question but did not state software versions.


    and what is

    EOS="13"

    doing in the configuration when the type and size of INT variable is known? i would expect this only to be valid for things of variable length (string/stream).


    1) read pinned topic: READ FIRST...

    2) if you have an issue with robot, post question in the correct forum section... do NOT contact me directly

    3) read 1 and 2

  • Andrea
    Reactions Received
    1
    Posts
    4
    • July 3, 2024 at 3:42 PM
    • #3

    Thank you for the response. Disregarding EOS was just a test; I have removed it, but the problem remains. I am using EthernetKRL V3.2.3 on KSS 8.7.7 and am using the KUKA XmlTransmit example with some modifications.

  • Online
    SkyeFire
    Reactions Received
    1,061
    Trophies
    12
    Posts
    9,463
    • July 3, 2024 at 4:11 PM
    • #4
    Quote from Andrea

    When I execute the communication between the robot and Hercules, I can both receive and send data, and the variable Nmb is set.

    What's Hercules?

    Quote from Andrea

    the robot shows the error: EKI00512.

    Which is? It's not like anyone has all the error codes memorized.

    My first thought is to try Wiresharking both ways, and compare exactly what is being sent to the KRC.

  • panic mode
    Reactions Received
    1,297
    Trophies
    11
    Posts
    13,141
    • July 3, 2024 at 4:16 PM
    • #5

    manual states that message 512 indicates external problem or disturbance... cable unplugged... or server dropped connection...or server stops running...

    1) read pinned topic: READ FIRST...

    2) if you have an issue with robot, post question in the correct forum section... do NOT contact me directly

    3) read 1 and 2

  • Andrea
    Reactions Received
    1
    Posts
    4
    • July 3, 2024 at 6:21 PM
    • #6

    Yes, I have also seen the documentation but it is not specific. The peculiar thing is that it works with Hercules (https://www.hw-group.com/software/hercules-setup-utility). I tried with Wireshark, but it's not very clear. You can find the Wireshark screenshots at this link: https://imgur.com/a/HaDcjU6. The Wireshark files are available for download for 7 days from today at this link: https://we.tl/t-yWUcP8hBv8

  • panic mode
    Reactions Received
    1,297
    Trophies
    11
    Posts
    13,141
    • July 3, 2024 at 8:07 PM
    • #7

    hercules probably does not drop connection when it sees a problem, while your python code does after throwing exception. so did you check if that is the cae and what the exception is? you could try to comment out or remove code to disconnect. after all, python is running as server and connecting/disconnecting should be something that client does.

    1) read pinned topic: READ FIRST...

    2) if you have an issue with robot, post question in the correct forum section... do NOT contact me directly

    3) read 1 and 2

  • Online
    SkyeFire
    Reactions Received
    1,061
    Trophies
    12
    Posts
    9,463
    • July 3, 2024 at 8:54 PM
    • #8

    Well, TCP/IP isn't my strength, but the two biggest differences that jump out at me between the two captures is that the Hercules one has a sequence:
    1. PSH, ACK (this is where the <SENSOR><NMB> gets sent
    2. ACK (acknowledgement)
    3. FIN, ACK (final handshake to end the data exchange)

    Where the Python capture goes:
    1. PSH, ACK
    2. RST, ACK (this usually means something went wrong)

    There's no discernible difference in the data packet, aside from the TCP/IP headers, and those I would expect to be different.

    However, the PSH,ACK and RST,ACK are both coming from the Python side, within <1ms between them, with no return traffic from the robot. At the moment, my best guess is that your Python code is sending the data and then instantly closing the port, without waiting for the ACK and FIN,ACK to come back from the robot. This might explain the KRC error message -- it's trying to handshake with a port that's been closed.

  • Andrea
    Reactions Received
    1
    Posts
    4
    • July 4, 2024 at 7:37 AM
    • #9

    Hi, I did some tests and it's as you said, the server was being closed too soon. I made some changes to my Python script and now it works. Here is my code:

    Code
    import socket
    import time
    import threading
    
    def handle_client(conn, addr):
        try:
            while True:
                request = '<Sensor><Nmb>11</Nmb></Sensor>'
                time.sleep(0.5)
                conn.sendall(request.encode('ascii'))
                print(f"Request sent to robot {request}")
        except Exception as e:
            print(f"Error: {e}")
        finally:
            conn.close()
            print(f"Connection with {addr} closed")
    
    def start_server(server_ip, server_port):
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
            s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            s.bind((server_ip, server_port))
            s.listen()
            print(f"Server listening on {server_ip}:{server_port}")
    
            while True:
                conn, addr = s.accept()
                print(f"Connected by {addr}")
                client_thread = threading.Thread(target=handle_client, args=(conn, addr))
                client_thread.daemon = True
                client_thread.start()
    
    if __name__ == "__main__":
        server_ip = '192.168.10.100'
        server_port = 59152
        start_server(server_ip, server_port)
    Display More

    Thank you so much!!!

Advertising from our partners

IRBCAM
Robotics Channel
Robotics Training
Advertise in robotics
Advertise in Robotics
Advertise in Robotics

Job Postings

  • Anyware Robotics is hiring!

    yzhou377 February 23, 2025 at 4:54 AM
  • How to see your Job Posting (search or recruit) here in Robot-Forum.com

    Werner Hampel November 18, 2021 at 3:44 PM
Your browser does not support videos RoboDK Software for simulation and programming

Tag Cloud

  • abb
  • Backup
  • calibration
  • Communication
  • CRX
  • DCS
  • dx100
  • dx200
  • error
  • Ethernet
  • Ethernet IP
  • external axis
  • Fanuc
  • help
  • hmi
  • I/O
  • irc5
  • IRVIsion
  • karel
  • kawasaki
  • KRC2
  • KRC4
  • KRC 4
  • krc5
  • KRL
  • KUKA
  • motoman
  • Offset
  • PLC
  • PROFINET
  • Program
  • Programming
  • RAPID
  • roboguide
  • robot
  • robotstudio
  • RSI
  • safety
  • Siemens
  • simulation
  • SPEED
  • staubli
  • tcp
  • TCP/IP
  • teach pendant
  • vision
  • Welding
  • workvisual
  • yaskawa
  • YRC1000

Thread Tag Cloud

  • abb
  • Backup
  • calibration
  • Communication
  • CRX
  • DCS
  • dx100
  • dx200
  • error
  • Ethernet
  • Ethernet IP
  • external axis
  • Fanuc
  • help
  • hmi
  • I/O
  • irc5
  • IRVIsion
  • karel
  • kawasaki
  • KRC2
  • KRC4
  • KRC 4
  • krc5
  • KRL
  • KUKA
  • motoman
  • Offset
  • PLC
  • PROFINET
  • Program
  • Programming
  • RAPID
  • roboguide
  • robot
  • robotstudio
  • RSI
  • safety
  • Siemens
  • simulation
  • SPEED
  • staubli
  • tcp
  • TCP/IP
  • teach pendant
  • vision
  • Welding
  • workvisual
  • yaskawa
  • YRC1000

Tags

  • KUKA
  • xml
  • Python
  • TCP/IP
  • Ethernet KRL

Users Viewing This Thread

  • 1 Guest
  1. Privacy Policy
  2. Legal Notice
Powered by WoltLab Suite™
As a registered Member:
* You will see no Google advertising
* You can translate posts into your local language
* You can ask questions or help the community with your knowledge
* You can thank the authors for their help
* You can receive notifications of replies or new topics on request
* We do not sell your data - we promise

JOIN OUR GREAT ROBOTICS COMMUNITY.
Don’t have an account yet? Register yourself now and be a part of our community!
Register Yourself Lost Password
Robotforum - Support and discussion community for industrial robots and cobots in the WSC-Connect App on Google Play
Robotforum - Support and discussion community for industrial robots and cobots in the WSC-Connect App on the App Store
Download