Using Python to connect to a power supply

Many programing languages offer support for connecting to peripherals using RS232 or USB. This is an example script written in Python .

#!/usr/bin/env python2
# coding=utf-8
# Name:
# Function:		Demo
#			Starts with connect dialog (IP and port)
# 			Connects to device
#			Starts loop which asks for query commands

import socket
import sys

# detect proper input function
    input = raw_input
except NameError:

# init screen
print(chr(27) + "[2J")
print("\033[1m" + 'Kniel Power Supply Socket Example')

# ask user for IP & Port
print("Enter connection information ...")
targetIP = input("Insert IP (example: ")
targetPORT = input("Insert Port (default: 10001): ")

# check user input
if not targetIP:  # empty IP is mission critical
    sys.exit("\nEmpty IP error")
if not targetPORT:
    print("Falling back to default port")
    targetPORT = "10001"  # if empty - use default port

# Try to connect to ip:port
srvsock = socket.socket()
srvsock.settimeout(3)  # 3 second timeout on commands
print("\nTrying to establish connection to " + targetIP + ":" + targetPORT)
targetPORT = int(targetPORT)  # Port must be integer
    srvsock.connect((targetIP, targetPORT))
except socket.timeout:
    sys.exit("Socket timeout")
except socket.error:
    sys.exit("Socket error")
except Exception as e:
    sys.exit("Unknown error")

print("Connection established\n")

# Start user input loop
while True:
    n = input("Insert query or (c)ancel?: ")
    if n == "c":
        print("\nClosing socket")
        print("\n\nBye Bye")
        break  # stops the loop
        MESSAGE = n + "\r"
        except TypeError:
        print("Send:\t\t" + MESSAGE)
        # receive
        data = srvsock.recv(4096)
            print("Received:\t" + data)
        except TypeError:
            print("Received:\t" + data.strip().decode('ascii'))


Inquire for information

Sending your email. Please wait...

Thanks for sending your email! We'll get back to you shortly.

There was a problem sending your email. Please try again or send us a message to

Please complete the mandatory fields (marked by red stars) in the form before sending.


Fixed voltages
for 19” systems

Ajustable power supplies
for 19” systems

Programmable power supplies
for 19” systems

energy for 19” systems

Fixed voltages
for wall mounting and DIN-rail mounting

Adjustable power supplies
for wall mounting and DIN-rail mounting

Programmable power supplies
for wall mounting and DIN-rail mounting

energy installation for wall mounting

Firmware, Flash-Tool, Drivers

General information


Kniel System-Electronic GmbH
Kurzheckweg 8 · 76187 Karlsruhe · Germany ·