Go back to Getting Started with OptoMMP for Python
Python Package Overview
To make using OptoMMP with Python more straightforward and easy to get started with for groov EPIC there is an open source Python Package available on the Python Package Index (PyPI) with files hosted on GitHub.
What this means is that you can easily download the package, import it with one line in your Python script, and then each task is just a simple function call while the complex hexadecimal arrays, sockets, and other details are handled for you.
The idea is to hit the ground running and go straight into developing applications for EPIC, rather than spending time writing and debugging the communication method.
Downloading
To download the package you will need to install Python-pip, the Python package manager (if you don’t already have it), and then use that to get the optommp
package from the Python Package Index (PyPI).
Follow these steps to get the package on your device:
-
sudo apt-get update
-
sudo apt-get install python-pip
-
sudo pip install optommp
Using the Package
To use the package see either the GitHub readme or the following steps:
-
After downloading the package, import it at the top of your script:
import optommp
-
Create an Opto 22 Memory-mapped object for either localhost (no parameter) or provide the hostname of the device:
grvEpic = optommp.O22MMP()
-
Call functions on this object, handing in any required parameters; for example to toggle the digital output module in slot 0, channel number 4 to on (1 = on, 0 = off):
grvEpic.SetDigitalPointState(0, 4, 1)
-
At the end of the script close the controller connection:
grvEpic.close()
There are more sample scripts available to see the package in use, or you can make a post to the Opto forum for more specific questions.
Included Functions
Create and Close Controller Object
-
O22MMP(host) - Initialize an Opto22 Memory Mapped object residing at ‘host’ IP address or hostname.
-
O22MMP() - Initialize object, default to
localhost
/127.0.0.1
. -
close() - Closes the socket connection to the device. Call this before the end of the script.
Misc. Functions
-
ReadRawOffset(offset, size, data_type) - Rads the raw address at
offset
, collectingsize
bytes, and usingdata_type
formatting to unpack it (see UnpackReadResponse for details). -
LastError() - Returns the last error response code stored on the controller, refer to the OptoMMP Protocol Manual (form 1465) for details.
-
UnitDescription() - Returns the device unit description. For example,
GRV-EPIC-PR1
. -
FirmwareVersion() - Returns the device firmware version. For example,
R1.3a
.
Ethernet Status Access Functions
-
IPAddressE0() - Returns the IP address associated with Ethernet 0 on the controller.
-
MACAddressE0() - Returns the MAC address associated with Ethernet 0 on the controller.
-
IPAddressE1() - Returns the IP address associated with Ethernet 1 on the controller.
-
MACAddressE1() - Returns the MAC address associated with Ethernet 1 on the controller.
Analog & Digital I/O Access Functions
-
SetDigitalPointState(module, channel, state) - The HD digital output at
channel
onmodule
will be toggled tostate
, which should be either 1 or 0. Returns status code, where code 0 means there was no error reported. -
GetDigitalPointState(module, channel) - The state of the HD digital output at
channel
onmodule
will be fetched. Returns state either 1 or 0. -
GetAnalogPointValue(module, channel) - Return the current float value of the analog I/O installed at
channel
onmodule
. -
SetAnalogPointValue(module, channel, value) - Set the analog I/O installed at
channel
onmodule
to bevalue
.value
should be a float. -
GetAnalogPointMin(module, channel) - Return the minimum float value of the analog I/O installed at
channel
onmodule
. -
GetAnalogPointMax(module, channel) - Return the maximum float value of the analog I/O installed at
channel
onmodule
.
ScratchPad Area Access Functions
-
GetScractchPadIntegerArea(index) - Returns the
index
th scratch pad integer. -
SetScractchPadIntegerArea(index, value) - Sets the
index
th scratch pad integer to bevalue
. -
GetScractchPadFloatArea(index) - Returns the
index
th scratch pad float. -
SetScractchPadFloatArea(index, value) - Sets the
index
th scratch pad float to bevalue
. -
GetScractchPadStringArea(index) - Returns the
index
th scratch pad string. -
SetScractchPadStringArea(index, data) - Sets the
index
th scratch pad string to bedata
.
Internal Memory-Map Functions
Most users will not want or need to use these functions directly in their code, they are mostly used as helpers for the functions above. However, understanding what they do may be helpful while debugging, or if you intend to modify or expand upon this package.
-
UnpackReadResponse(data, data_type) - Unpacks the string data from bytes 16-20 of a read response. Returns formatted data.
data_type
–> struct format characters ‘c’, ‘i’, ‘d’, ‘f’, etc., or specifically ‘FIRMWARE’, ‘IP’, or ‘MAC’ for custom formatting, or ‘NONE’ for raw binary data. -
UnpackWriteResponse(data) - Unpacks the integer status code from bytes 4-8 of a write response. Returns int status, where code 0 means there was no error reported.
-
PackFloat(value) - Packs floating point
vlaue
into a four-byte hexidecimal array. -
PackInteger(value) - Packs integer point
vlaue
into a four-byte hexidecimal array. -
ReadBlock(address) - Read value at memory location
address
. Relies onBuidReadBlockRequest()
, wraps up.send()
and.recv()
methods. Returns unpacked string data. -
WriteBlock(address, value) - Write
value
into memory locationaddress
. Relies onBuildWriteBlockRequest()
, wraps up.send()
and.recv()
methods. Returns int status. -
BuildReadBlockRequest(dest, size) - Build the read block request bytearray. Returns bytearray block.
This is an internally used utility method to build a read request. Client code isn’t likely to need it. -
BuildWriteBlockRequest(dest, value) - Build the write block request bytearray. Returns bytearray block.
This is an internally used utility method to build a read request. Client code isn’t likely to need it.
To understand the methods used to write this package, check out the earlier pages in this Getting Started with OptoMMP for Python guide.