|
| 1 | +.. _page_connections: |
| 2 | + |
| 3 | +Connections |
| 4 | +=========== |
| 5 | + |
| 6 | +Total stations and digital levels usually support a serial connection |
| 7 | +with RS232. Some instruments come with additional connection methods, like |
| 8 | +bluetooth as well. |
| 9 | + |
| 10 | +Serial Line |
| 11 | +----------- |
| 12 | + |
| 13 | +The RS232 serial line is the main method of connection. The relevant main |
| 14 | +primitive is the :class:`~geocompy.communication.SerialConnection` class, |
| 15 | +that acts as a wrapper around a :class:`~serial.Serial` object that |
| 16 | +implements the actual low level serial communication. |
| 17 | + |
| 18 | +.. code-block:: python |
| 19 | + :caption: Simple serial connection |
| 20 | + :linenos: |
| 21 | +
|
| 22 | + from serial import Serial |
| 23 | + from geocompy.communication import SerialConnection |
| 24 | +
|
| 25 | +
|
| 26 | + port = Serial("COM1", timeout=15) |
| 27 | + com = SerialConnection(port) |
| 28 | + com.send("some message") |
| 29 | + com.close() # Closes the wrapped serial port |
| 30 | +
|
| 31 | +.. caution:: |
| 32 | + :class: warning |
| 33 | + |
| 34 | + It is strongly recommended to set a ``timeout`` on the connection. Without |
| 35 | + a ``timeout`` set, the connection may end up in a perpetual waiting state |
| 36 | + if the instrument becomes unresponsive. A too small value however might |
| 37 | + result in premature timeout issues when using slow commands (e.g. |
| 38 | + motorized functions, measurements). |
| 39 | + |
| 40 | +The :class:`~geocompy.communication.SerialConnection` can also be used as a |
| 41 | +context manager, that automatically closes the serial port when the context |
| 42 | +is left. |
| 43 | + |
| 44 | +.. code-block:: python |
| 45 | + :caption: Serial connection as context manager |
| 46 | + :linenos: |
| 47 | +
|
| 48 | + from serial import Serial |
| 49 | + from geocompy.communication import SerialConnection |
| 50 | +
|
| 51 | +
|
| 52 | + port = Serial("COM1", timeout=15) |
| 53 | + with SerialConnection(port) as com: |
| 54 | + com.send("some message") |
| 55 | +
|
| 56 | +To make the connection creation simpler, a utility function is also included |
| 57 | +that can be used similarly to the :func:`open` function of the standard |
| 58 | +library. |
| 59 | + |
| 60 | +.. code-block:: python |
| 61 | + :caption: Creating connection with the utility function |
| 62 | + :linenos: |
| 63 | +
|
| 64 | + from geocompy.communication import open_serial |
| 65 | +
|
| 66 | +
|
| 67 | + with open_serial("COM1", timeout=15) as com: |
| 68 | + com.send("some message") |
| 69 | +
|
| 70 | +If a time consuming request has to be executed (that might exceed the normal |
| 71 | +connection timeout), it is possible to run it with a temporary override. |
| 72 | + |
| 73 | +.. code-block:: python |
| 74 | + :caption: Timeout override for slow requests |
| 75 | + :linenos: |
| 76 | +
|
| 77 | + from geocompy.communication import open_serial |
| 78 | +
|
| 79 | +
|
| 80 | + with open_serial("COM1", timeout=5) as com: |
| 81 | + ans = com.exchage("message") |
| 82 | + # normal operation |
| 83 | +
|
| 84 | + # request that might time out |
| 85 | + with com.timeout_override(20): |
| 86 | + ans = com.exchange("blocking message") |
| 87 | + |
| 88 | + # resumed normal operation |
| 89 | +
|
| 90 | +Bluetooth |
| 91 | +--------- |
| 92 | +
|
| 93 | +Newer instruments (particularly robotic total stations) might come with |
| 94 | +built-in or attachable bluetooth connection capabilities (e.g. Leica TS15 |
| 95 | +with radio handle). These instruments communicate over Serial Port Profile |
| 96 | +Bluetooth Classic, that emulates a direct line serial connection. |
| 97 | + |
| 98 | +.. note:: |
| 99 | + |
| 100 | + In case of Leica instruments and GeoCom, the GeoCom interface on the |
| 101 | + instrument might have to be manually switched to the bluetooth device, |
| 102 | + before initiating a connection. Make sure to sync the port parameters |
| 103 | + (e.g. speed, parity) between the instrument and the computer! |
| 104 | + |
| 105 | +To initiate a connection like this, the instrument first has to be paired |
| 106 | +to the controlling computer, and the bluetooth address of the instrument |
| 107 | +must be bound to an RFCOMM port as well. |
| 108 | + |
| 109 | +On windows machines this can be done manually through the Devices and |
| 110 | +Printers in the Control Panel. These RFCOMM devices will typically get one |
| 111 | +of the higher numbered ports, like ``COM9``. |
| 112 | + |
| 113 | +Linux systems will typically use something like |
| 114 | +`bluetoothctl <https://documentation.ubuntu.com/core/explanation/system-snaps/bluetooth/pairing/index.html>`_ |
| 115 | +to handle the pairing process, and then ``rfcomm`` command to bind a device |
| 116 | +to an RFCOMM port. |
| 117 | + |
| 118 | +Once the pairing and binding is complete, the connection over bluetooth can |
| 119 | +be opened just like a normal serial line. |
| 120 | + |
| 121 | +.. code-block:: python |
| 122 | + :caption: Opening connection through an RFCOMM port |
| 123 | + :linenos: |
| 124 | +
|
| 125 | + from geocompy.communication import open_serial |
| 126 | +
|
| 127 | +
|
| 128 | + with open_serial("COM9") as com: |
| 129 | + com.send("some message") |
| 130 | +
|
| 131 | +.. seealso:: |
| 132 | + |
| 133 | + https://youtu.be/6Z4PXct8Rg0?si=db53q6F6NRi2M4BF |
| 134 | + Video explaining the pairing process between a Raspberry PI and |
| 135 | + a windows PC. It shows how to properly add an RFCOMM device in |
| 136 | + the Control Panel. |
0 commit comments