To simplify the code that made up my last Python OptoMMP scripts, I put together an open-source Python package with system status, scratchpad, and I/O access functions. The socket and memory-map details are handled by the package while you just import and then interact with simple single-line function calls!
It’s optommp
on the Python Package Index so all you need to do is sudo pip install optommp
and then import optommp
at the top of your script, and you’re good to go.
The optommp GitHub repo has a basic readme, but there are also some sample scripts available so you can see it in use.
For example, here is demo.py
:
import sys
import optommp
from timeit import default_timer as timer
start = timer()
integer = 22
value = 0
index = int(sys.argv[1]) if (len(sys.argv) == 3) else 0
data = sys.argv[2] if (len(sys.argv) ==3) else 'NULL'
grvEpic = optommp.O22MMP()
print '\nTerry`s groov EPIC:'
print 'Writing ' + str(value) + ' to analog at mod1 ch0'
print 'Initial value = ' + str(grvEpic.GetAnalogPointValue(1,0))
print 'Write success -> ' + str(grvEpic.SetAnalogPointValue(1, 0, value))
print 'Updated value = ' + str(grvEpic.GetAnalogPointValue(1,0))
print ''
print 'ScratchPad string old value:\t' + grvEpic.GetScratchPadStringArea(index)
print 'Writing "' + data + '" to ScratchPad string area #' + str(index) + ' ->' + str(grvEpic.SetScratchPadStringArea(index, data))
print 'ScratchPad string new value:\t' + grvEpic.GetScratchPadStringArea(index)
print ''
print 'ScratchPad integer at index 0:\t' + str(grvEpic.GetScratchPadIntegerArea(0))
print 'Writing `' + str(integer) + '` to ScratchPad integer #0 ->' + str(grvEpic.SetScratchPadIntegerArea(0, integer))
print 'ScratchPad integer at index 0:\t' + str(grvEpic.GetScratchPadIntegerArea(0)) + '\n'
print 'ETH 0 MAC addr.: ' + str(grvEpic.MACAddressE0())
print 'ETH 0 IP addr. : ' + str(grvEpic.IPAddressE0())
print 'ETH 1 MAC addr.: ' + str(grvEpic.MACAddressE1())
print 'ETH 1 IP addr. : ' + str(grvEpic.IPAddressE1())
print 'FirmwareVersion: ' + str(grvEpic.FirmwareVersion())
print 'UnitDescription: ' + str(grvEpic.UnitDescription())
print 'LastError : ' + str(grvEpic.LastError())
print 'digPt mod0 ch5= ' + str(grvEpic.GetDigitalPointState(0, 5))
print 'analog mod2 ch0= ' + str(grvEpic.GetAnalogPointValue(2, 0))
print 'analog min val = ' + str(grvEpic.GetAnalogPointMin(2, 0))
print 'analog max val = ' + str(grvEpic.GetAnalogPointMax(2, 0))
#print 'Raw read result: ' + str(grvEpic.ReadRawOffset(offset, size, data_type)) + '\n'
grvEpic.close()
end = timer()
print '\ntime elapsed = ' + str(end-start) + 's\n'
and a screenshot of what its output looks like:
Happy coding!