..
 #############################################################
 # This file was automatically generated on 2015-05-19.      #
 #                                                           #
 # If you have a bugfix for this file and want to commit it, #
 # please fix the bug in the generator. You can find a link  #
 # to the generators git repository on tinkerforge.com       #
 #############################################################

:orphan:
:breadcrumbs: <a href="../../index.html">Home</a> / <a href="../../index.html#software-delphi">Software</a> / Delphi/Lazarus - RS232 Bricklet

.. |device_identifier_constant| replace:: There is also a :ref:`constant <rs232_bricklet_delphi_constants>` for the device identifier of this Bricklet.

.. _rs232_bricklet_delphi:

Delphi/Lazarus - RS232 Bricklet
===============================

.. note::
 This Bricklet is currently in the prototype stage and the software/hardware
 as well as the documentation is in an incomplete state.


This is the description of the :ref:`Delphi/Lazarus API bindings <api_bindings_delphi>` for the :ref:`RS232 Bricklet <rs232_bricklet>`. General information and technical
specifications for the RS232 Bricklet are summarized in its :ref:`hardware description
<rs232_bricklet_description>`.

An :ref:`installation guide <api_bindings_delphi_install>` for the Delphi/Lazarus API
bindings is part of their general description.

.. _rs232_bricklet_delphi_examples:


Examples
--------

The example code below is `Public Domain (CC0 1.0)
<http://creativecommons.org/publicdomain/zero/1.0/>`__.

Loopback
^^^^^^^^

`Download (ExampleLoopback.pas) <https://github.com/Tinkerforge/rs232-bricklet/raw/master/software/examples/delphi/ExampleLoopback.pas>`__

.. literalinclude:: RS232_Bricklet_Delphi_ExampleLoopback.pas
 :language: delphi
 :linenos:
 :tab-width: 4

.. _rs232_bricklet_delphi_api:

API
---

Since Delphi does not support multiple return values directly, we use the
``out`` keyword to return multiple values from a function.

All functions and procedures listed below are thread-safe.




Basic Functions
^^^^^^^^^^^^^^^


.. delphi:function:: constructor TBrickletRS232.Create(const uid: string; ipcon: TIPConnection)

 Creates an object with the unique device ID ``uid``:

 .. code-block:: delphi

    rs232 := TBrickletRS232.Create('YOUR_DEVICE_UID', ipcon);

 This object can then be used after the IP Connection is connected
 (see examples :ref:`above <rs232_bricklet_delphi_examples>`).


.. delphi:function:: function TBrickletRS232.Write(const message: array [0..59] of char; const length: byte): byte

 Writes a string of up to 60 characters to the RS232 interface. The string
 can be binary data, ASCII or similar is not necessary.
 
 The length of the string has to be given as an additional parameter.
 
 The return value is the number of bytes that could be written.
 
 See :func:`SetConfigurations` for configuration possibilities
 regarding baudrate, parity and so on.
 
.. delphi:function:: procedure TBrickletRS232.Read(out message: array [0..59] of char; out length: byte)

 Returns the currently buffered message. The maximum length
 of message is 60. If the length is given as 0, there was no
 new data available.
 
 Instead of polling with this function, you can also use
 callbacks. See :delphi:func:`EnableCallback <TBrickletRS232.EnableCallback>` and :delphi:func:`OnReadCallback <TBrickletRS232.OnReadCallback>`.
 
