dm_tty Module

Serial port access (TTY/PTY) on Unix.


Uses

  • module~~dm_tty~~UsesGraph module~dm_tty dm_tty iso_c_binding iso_c_binding module~dm_tty->iso_c_binding module~dm_error dm_error module~dm_tty->module~dm_error module~dm_file dm_file module~dm_tty->module~dm_file module~dm_kind dm_kind module~dm_tty->module~dm_kind module~dm_string dm_string module~dm_tty->module~dm_string module~dm_error->module~dm_kind module~dm_ascii dm_ascii module~dm_error->module~dm_ascii module~dm_file->module~dm_error module~dm_file->module~dm_kind iso_fortran_env iso_fortran_env module~dm_kind->iso_fortran_env module~dm_string->module~dm_error module~dm_string->module~dm_kind

Used by

  • module~~dm_tty~~UsedByGraph module~dm_tty dm_tty module~dm_geocom dm_geocom module~dm_geocom->module~dm_tty module~dmpack dmpack module~dmpack->module~dm_tty module~dmpack->module~dm_geocom proc~dm_modbus_create_rtu dm_modbus_create_rtu proc~dm_modbus_create_rtu->module~dm_tty

Variables

Type Visibility Attributes Name Initial
integer, public, parameter :: TTY_RDONLY = 1

Read-only.

integer, public, parameter :: TTY_WRONLY = 2

Write-only.

integer, public, parameter :: TTY_RDWR = 3

Read/write.

integer, public, parameter :: TTY_B0 = 0
integer, public, parameter :: TTY_B50 = 50
integer, public, parameter :: TTY_B75 = 75
integer, public, parameter :: TTY_B110 = 110
integer, public, parameter :: TTY_B134 = 134
integer, public, parameter :: TTY_B150 = 150
integer, public, parameter :: TTY_B200 = 200
integer, public, parameter :: TTY_B300 = 300
integer, public, parameter :: TTY_B600 = 600
integer, public, parameter :: TTY_B1200 = 1200
integer, public, parameter :: TTY_B1800 = 1800
integer, public, parameter :: TTY_B2400 = 2400
integer, public, parameter :: TTY_B4800 = 4800
integer, public, parameter :: TTY_B9600 = 9600
integer, public, parameter :: TTY_B19200 = 19200
integer, public, parameter :: TTY_B38400 = 38400
integer, public, parameter :: TTY_B57600 = 57600
integer, public, parameter :: TTY_B115200 = 115200
integer, public, parameter :: TTY_B230400 = 230400
integer, public, parameter :: TTY_B460800 = 460800
integer, public, parameter :: TTY_B921600 = 921600
integer, public, parameter :: TTY_PARITY_NAME_LEN = 4

Parity string length.

integer, public, parameter :: TTY_PARITY_NONE = 1

No parity.

integer, public, parameter :: TTY_PARITY_EVEN = 2

Even parity.

integer, public, parameter :: TTY_PARITY_ODD = 3

Odd parity.

integer, public, parameter :: TTY_BYTE_SIZE5 = 1

5 bits.

integer, public, parameter :: TTY_BYTE_SIZE6 = 2

6 bits.

integer, public, parameter :: TTY_BYTE_SIZE7 = 3

7 bits.

integer, public, parameter :: TTY_BYTE_SIZE8 = 4

8 bits.

integer, public, parameter :: TTY_STOP_BITS1 = 1

1 stop bit.

integer, public, parameter :: TTY_STOP_BITS2 = 2

2 stop bits.


Interfaces

public interface dm_tty_read

Generic TTY read function.

  • public function dm_tty_read_bytes(tty, bytes, del, nbytes) result(rc)

    Reads from TTY into buf until delimiter del occurs. The number of bytes read is returned in n.

    The function returns the following error codes:

    • E_BOUNDS if end of buffer is reached.
    • E_READ if the read operation failed.

    Arguments

    Type IntentOptional Attributes Name
    type(tty_type), intent(inout) :: tty

    TTY type.

    character(len=*), intent(inout) :: bytes

    Input buffer.

    character(len=*), intent(in) :: del

    Delimiter.

    integer(kind=i8), intent(out), optional :: nbytes

    Number of bytes read.

    Return Value integer

  • public function dm_tty_read_request(tty, request) result(rc)

    Reads TTY response into request. The request delimiter is unescaped. The response is escaped before being stored in the request.

    The function returns the following error codes:

    • E_BOUNDS if the response is longer than REQUEST_RESPONSE_LEN.
    • E_READ if reading from TTY failed.

    Arguments

    Type IntentOptional Attributes Name
    type(tty_type), intent(inout) :: tty

    TTY type.

    type(request_type), intent(inout) :: request

    Request type.

    Return Value integer

