<commit_before>"""
Base class for equipment that is connected through a Serial port.
"""
import re
import time
import serial

from msl.equipment import constants
from msl.equipment.connection_message_based import ConnectionMessageBased


class ConnectionSerial(ConnectionMessageBased):

    def __init__(self, record):
        """Base class for equipment that is connected through a Serial port.

        The :obj:`~msl.equipment.record_types.ConnectionRecord.properties`
        for a Serial connection supports the following key-value pairs in the
        :ref:`connection_database` (see also :class:`serial.Serial` for more details about each parameter)::

            'read_termination': str or None (e.g., "\\r\\n")
            'write_termination': str or None (e.g., "\\n")
            'read_size': int (the number of bytes to be read, must be > 0)
            'encoding': str (e.g., 'ascii')
            'baud_rate': int (e.g., 9600, 115200)
            'data_bits': int (e.g., 5, 6, 7, 8)
            'stop_bits': int or float (e.g., 1, 1.5, 2)
            'parity': str (e.g., none, even, odd, mark, space)
            'timeout': float or None (the read timeout value)
            'write_timeout': float or None (the write timeout value)
            'inter_byte_timeout': float or None (the inter-character timeout)
            'exclusive': bool (enable exclusive access mode, for POSIX only)
            'xon_xoff': bool (enable software flow control)
            'rts_cts': bool (enable hardware (RTS/CTS) flow control)
            'dsr_dtr': bool (enable hardware (DSR/DTR) flow control)

        The :data:`record.connection.backend <msl.equipment.record_types.ConnectionRecord.backend>`
        value must be equal to :data:`Backend.MSL <msl.equipment.constants.Backend.MSL>`
        to use this class for the communication system. This is achieved by setting the
        value in the **Backend** field for a connection record in the :ref:`connection_database`
        to be ``MSL``.

        Do not instantiate this class directly. Use the
        :obj:`record.connect() <.record_types.EquipmentRecord.connect>` method
        to connect to the equipment.

        Parameters
        ----------
        record : :class:`~.record_types.EquipmentRecord`
            A record from an :ref:`equipment_database`.

        Raises
        ------
        :exc:`~.exceptions.MSLConnectionError`
            If the Serial port cannot be opened.
        """
        ConnectionMessageBased.__init__(self, record)

        props = record.connection.properties

        self._serial = serial.Serial()

        self.read_termination = props.get('read_termination', self.read_termination)
        self.write_termination = props.get('write_termination', self.write_termination)
        self.max_read_size = props.get('read_size', self.max_read_size)
        self.encoding = props.get('encoding', self.encoding)
        self.timeout = props.get('timeout', None)

        # PyVISA and PyVISA-py accept various resource names: 'ASRL', 'COM', 'LPT', 'ASRLCOM'
        # but Serial seems to only be happy with 'COM'
        number = re.search('\d+', record.connection.address)  # get the port number from the address
        if number is None:
            self.raise_exception('No port number was specified -- address={}'.format(record.connection.address))
        self._serial.port = 'COM{}'.format(number.group(0))

        self._serial.parity = props.get('parity', constants.Parity.NONE).value
        self._serial.write_timeout = props.get('write_timeout', None)
        self._serial.inter_byte_timeout = props.get('inter_byte_timeout', None)
        self._serial.exclusive = props.get('exclusive', None)
        self._serial.xonxoff = props.get('xon_xoff', False)
        self._serial.rtscts = props.get('rts_cts', False)
        self._serial.dsrdtr = props.get('dsr_dtr', False)

        if 'baud_rate' in props:
            self._serial.baudrate = props['baud_rate']
        else:
            self._serial.baudrate = props.get('baudrate', 9600)

        if 'data_bits' in props:
            self._serial.bytesize = props['data_bits']<commit_msg>rename read_size to max_read_size in properties<commit_after>"""
Base class for equipment that is connected through a Serial port.
"""
import re
import time
import serial