.. delphi:function:: procedure TBrickletRS232.SetConfiguration(const baudrate: byte; const parity: byte; const stopbits: byte; const wordlength: byte; const hardwareFlowcontrol: byte; const softwareFlowcontrol: byte)

 Sets the configuration for the RS232 communication. Available options:
 
 * Baudrate between 300 and 230400 baud.
 * Parity of none, odd, even or forced parity.
 * Stopbits can be 1 or 2.
 * Word length of 5 to 8.
 * Hard-/Software flow control can each be on or off.
 
 The default is: 115200 baud, parity none, 1 stop bit, word length 8, hard-/software flow control off.
 
 The following constants are available for this function:
 
 * BRICKLET_RS232_BAUDRATE_300 = 0
 * BRICKLET_RS232_BAUDRATE_600 = 1
 * BRICKLET_RS232_BAUDRATE_1200 = 2
 * BRICKLET_RS232_BAUDRATE_2400 = 3
 * BRICKLET_RS232_BAUDRATE_4800 = 4
 * BRICKLET_RS232_BAUDRATE_9600 = 5
 * BRICKLET_RS232_BAUDRATE_14400 = 6
 * BRICKLET_RS232_BAUDRATE_28800 = 7
 * BRICKLET_RS232_BAUDRATE_38400 = 8
 * BRICKLET_RS232_BAUDRATE_57600 = 9
 * BRICKLET_RS232_BAUDRATE_115200 = 10
 * BRICKLET_RS232_BAUDRATE_230400 = 11
 * BRICKLET_RS232_PARITY_NONE = 0
 * BRICKLET_RS232_PARITY_ODD = 1
 * BRICKLET_RS232_PARITY_EVEN = 2
 * BRICKLET_RS232_PARITY_FORCED_PARITY_1 = 3
 * BRICKLET_RS232_PARITY_FORCED_PARITY_0 = 4
 * BRICKLET_RS232_STOPBITS_1 = 1
 * BRICKLET_RS232_STOPBITS_2 = 2
 * BRICKLET_RS232_WORDLENGTH_5 = 5
 * BRICKLET_RS232_WORDLENGTH_6 = 6
 * BRICKLET_RS232_WORDLENGTH_7 = 7
 * BRICKLET_RS232_WORDLENGTH_8 = 8
 * BRICKLET_RS232_HARDWARE_FLOWCONTROL_OFF = 0
 * BRICKLET_RS232_HARDWARE_FLOWCONTROL_ON = 1
 * BRICKLET_RS232_SOFTWARE_FLOWCONTROL_OFF = 0
 * BRICKLET_RS232_SOFTWARE_FLOWCONTROL_ON = 1
 
.. delphi:function:: procedure TBrickletRS232.GetConfiguration(out baudrate: byte; out parity: byte; out stopbits: byte; out wordlength: byte; out hardwareFlowcontrol: byte; out softwareFlowcontrol: byte)

 Returns the configuration as set by :delphi:func:`SetConfiguration <TBrickletRS232.SetConfiguration>`.
 
 The following constants are available for this function:
 
 * BRICKLET_RS232_BAUDRATE_300 = 0
 * BRICKLET_RS232_BAUDRATE_600 = 1
 * BRICKLET_RS232_BAUDRATE_1200 = 2
 * BRICKLET_RS232_BAUDRATE_2400 = 3
 * BRICKLET_RS232_BAUDRATE_4800 = 4
 * BRICKLET_RS232_BAUDRATE_9600 = 5
 * BRICKLET_RS232_BAUDRATE_14400 = 6
 * BRICKLET_RS232_BAUDRATE_28800 = 7
 * BRICKLET_RS232_BAUDRATE_38400 = 8
 * BRICKLET_RS232_BAUDRATE_57600 = 9
 * BRICKLET_RS232_BAUDRATE_115200 = 10
 * BRICKLET_RS232_BAUDRATE_230400 = 11
 * BRICKLET_RS232_PARITY_NONE = 0
 * BRICKLET_RS232_PARITY_ODD = 1
 * BRICKLET_RS232_PARITY_EVEN = 2
 * BRICKLET_RS232_PARITY_FORCED_PARITY_1 = 3
 * BRICKLET_RS232_PARITY_FORCED_PARITY_0 = 4
 * BRICKLET_RS232_STOPBITS_1 = 1
 * BRICKLET_RS232_STOPBITS_2 = 2
 * BRICKLET_RS232_WORDLENGTH_5 = 5
 * BRICKLET_RS232_WORDLENGTH_6 = 6
 * BRICKLET_RS232_WORDLENGTH_7 = 7
 * BRICKLET_RS232_WORDLENGTH_8 = 8
 * BRICKLET_RS232_HARDWARE_FLOWCONTROL_OFF = 0
 * BRICKLET_RS232_HARDWARE_FLOWCONTROL_ON = 1
 * BRICKLET_RS232_SOFTWARE_FLOWCONTROL_OFF = 0
 * BRICKLET_RS232_SOFTWARE_FLOWCONTROL_ON = 1
 


Advanced Functions
^^^^^^^^^^^^^^^^^^

.. delphi:function:: function TBrickletRS232.GetAPIVersion(): array [0..2] of byte

 Returns the version of the API definition (major, minor, revision) implemented
 by this API bindings. This is neither the release version of this API bindings
 nor does it tell you anything about the represented Brick or Bricklet.
 