public interface dm_tty_write

Generic TTY write function.

  • public function dm_tty_write_bytes(tty, bytes, nbytes) result(rc)

    Writes given string to TTY. Returns E_WRITE on error. The function may cause an access violation if nbytes is greater than the length of bytes. Returns E_WRITE on error.

    Arguments

    Type IntentOptional Attributes Name
    type(tty_type), intent(inout) :: tty

    TTY type.

    character(len=*), intent(in), target :: bytes

    Bytes to send.

    integer, intent(in), optional :: nbytes

    Number of bytes to send.

    Return Value integer

  • public function dm_tty_write_request(tty, request) result(rc)

    Writes given request to TTY. The function unescapes the request string. The function returns E_WRITE on error.

    Arguments

    Type IntentOptional Attributes Name
    type(tty_type), intent(inout) :: tty

    TTY type.

    type(request_type), intent(inout) :: request

    Request type

    Return Value integer


Derived Types

type, public ::  tty_type

TTY/PTY data type that stores serial port settings (default: 9600 baud, 8N1).

Components

Type Visibility Attributes Name Initial
character(len=FILE_PATH_LEN), public :: path = ' '

TTY/PTY path.

integer, public :: access = TTY_RDWR

Access mode (read/write).

integer, public :: baud_rate = TTY_B9600

Baud rate (9600).

integer, public :: byte_size = TTY_BYTE_SIZE8

Byte size (8).

integer, public :: stop_bits = TTY_STOP_BITS1

Stop bits (1).

integer, public :: parity = TTY_PARITY_NONE

Parity (none).

integer, public :: timeout = 5

Read timeout in seconds.

logical, public :: dtr = .false.

Data Terminal Ready.

logical, public :: rts = .false.

Request To Send.

logical, public :: blocking = .true.

Blocking read.


Functions

public function dm_tty_baud_rate_from_value(value, error) result(baud_rate)

Returns baud rate enumerator from numeric value. If the value is invalid, returns 0 by default and sets optional argument error to E_INVALID.

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: value

Numeric baud rate value.

integer, intent(out), optional :: error

Error code.

Return Value integer

public function dm_tty_byte_size_from_value(value, error) result(byte_size)

Returns byte size enumerator from numeric value. If the value is invalid, returns 0 by default and sets optional argument error to E_INVALID.

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: value

Numeric byte size value.

integer, intent(out), optional :: error

Error code.

Return Value integer

public function dm_tty_flush(tty, input, output) result(rc)

Flushes TTY input and output buffer. Returns E_INVALID if the passed tty type is invalid, or E_SYSTEM if the system call failed.

Arguments

Type IntentOptional Attributes Name
type(tty_type), intent(inout) :: tty

TTY type.

logical, intent(in), optional :: input

Flush input buffer.

logical, intent(in), optional :: output

Flush output buffer.

Return Value integer

public function dm_tty_is_connected(tty) result(connected)

Return .true. if TTY is connected, else .false..

Arguments

Type IntentOptional Attributes Name
type(tty_type), intent(inout) :: tty

TTY type.

Return Value logical

public pure elemental function dm_tty_is_valid_baud_rate(baud_rate) result(valid)

Returns .true. if given baud rate value is valid, else .false..

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: baud_rate

Baud rate.

Return Value logical

public pure elemental function dm_tty_is_valid_byte_size(byte_size) result(valid)

Returns .true. if given byte size value is valid, else .false..

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: byte_size

Byte size.

Return Value logical

public pure elemental function dm_tty_is_valid_parity(parity) result(valid)

Returns .true. if given parity value is valid, else .false..

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: parity

Parity.

Return Value logical

public pure elemental function dm_tty_is_valid_stop_bits(stop_bits) result(valid)

Returns .true. if given stop bits value is valid, else .false..

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: stop_bits

Stop bits.

Return Value logical

public pure elemental function dm_tty_is_valid_timeout(timeout) result(valid)

Returns .true. if given timeout value is valid, else .false..

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: timeout

Timeout.

Return Value logical

public function dm_tty_open(tty, path, baud_rate, byte_size, parity, stop_bits) result(rc)

