
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/client/4_messages.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_gallery_client_4_messages.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_gallery_client_4_messages.py:


Messages
========

Illustrate how a client receives an Aseba message.

.. GENERATED FROM PYTHON SOURCE LINES 7-14

.. code-block:: Python


    from pyaseba.client import Client
    from pyaseba.client.msgs import GetVariables, Variables

    client = Client()
    connection = client.connect("tcp", port=33333)








.. GENERATED FROM PYTHON SOURCE LINES 15-16

Receiving messages blocks until the next message is received.

.. GENERATED FROM PYTHON SOURCE LINES 16-20

.. code-block:: Python


    msg, connection = client.get_message()
    print(f"Got message {msg} from network #{connection}")





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    Got message NodePresent(source=0, version=9) from network #1




.. GENERATED FROM PYTHON SOURCE LINES 21-24

Sending messages is (almost) instantaneous instead.
Let us send a (manual) request for some variables
and wait until we get a response.

.. GENERATED FROM PYTHON SOURCE LINES 24-36

.. code-block:: Python


    client.send_message(GetVariables(dest=0, start=0, length=10))
    for _ in range(5):
        msg, connection = client.get_message()
        if isinstance(msg, Variables):
            print(f"Got response {msg}")
            break
        else:
            print(f"Ignoring {msg}")


    client.close()




.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    Ignoring Description(source=0, name='SimpleNode', protocol_version=9, variables=[('', 0), ('', 0), ('', 0), ('', 0), ('', 0), ('', 0)], events=[('', '')], functions=[('', '', []), ('', '', [])])
    Ignoring NamedVariableDescription(source=0, name='id', size=1)
    Ignoring NamedVariableDescription(source=0, name='source', size=1)
    Ignoring NamedVariableDescription(source=0, name='args', size=32)
    Ignoring NamedVariableDescription(source=0, name='_productId', size=1)





.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 1.010 seconds)


.. _sphx_glr_download_gallery_client_4_messages.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: 4_messages.ipynb <4_messages.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: 4_messages.py <4_messages.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: 4_messages.zip <4_messages.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
