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: KnielPowerSupplySocketCommEnhanced.py # 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 try: input = raw_input except NameError: pass # init screen # print(chr(27) + "[2J") print("\033[1m" + 'Kniel Power Supply Socket Example') print("\033[0m") # ask user for IP & Port # print("Enter connection information ...") targetIP = input("Insert IP (example: 192.168.1.1): ") 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 try: srvsock.connect((targetIP, targetPORT)) except socket.timeout: sys.exit("Socket timeout") except socket.error: sys.exit("Socket error") except Exception as e: print(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") srvsock.close() print("\n\nBye Bye") break # stops the loop else: MESSAGE = n + "\r" try: srvsock.sendall(MESSAGE) except TypeError: srvsock.sendall(MESSAGE.encode('ascii')) print("Send:\t\t" + MESSAGE) # receive data = srvsock.recv(4096) try: 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 vertrieb@kniel.de.
Please complete the mandatory fields (marked by red stars) in the form before sending.