def write(self, data):
        """
        Writes data to the device.

        :param data: data to write
        :type data: string

        :returns: number of bytes sent
        :raises: :py:class:`~alarmdecoder.util.CommError`
        """
        data_sent = None

        try:
            if isinstance(data, str):
                data = data.encode('utf-8')

            data_sent = self._device.send(data)

            if data_sent == 0:
                raise CommError('Error writing to device.')

            self.on_write(data=data)

        except (SSL.Error, socket.error) as err:
            raise CommError('Error writing to device.', err)

        return data_sent
Writes data to the device.

        :param data: data to write
        :type data: string

        :returns: number of bytes sent
        :raises: :py:class:`~alarmdecoder.util.CommError`