from msl.equipment import constants
from msl.equipment.connection_message_based import ConnectionMessageBased


class ConnectionSerial(ConnectionMessageBased):

    def __init__(self, record):
        """Base class for equipment that is connected through a Serial port.

        The :obj:`~msl.equipment.record_types.ConnectionRecord.properties`
        for a Serial connection supports the following key-value pairs in the
        :ref:`connection_database` (see also :class:`serial.Serial` for more details about each parameter)::

            'read_termination': str or None (e.g., "\\r\\n")
            'write_termination': str or None (e.g., "\\n")
            'max_read_size': int (the maximum number of bytes to be read, must be > 0)
            'encoding': str (e.g., 'ascii')
            'baud_rate': int (e.g., 9600, 115200)
            'data_bits': int (e.g., 5, 6, 7, 8)
            'stop_bits': int or float (e.g., 1, 1.5, 2)
            'parity': str (e.g., none, even, odd, mark, space)
            'timeout': float or None (the read timeout value)
            'write_timeout': float or None (the write timeout value)
            'inter_byte_timeout': float or None (the inter-character timeout)
            'exclusive': bool (enable exclusive access mode, for POSIX only)
            'xon_xoff': bool (enable software flow control)
            'rts_cts': bool (enable hardware (RTS/CTS) flow control)
            'dsr_dtr': bool (enable hardware (DSR/DTR) flow control)

        The :data:`record.connection.backend <msl.equipment.record_types.ConnectionRecord.backend>`
        value must be equal to :data:`Backend.MSL <msl.equipment.constants.Backend.MSL>`
        to use this class for the communication system. This is achieved by setting the
        value in the **Backend** field for a connection record in the :ref:`connection_database`
        to be ``MSL``.

        Do not instantiate this class directly. Use the
        :obj:`record.connect() <.record_types.EquipmentRecord.connect>` method
        to connect to the equipment.

        Parameters
        ----------
        record : :class:`~.record_types.EquipmentRecord`
            A record from an :ref:`equipment_database`.

        Raises
        ------
        :exc:`~.exceptions.MSLConnectionError`
            If the Serial port cannot be opened.
        """
        ConnectionMessageBased.__init__(self, record)

        props = record.connection.properties

        self._serial = serial.Serial()

        self.read_termination = props.get('read_termination', self.read_termination)
        self.write_termination = props.get('write_termination', self.write_termination)
        self.max_read_size = props.get('max_read_size', self.max_read_size)
        self.encoding = props.get('encoding', self.encoding)
        self.timeout = props.get('timeout', None)

        # PyVISA and PyVISA-py accept various resource names: 'ASRL', 'COM', 'LPT', 'ASRLCOM'
        # but Serial seems to only be happy with 'COM'
        number = re.search('\d+', record.connection.address)  # get the port number from the address
        if number is None:
            self.raise_exception('No port number was specified -- address={}'.format(record.connection.address))
        self._serial.port = 'COM{}'.format(number.group(0))

        self._serial.parity = props.get('parity', constants.Parity.NONE).value
        self._serial.write_timeout = props.get('write_timeout', None)
        self._serial.inter_byte_timeout = props.get('inter_byte_timeout', None)
        self._serial.exclusive = props.get('exclusive', None)
        self._serial.xonxoff = props.get('xon_xoff', False)
        self._serial.rtscts = props.get('rts_cts', False)
        self._serial.dsrdtr = props.get('dsr_dtr', False)

        if 'baud_rate' in props:
            self._serial.baudrate = props['baud_rate']
        else:
            self._serial.baudrate = props.get('baudrate', 9600)

        if 'data_bits' in props:
            self._serial.bytesize = props['data_bits']
{'fraction_non_alphanumeric': 0.11438848920863309, 'fraction_numerical': 0.0057553956834532375, 'mean_word_length': 2.698891352549889, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4642859', 'n_tokens_mistral': 2445, 'n_tokens_neox': 2327, 'n_words': 697}