def find_serial_port(name):
    """Utility function to scan available serial ports by name. The returned object is intended to be passed to the
    :meth:`~pyrealtime.serial_layer.SerialWriteLayer.from_port` constructor of
    :class:`~pyrealtime.serial_layer.SerialReadLayer` or :class:`~pyrealtime.serial_layer.SerialWriteLayer`.

    :param name: The name of the serial port to scan for. It will return the first available port containing name.
    :return: A closed serial port object
    """
    try:
        import serial
        import serial.tools.list_ports
    except ImportError:
        raise ModuleNotFoundError('PySerial not found')
    ports = list(serial.tools.list_ports.comports())
    port = None
    for p in ports:
        if name in p.description:
            port = p.device
    if port is None:
        for p in ports:
            if name in p.device:
                port = p.device
    if port is None:
        print('Error: could not find port: %s.' % name)
        print('Available ports:')
        for p in ports:
            print('%s: %s' % (p, p.description))
    return port