Opens TTY/PTS device in set access mode and applies serial port attributes. The arguments baud_rate, byte_size, parity, and stop_bits must be valid enumerators.

Read more…

Arguments

Type IntentOptional Attributes Name
type(tty_type), intent(inout) :: tty

TTY type.

character(len=*), intent(in), optional :: path

Device path.

integer, intent(in), optional :: baud_rate

Baud rate enumerator (TTY_B*).

integer, intent(in), optional :: byte_size

Byte size enumerator (TTY_BYTE_SIZE*).

integer, intent(in), optional :: parity

Parity enumerator (TTY_PARITY_*).

integer, intent(in), optional :: stop_bits

Stop bits enumerator (TTY_STOP_BITS*).

Return Value integer

public function dm_tty_parity_from_name(name, error) result(parity)

Returns parity from character string (none, even, odd). If the parity is not recognised, returns 0 by default and sets optional argument error to E_INVALID.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: name

Parity name.

integer, intent(out), optional :: error

Error code.

Return Value integer

public function dm_tty_read_byte(tty, byte) result(rc)

Reads single byte from file descriptor.

Arguments

Type IntentOptional Attributes Name
type(tty_type), intent(inout) :: tty

TTY type.

character(len=1), intent(out), target :: byte

Byte read.

Return Value integer

public function dm_tty_read_bytes(tty, bytes, del, nbytes) result(rc)

Reads from TTY into buf until delimiter del occurs. The number of bytes read is returned in n.

Read more…

Arguments

Type IntentOptional Attributes Name
type(tty_type), intent(inout) :: tty

TTY type.

character(len=*), intent(inout) :: bytes

Input buffer.

character(len=*), intent(in) :: del

Delimiter.

integer(kind=i8), intent(out), optional :: nbytes

Number of bytes read.

Return Value integer

public function dm_tty_read_request(tty, request) result(rc)

Reads TTY response into request. The request delimiter is unescaped. The response is escaped before being stored in the request.

Read more…

Arguments

Type IntentOptional Attributes Name
type(tty_type), intent(inout) :: tty

TTY type.

type(request_type), intent(inout) :: request

Request type.

Return Value integer

public function dm_tty_set_attributes(tty) result(rc)

Sets terminal attributes.

Read more…

Arguments

Type IntentOptional Attributes Name
type(tty_type), intent(inout) :: tty

TTY type.

Return Value integer

public function dm_tty_set_blocking(tty, blocking) result(rc)

Sets TTY to blocking or non-blocking.

Read more…

Arguments

Type IntentOptional Attributes Name
type(tty_type), intent(inout) :: tty

TTY type.

logical, intent(in) :: blocking

Blocking mode.

Return Value integer

public function dm_tty_set_timeout(tty, timeout) result(rc)

Sets timeout of given TTY. A timeout of 0 results in blocking read without timeout. The minimum timeout is 0 seconds, the maximum is 25 seconds.

Read more…

Arguments

Type IntentOptional Attributes Name
type(tty_type), intent(inout) :: tty

TTY type.

integer, intent(in) :: timeout

Timeout in seconds.

Return Value integer

public function dm_tty_stop_bits_from_value(value, error) result(stop_bits)

Returns stop bits enumerator from numeric value. If the value is invalid, returns 0 by default and sets optional argument error to E_INVALID.

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: value

Numeric byte size value.

integer, intent(out), optional :: error

Error code.

Return Value integer

public function dm_tty_write_bytes(tty, bytes, nbytes) result(rc)

Writes given string to TTY. Returns E_WRITE on error. The function may cause an access violation if nbytes is greater than the length of bytes. Returns E_WRITE on error.

Arguments

Type IntentOptional Attributes Name
type(tty_type), intent(inout) :: tty

TTY type.

character(len=*), intent(in), target :: bytes

Bytes to send.

integer, intent(in), optional :: nbytes

Number of bytes to send.

Return Value integer

public function dm_tty_write_request(tty, request) result(rc)

Writes given request to TTY. The function unescapes the request string. The function returns E_WRITE on error.

Arguments

Type IntentOptional Attributes Name
type(tty_type), intent(inout) :: tty

TTY type.

type(request_type), intent(inout) :: request

Request type

Return Value integer


Subroutines

public subroutine dm_tty_close(tty)

Closes file descriptor.

Arguments

Type IntentOptional Attributes Name
type(tty_type), intent(inout) :: tty

TTY type.