""" This is some code for communicating with a Delta-Tau PMAC motor controller as used at SINQ from python. Python 3 is assumed and tested. We use the ethernet protocol of the Pmac. This code benefits from the SICS driver for Pmac which in turn benefits from the driver written by Pete Leicester at Diamond. Usage: python36 deltatau.py pmachost:port command Then returns the response for command. Mark Koennecke, October 2019 """ import struct import socket def packPmacCommand(command): # 0x40 = VR_DOWNLOAD # 0xBF = VR_PMAC_GETRESPONSE buf = struct.pack('BBHHH',0x40,0xBF,0,0,socket.htons(len(command))) buf = buf + bytes(command,'utf-8') return buf def readPmacReply(input): msg = bytearray() expectAck = True while True: b = input.recv(1) bint = int.from_bytes(b,byteorder='little') if bint == 2 or bint == 7: #STX or BELL expectAck = False continue if expectAck and bint == 6: # ACK return bytes(msg) else: if bint == 13 and not expectAck: # CR return bytes(msg) else: msg.append(bint) if __name__ == "__main__": from sys import argv addr = argv[1].split(':') s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) s.connect((addr[0],int(addr[1]))) buf = packPmacCommand(argv[2]) s.send(buf) reply = readPmacReply(s) print(reply.decode('utf-8') + '\n')