.. delphi:function:: function TBrickletRS232.GetResponseExpected(const functionId: byte): boolean

 Returns the response expected flag for the function specified by the function
 ID parameter. It is *true* if the function is expected to send a response,
 *false* otherwise.
 
 For getter functions this is enabled by default and cannot be disabled,
 because those functions will always send a response. For callback configuration
 functions it is enabled by default too, but can be disabled by
 :delphi:func:`SetResponseExpected <TBrickletRS232.SetResponseExpected>`. For setter functions it is disabled by default
 and can be enabled.
 
 Enabling the response expected flag for a setter function allows to detect
 timeouts and other error conditions calls of this setter as well. The
 device will then send a response for this purpose. If this flag is disabled for
 a setter function then no response is send and errors are silently ignored,
 because they cannot be detected.
 
 See :delphi:func:`SetResponseExpected <TBrickletRS232.SetResponseExpected>`
 for the list of function ID constants available for this function.
 
.. delphi:function:: procedure TBrickletRS232.SetResponseExpected(const functionId: byte; const responseExpected: boolean)

 Changes the response expected flag of the function specified by the
 function ID parameter. This flag can only be changed for setter (default value:
 *false*) and callback configuration functions (default value: *true*). For
 getter functions it is always enabled and callbacks it is always disabled.
 
 Enabling the response expected flag for a setter function allows to detect
 timeouts and other error conditions calls of this setter as well. The
 device will then send a response for this purpose. If this flag is disabled for
 a setter function then no response is send and errors are silently ignored,
 because they cannot be detected.
 
 The following function ID constants are available for this function:
 
 * BRICKLET_RS232_FUNCTION_ENABLE_CALLBACK = 3
 * BRICKLET_RS232_FUNCTION_DISABLE_CALLBACK = 4
 * BRICKLET_RS232_FUNCTION_SET_CONFIGURATION = 6
 
.. delphi:function:: procedure TBrickletRS232.SetResponseExpectedAll(const responseExpected: boolean)

 Changes the response expected flag for all setter and callback configuration
 functions of this device at once.
 
.. delphi:function:: procedure TBrickletRS232.GetIdentity(out uid: string; out connectedUid: string; out position: char; out hardwareVersion: array [0..2] of byte; out firmwareVersion: array [0..2] of byte; out deviceIdentifier: word)

 Returns the UID, the UID where the Bricklet is connected to, 
 the position, the hardware and firmware version as well as the
 device identifier.
 
 The position can be 'a', 'b', 'c' or 'd'.
 
 The device identifier numbers can be found :ref:`here <device_identifier>`.
 |device_identifier_constant|
 


Callback Configuration Functions
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. delphi:function:: procedure TBrickletRS232.EnableCallback()

 Enables the :delphi:func:`OnReadCallback <TBrickletRS232.OnReadCallback>`.
 
 By default the callback is disabled.
 
.. delphi:function:: procedure TBrickletRS232.DisableCallback()

 Disables the :delphi:func:`OnReadCallback <TBrickletRS232.OnReadCallback>`.
 
 By default the callback is disabled.
 
.. delphi:function:: function TBrickletRS232.IsCallbackEnabled(): boolean

 Returns *true* if the :delphi:func:`OnReadCallback <TBrickletRS232.OnReadCallback>` is enabled,
 *false* otherwise.
 




.. _rs232_bricklet_delphi_callbacks:

Callbacks
^^^^^^^^^

Callbacks can be registered to receive time critical or recurring data from
the device. The registration is done by assigning a procedure to an callback
property of the device object:

 .. code-block:: delphi

  procedure TExample.MyCallback(sender: TBrickletRS232; const param: word);
  begin
    WriteLn(param);
  end;

  rs232.OnExample := {$ifdef FPC}@{$endif}example.MyCallback;

The available callback property and their type of parameters are described below.

.. note::
 Using callbacks for recurring events is *always* preferred
 compared to using getters. It will use less USB bandwidth and the latency
 will be a lot better, since there is no round trip time.

.. delphi:function:: property TBrickletRS232.OnReadCallback

 .. code-block:: delphi

  procedure(sender: TBrickletRS232; const message: array [0..59] of char; const length: byte) of object;


 This callback is called if new data is available. The message has
 a maximum size of 60 characters. The actual length of the message
 is given in addition.
 
 To enable this callback, use :delphi:func:`EnableCallback <TBrickletRS232.EnableCallback>`.
 


.. _rs232_bricklet_delphi_constants:

Constants
^^^^^^^^^

.. delphi:function:: const BRICKLET_RS232_DEVICE_IDENTIFIER

 This constant is used to identify a RS232 Bricklet.

 The :delphi:func:`GetIdentity <TBrickletRS232.GetIdentity>` function and the
 :delphi:func:`OnEnumerate <TIPConnection.OnEnumerate>`
 callback of the IP Connection have a ``deviceIdentifier`` parameter to specify
 the Brick's or Bricklet's type.

