First working version of the MasterMACS EPICS driver
Also added some test code
This commit is contained in:
53
utils/macmaster.py
Normal file
53
utils/macmaster.py
Normal file
@ -0,0 +1,53 @@
|
||||
"""
|
||||
Usage: macmaster.py macmasterhost port
|
||||
Listen for commands to send and returns reponse
|
||||
until exit has been typed
|
||||
|
||||
Mark Koennecke, March 2023
|
||||
"""
|
||||
|
||||
import socket
|
||||
import struct
|
||||
|
||||
|
||||
class MasterMACS():
|
||||
def __init__(self, host, port):
|
||||
self._socke = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
self._socke.connect((host, int(port)))
|
||||
|
||||
def send(self, command):
|
||||
buf = struct.pack('BBBB', 0x05, len(command) + 4, 0, 0x19)
|
||||
buf += bytes(command, 'utf-8')
|
||||
buf += struct.pack('BB', 0x0D, 0x03)
|
||||
self._socke.send(buf)
|
||||
|
||||
def receive(self):
|
||||
buf = self._socke.recv(35, socket.MSG_WAITALL)
|
||||
if len(buf) < 35:
|
||||
raise EOFException('Master MACS returned only %d bytes, 30 expected' % len(buf))
|
||||
idx = buf.find(0x0D)
|
||||
ackbyte = buf[idx-1]
|
||||
if ackbyte == 0x06:
|
||||
ack = 'ACK'
|
||||
elif ackbyte == 0x15:
|
||||
ack = 'NAK',
|
||||
else:
|
||||
ack = 'NO'
|
||||
reply = buf[4:idx-1].decode('utf-8')
|
||||
return ack, reply
|
||||
|
||||
if __name__ == "__main__":
|
||||
from sys import argv, exit, stdin
|
||||
if len(argv) < 3:
|
||||
print('Usage:\n\tmacmaster.py machost macport')
|
||||
exit(1)
|
||||
mac = MasterMACS(argv[1], argv[2])
|
||||
while(True):
|
||||
# import pdb; pdb.set_trace()
|
||||
line = stdin.readline()
|
||||
if line.find('exit') >= 0:
|
||||
exit(0)
|
||||
mac.send(line.strip())
|
||||
ack, reply = mac.receive()
|
||||
print('%s, %s' %(ack, reply))
|
||||
|
90
utils/syncMasterMAC.py
Executable file
90
utils/syncMasterMAC.py
Executable file
@ -0,0 +1,90 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
This little program synchronizes an EPICS substitutions file with values read
|
||||
from a MasterMAC motor controller.
|
||||
|
||||
Mark Koennecke, February 2023
|
||||
"""
|
||||
import sys
|
||||
|
||||
from macmaster import MasterMACS
|
||||
|
||||
def find_commas(rawline):
|
||||
comma_list = []
|
||||
for i in range(len(rawline)):
|
||||
if rawline[i] == ',':
|
||||
comma_list.append(i)
|
||||
return comma_list
|
||||
|
||||
def pretty_line(newlist, comma_list):
|
||||
"""
|
||||
adds spaces to match the tabbing of the patter line
|
||||
"""
|
||||
newline = ''
|
||||
for item, idx in zip(newlist, comma_list):
|
||||
length = len(newline) + 1 + len(item)
|
||||
if length < idx:
|
||||
newline += ' ' * (idx - length)
|
||||
newline += item
|
||||
newline += ','
|
||||
newline += newlist[-1]
|
||||
return newline[0:-1]
|
||||
|
||||
def transact(command):
|
||||
global mac
|
||||
mac.send(command)
|
||||
return mac.receive()
|
||||
|
||||
def fix_line(par_list, index_list):
|
||||
# import pdb; pdb.set_trace()
|
||||
addridx = index_list.index('ADDR')
|
||||
motNo = int(par_list[addridx])
|
||||
ack, reply = transact('%dR24' % motNo)
|
||||
idx = reply.find('=')
|
||||
lowlim = reply[idx+1:]
|
||||
lowidx = index_list.index('DLLM')
|
||||
par_list[lowidx] = lowlim.strip()
|
||||
ack, reply = transact('%dR23' % motNo)
|
||||
idx = reply.find('=')
|
||||
highlim = reply[idx+1:]
|
||||
highidx = index_list.index('DHLM')
|
||||
par_list[highidx] = highlim.strip()
|
||||
# speed = transact('Q%d03' % motNo)
|
||||
# speedidx = index_list['VELO']
|
||||
# par_list[speedidx] = speed.trim()
|
||||
return par_list
|
||||
|
||||
def scan_substitution_file(filename):
|
||||
index_list = None
|
||||
# import pdb; pdb.set_trace()
|
||||
with open(filename, 'r') as fin:
|
||||
rawline = fin.readline()
|
||||
while rawline:
|
||||
line = rawline.replace(' ','')
|
||||
line = line.strip('{}')
|
||||
l = line.split(',')
|
||||
if line.find('DHLM') > 0:
|
||||
index_list = l
|
||||
comma_list = find_commas(rawline)
|
||||
sys.stdout.write(rawline)
|
||||
elif not index_list:
|
||||
sys.stdout.write(rawline)
|
||||
else:
|
||||
if len(l) == len(index_list):
|
||||
newlist = fix_line(l, index_list)
|
||||
# newline = ','.join(newlist)
|
||||
newline = pretty_line(newlist, comma_list)
|
||||
sys.stdout.write('{' + newline + '\n')
|
||||
else:
|
||||
sys.stdout.write(rawline)
|
||||
rawline = fin.readline()
|
||||
|
||||
#------------------ main
|
||||
if len(sys.argv) < 4:
|
||||
print('Usage:\n\tsyncMMACSub.py <host> <port> <substitutions-file>\n')
|
||||
sys.exit(1)
|
||||
|
||||
mac = MasterMACS(sys.argv[1], sys.argv[2])
|
||||
scan_substitution_file(sys.argv[3])
|
||||
|
||||
|
Reference in New Issue
Block a user