meerstetter / pyMeCom

Python interface for the MeCom protocol by Meerstetter Engineering.
https://www.meerstetter.ch
MIT License
23 stars 24 forks source link

GPIO switching #20

Open Cdriko opened 10 months ago

Cdriko commented 10 months ago

Hello I try to switch GPIO of a Meerstetter TEC 1089 as in the attached file

from pyMeCom.mecom import MeCom, ResponseException, WrongChecksum
from serial import SerialException

# default queries from command table below
DEFAULT_QUERIES = [
    "loop status",
    "object temperature",
    "target object temperature",
    "output current",
    "output voltage"
]
>>
# syntax
# { display_name: [parameter_id, unit], }
COMMAND_TABLE = {
    "loop status": [1200, ""],
    "object temperature": [1000, "degC"],
    "target object temperature": [1010, "degC"],
    "output current": [1020, "A"],
    "output voltage": [1021, "V"],
    "sink temperature": [1001, "degC"],
    "ramp temperature": [1011, "degC"],
    "enable IO function": [52100, ""],
    "set output to Push-Pull" : [52101,""],
    "set IO output state": [52102,""],
    "get IO output state": [52103,""],
    "GPIO function": [6100,""]
}

'''
    {"id": 6101, "name": "GPIO Level Assignment", "format": "INT32"},
    {"id": 6102, "name": "GPIO Hardware Configuration", "format": "INT32"},
    {"id": 6103, "name": "GPIO Channel", "format": "INT32"},
    '''

class MeerstetterTEC(object):
    """
    Controlling TEC devices via serial.
    """

    def _tearDown(self):
        self.session().stop()

    def __init__(self, port="/dev/ttyUSB0", channel=1, queries=DEFAULT_QUERIES, *args, **kwars):
        assert channel in (1, 2)
        self.channel = channel
        self.port = port
        self.queries = queries
        self._session = None
        self._connect()

    def _connect(self):
        # open session
        self._session = MeCom(serialport=self.port)
        # get device address
        self.address = self._session.identify()

    def session(self):
        if self._session is None:
            self._connect()
        return self._session

    def get_data(self):
        data = {}
        for description in self.queries:
            id, unit = COMMAND_TABLE[description]
            try:
                value = self.session().get_parameter(parameter_id=id, address=self.address, parameter_instance=self.channel)
                data.update({description: (value, unit)})
            except (ResponseException, WrongChecksum) as ex:
                self.session().stop()
                self._session = None
        return data

if __name__ == '__main__':

    # initialize controller
    mc = MeerstetterTEC()

    '''

    from "TEC Controller Communication Protocol 5136AM.pdf"

    The pins are addressed by a bit field.
    Example:
    To configure GPIO3 / GPIO4 as output pins, and to set
    GPIO3 to high level and GPIO4 to Low Level, use the
    following commands:

    Set ID 52102 to 4 (Set Bit Number 2 to '1') set IO output state
    Set ID 52101 to 12 (Set Bit Numbers 2 and 3 to '1') set output to Push-Pull"
    Set ID 52100 to 1 (Enable the Function) enable IO function

    Bit Number Output Signal
    0           GPIO1     1
    1           GPIO 2    2
    2           GPIO 3    4
    3           GPIO 4    8
    4           GPIO 5    16
    5           GPIO 6    32

            "enable IO function": [52100, ""],
    "set output to Push-Pull" : [52101,""],
    "set IO output state": [52102,""],
    "get IO output state": [52103,""],
    '''

    #set "set IO output state" to 1 (GPIO1)
    id, unit = COMMAND_TABLE["set IO output state"] #[52102,""],

    print(mc.session().set_parameter(parameter_id=52102, value=1,address=mc.address))

    # set the "set output to Push-Pull" to 1
    id, unit = COMMAND_TABLE["set output to Push-Pull"]
    print(id)
    print(mc.session().set_parameter(parameter_id=id, value=1 ,address=mc.address))

    #"enable IO function"
    id, unit = COMMAND_TABLE["enable IO function"]
    print(id)
    print(mc.session().set_parameter(parameter_id=id, value=1 ,address=mc.address))

    #"get IO output state"
    id, unit = COMMAND_TABLE["get IO output state"]
    print(id)
    print(mc.session().get_parameter(parameter_id=id, address=mc.address))

So watever I change, the "get IO output state" response is zero, and I measure 0 V.

Does I do a mistake ? do you have an idea ?

Thanks

samirceka commented 2 weeks ago

In case you haven't resolved this issue in the meantime.

The GPIO Function parameter (ID 6100) must be set to "Data Interface". Furthermore, there is currently an issue with the Set Output to Push-Pull parameter which makes it work unreliably. This will be fixed in the next firmware release. I recommend explicitly setting the GPIO to push-pull using the GPIO Hardware Configuration (ID 6102) parameter instead.