Exemplo n.º 1
0
    def __init__(self, *args):
        PluginBase.__init__(self, BrickletRS232, *args)

        self.setupUi(self)
        
        has_errors = self.firmware_version >= (2, 0, 1)
        
        self.text.setReadOnly(True)

        self.rs232 = self.device

        self.qtcb_read.connect(self.cb_read)
        self.rs232.register_callback(self.rs232.CALLBACK_READ_CALLBACK,
                                     self.qtcb_read.emit)

        if has_errors:
            self.label_no_error_support.hide()
            self.qtcb_error.connect(self.cb_error)
            self.rs232.register_callback(self.rs232.CALLBACK_ERROR_CALLBACK,
                                         self.qtcb_error.emit)
        else:
            self.widget_errors.hide()

        self.input_combobox.addItem("")
        self.input_combobox.lineEdit().setMaxLength(58)
        self.input_combobox.lineEdit().returnPressed.connect(self.input_changed)

        self.line_ending_lineedit.setValidator(TFHexValidator())
        self.line_ending_combobox.currentIndexChanged.connect(self.line_ending_changed)
        self.line_ending_lineedit.editingFinished.connect(self.line_ending_changed)

        self.baudrate_combobox.currentIndexChanged.connect(self.configuration_changed)
        self.parity_combobox.currentIndexChanged.connect(self.configuration_changed)
        self.stopbits_spinbox.valueChanged.connect(self.configuration_changed)
        self.wordlength_spinbox.valueChanged.connect(self.configuration_changed)
        self.hardware_flowcontrol_combobox.currentIndexChanged.connect(self.configuration_changed)
        self.software_flowcontrol_combobox.currentIndexChanged.connect(self.configuration_changed)
        self.text_type_combobox.currentIndexChanged.connect(self.text_type_changed)

        self.hextext = QHexeditWidget(self.text.font())
        self.hextext.hide()
        self.layout().insertWidget(2, self.hextext)

        self.button_clear_text.clicked.connect(lambda: self.text.setPlainText(""))
        self.button_clear_text.clicked.connect(self.hextext.clear)

        self.save_button.clicked.connect(self.save_clicked)
        
        self.error_overrun = 0
        self.error_parity = 0
        self.error_framing = 0

        self.last_char = ''
Exemplo n.º 2
0
    def __init__(self, *args):
        PluginBase.__init__(self, BrickletRS232, *args)

        self.setupUi(self)
        self.text.setReadOnly(True)

        self.rs232 = self.device

        self.qtcb_read.connect(self.cb_read)
        self.rs232.register_callback(self.rs232.CALLBACK_READ_CALLBACK,
                                     self.qtcb_read.emit)

        self.input_combobox.addItem("")
        self.input_combobox.lineEdit().setMaxLength(58)
        self.input_combobox.lineEdit().returnPressed.connect(self.input_changed)

        self.line_ending_lineedit.setValidator(TFHexValidator())
        self.line_ending_combobox.currentIndexChanged.connect(self.line_ending_changed)
        self.line_ending_lineedit.editingFinished.connect(self.line_ending_changed)

        self.baudrate_combobox.currentIndexChanged.connect(self.configuration_changed)
        self.parity_combobox.currentIndexChanged.connect(self.configuration_changed)
        self.stopbits_spinbox.valueChanged.connect(self.configuration_changed)
        self.wordlength_spinbox.valueChanged.connect(self.configuration_changed)
        self.hardware_flowcontrol_combobox.currentIndexChanged.connect(self.configuration_changed)
        self.software_flowcontrol_combobox.currentIndexChanged.connect(self.configuration_changed)
        self.text_type_combobox.currentIndexChanged.connect(self.text_type_changed)

        self.hextext = QHexeditWidget(self.text.font())
        self.hextext.hide()
        self.layout().insertWidget(2, self.hextext)

        self.save_button.clicked.connect(self.save_clicked)
Exemplo n.º 3
0
    def __init__(self, *args):
        PluginBase.__init__(self, BrickletRS232, *args)

        self.setupUi(self)
        
        has_errors = self.firmware_version >= (2, 0, 1)
        
        self.text.setReadOnly(True)

        self.rs232 = self.device

        self.read_callback_was_enabled = False

        self.qtcb_read.connect(self.cb_read)
        self.rs232.register_callback(self.rs232.CALLBACK_READ_CALLBACK,
                                     self.qtcb_read.emit)

        if has_errors:
            self.label_no_error_support.hide()
            self.qtcb_error.connect(self.cb_error)
            self.rs232.register_callback(self.rs232.CALLBACK_ERROR_CALLBACK,
                                         self.qtcb_error.emit)
        else:
            self.widget_errors.hide()

        self.input_combobox.addItem("")
        self.input_combobox.lineEdit().setMaxLength(58)
        self.input_combobox.lineEdit().returnPressed.connect(self.input_changed)

        self.line_ending_lineedit.setValidator(HexValidator())
        self.line_ending_combobox.currentIndexChanged.connect(self.line_ending_changed)
        self.line_ending_lineedit.editingFinished.connect(self.line_ending_changed)

        self.baudrate_combobox.currentIndexChanged.connect(self.configuration_changed)
        self.parity_combobox.currentIndexChanged.connect(self.configuration_changed)
        self.stopbits_spinbox.valueChanged.connect(self.configuration_changed)
        self.wordlength_spinbox.valueChanged.connect(self.configuration_changed)
        self.hardware_flowcontrol_combobox.currentIndexChanged.connect(self.configuration_changed)
        self.software_flowcontrol_combobox.currentIndexChanged.connect(self.configuration_changed)
        self.text_type_combobox.currentIndexChanged.connect(self.text_type_changed)

        self.hextext = QHexeditWidget(self.text.font())
        self.hextext.hide()
        self.layout().insertWidget(2, self.hextext)

        self.button_clear_text.clicked.connect(lambda: self.text.setPlainText(""))
        self.button_clear_text.clicked.connect(self.hextext.clear)

        self.save_button.clicked.connect(self.save_clicked)
        
        self.error_overrun = 0
        self.error_parity = 0
        self.error_framing = 0

        self.last_char = ''
Exemplo n.º 4
0
class RS232(PluginBase, Ui_RS232):
    qtcb_read = pyqtSignal(object, int)
    qtcb_error = pyqtSignal(int)

    def __init__(self, *args):
        PluginBase.__init__(self, BrickletRS232, *args)

        self.setupUi(self)
        
        has_errors = self.firmware_version >= (2, 0, 1)
        
        self.text.setReadOnly(True)

        self.rs232 = self.device

        self.read_callback_was_enabled = False

        self.qtcb_read.connect(self.cb_read)
        self.rs232.register_callback(self.rs232.CALLBACK_READ_CALLBACK,
                                     self.qtcb_read.emit)

        if has_errors:
            self.label_no_error_support.hide()
            self.qtcb_error.connect(self.cb_error)
            self.rs232.register_callback(self.rs232.CALLBACK_ERROR_CALLBACK,
                                         self.qtcb_error.emit)
        else:
            self.widget_errors.hide()

        self.input_combobox.addItem("")
        self.input_combobox.lineEdit().setMaxLength(58)
        self.input_combobox.lineEdit().returnPressed.connect(self.input_changed)

        self.line_ending_lineedit.setValidator(HexValidator())
        self.line_ending_combobox.currentIndexChanged.connect(self.line_ending_changed)
        self.line_ending_lineedit.editingFinished.connect(self.line_ending_changed)

        self.baudrate_combobox.currentIndexChanged.connect(self.configuration_changed)
        self.parity_combobox.currentIndexChanged.connect(self.configuration_changed)
        self.stopbits_spinbox.valueChanged.connect(self.configuration_changed)
        self.wordlength_spinbox.valueChanged.connect(self.configuration_changed)
        self.hardware_flowcontrol_combobox.currentIndexChanged.connect(self.configuration_changed)
        self.software_flowcontrol_combobox.currentIndexChanged.connect(self.configuration_changed)
        self.text_type_combobox.currentIndexChanged.connect(self.text_type_changed)

        self.hextext = QHexeditWidget(self.text.font())
        self.hextext.hide()
        self.layout().insertWidget(2, self.hextext)

        self.button_clear_text.clicked.connect(lambda: self.text.setPlainText(""))
        self.button_clear_text.clicked.connect(self.hextext.clear)

        self.save_button.clicked.connect(self.save_clicked)
        
        self.error_overrun = 0
        self.error_parity = 0
        self.error_framing = 0

        self.last_char = ''

    def cb_error(self, error):
        if error & BrickletRS232.ERROR_OVERRUN:
            self.error_overrun += 1
            self.label_error_overrun.setText(str(self.error_overrun))
        if error & BrickletRS232.ERROR_PARITY:
            self.error_parity += 1
            self.label_error_parity.setText(str(self.error_parity))
        if error & BrickletRS232.ERROR_FRAMING:
            self.error_framing += 1
            self.label_error_framing.setText(str(self.error_framing))

    def cb_read(self, message, length):
        s = ''.join(message[:length])
        self.hextext.appendData(s)

        # check if a \r\n or \n\r was split into two messages. the first one
        # ended with \r or \n and the net one starts with \n or \r
        if len(s) > 0:
            if s[0] != self.last_char and self.last_char in ['\r', '\n'] and s[0] in ['\r', '\n']:
                s = s[1:]

            if len(s) > 0:
                self.last_char = s[-1]
            else:
                self.last_char = ''

        # QTextEdit breaks lines at \r and \n
        s = s.replace('\n\r', '\n').replace('\r\n', '\n')

        ascii = ''
        for c in s:
            if (ord(c) < 32 or ord(c) > 126) and not (ord(c) in (10, 13)):
                ascii += '.'
            else:
                ascii += c

        self.text.moveCursor(QTextCursor.End)
        self.text.insertPlainText(ascii)
        self.text.moveCursor(QTextCursor.End)

    def line_ending_changed(self):
        selected_line_ending = self.line_ending_combobox.currentText()
        self.line_ending_lineedit.setEnabled( (selected_line_ending == 'Hex:' ))

    def get_line_ending(self):
        selected_line_ending = self.line_ending_combobox.currentText()

        if selected_line_ending == '\\n':
            hex_le = '0A'
        elif selected_line_ending == '\\r':
            hex_le = '0D'
        elif selected_line_ending == '\\r\\n':
            hex_le = '0D0A'
        elif selected_line_ending == '\\n\\r':
            hex_le = '0A0D'
        elif selected_line_ending == '\\0':
            hex_le = "00"
        elif selected_line_ending == 'Hex:':
            hex_le = self.line_ending_lineedit.text()
        else:
            hex_le = ''

        try:
            line_ending = hex_le.decode('hex')
        except TypeError:
            # TODO: Handle Error!
            # Should never happen, because LineEdit has a validator applied
            line_ending = ''

        return line_ending

    def input_changed(self):
        text = self.input_combobox.currentText().encode('utf-8') + self.get_line_ending()
        c = ['\0']*60
        for i, t in enumerate(text):
            c[i] = t

        length = len(text)
        written = 0
        while length != 0:
            written = self.rs232.write(c, length)
            c = c[written:]
            c = c + ['\0']*written
            length = length - written

        self.input_combobox.setCurrentIndex(0)

    def get_configuration_async(self, conf):
        self.baudrate_combobox.setCurrentIndex(conf.baudrate)
        self.parity_combobox.setCurrentIndex(conf.parity)
        self.stopbits_spinbox.setValue(conf.stopbits)
        self.wordlength_spinbox.setValue(conf.wordlength)
        self.hardware_flowcontrol_combobox.setCurrentIndex(conf.hardware_flowcontrol)
        self.software_flowcontrol_combobox.setCurrentIndex(conf.software_flowcontrol)
        self.save_button.setEnabled(False)

    def text_type_changed(self):
        if self.text_type_combobox.currentIndex() == 0:
            self.hextext.hide()
            self.text.show()
        else:
            self.text.hide()
            self.hextext.show()

    def configuration_changed(self):
        self.save_button.setEnabled(True)

    def save_clicked(self):
        baudrate = self.baudrate_combobox.currentIndex()
        parity = self.parity_combobox.currentIndex()
        stopbits = self.stopbits_spinbox.value()
        wordlength = self.wordlength_spinbox.value()
        hardware_flowcontrol = self.hardware_flowcontrol_combobox.currentIndex()
        software_flowcontrol = self.software_flowcontrol_combobox.currentIndex()

        self.rs232.set_configuration(baudrate, parity, stopbits, wordlength, hardware_flowcontrol, software_flowcontrol)
        self.save_button.setEnabled(False)

    def is_read_callback_enabled_async(self, enabled):
        self.read_callback_was_enabled = enabled
        self.rs232.enable_read_callback()

    def start(self):
        self.read_callback_was_enabled = False

        async_call(self.rs232.is_read_callback_enabled, None, self.is_read_callback_enabled_async, self.increase_error_count)
        async_call(self.rs232.get_configuration, None, self.get_configuration_async, self.increase_error_count)

    def stop(self):
        if not self.read_callback_was_enabled:
            try:
                self.rs232.disable_read_callback()
            except:
                pass

    def destroy(self):
        pass

    def get_url_part(self):
        return 'rs232'

    @staticmethod
    def has_device_identifier(device_identifier):
        return device_identifier == BrickletRS232.DEVICE_IDENTIFIER
Exemplo n.º 5
0
class RS232(PluginBase, Ui_RS232):
    qtcb_read = pyqtSignal(object, int)
    qtcb_error = pyqtSignal(int)

    def __init__(self, *args):
        PluginBase.__init__(self, BrickletRS232, *args)

        self.setupUi(self)
        
        has_errors = self.firmware_version >= (2, 0, 1)
        
        self.text.setReadOnly(True)

        self.rs232 = self.device

        self.qtcb_read.connect(self.cb_read)
        self.rs232.register_callback(self.rs232.CALLBACK_READ_CALLBACK,
                                     self.qtcb_read.emit)

        if has_errors:
            self.label_no_error_support.hide()
            self.qtcb_error.connect(self.cb_error)
            self.rs232.register_callback(self.rs232.CALLBACK_ERROR_CALLBACK,
                                         self.qtcb_error.emit)
        else:
            self.widget_errors.hide()

        self.input_combobox.addItem("")
        self.input_combobox.lineEdit().setMaxLength(58)
        self.input_combobox.lineEdit().returnPressed.connect(self.input_changed)

        self.line_ending_lineedit.setValidator(TFHexValidator())
        self.line_ending_combobox.currentIndexChanged.connect(self.line_ending_changed)
        self.line_ending_lineedit.editingFinished.connect(self.line_ending_changed)

        self.baudrate_combobox.currentIndexChanged.connect(self.configuration_changed)
        self.parity_combobox.currentIndexChanged.connect(self.configuration_changed)
        self.stopbits_spinbox.valueChanged.connect(self.configuration_changed)
        self.wordlength_spinbox.valueChanged.connect(self.configuration_changed)
        self.hardware_flowcontrol_combobox.currentIndexChanged.connect(self.configuration_changed)
        self.software_flowcontrol_combobox.currentIndexChanged.connect(self.configuration_changed)
        self.text_type_combobox.currentIndexChanged.connect(self.text_type_changed)

        self.hextext = QHexeditWidget(self.text.font())
        self.hextext.hide()
        self.layout().insertWidget(2, self.hextext)

        self.button_clear_text.clicked.connect(lambda: self.text.setPlainText(""))
        self.button_clear_text.clicked.connect(self.hextext.clear)

        self.save_button.clicked.connect(self.save_clicked)
        
        self.error_overrun = 0
        self.error_parity = 0
        self.error_framing = 0

        self.last_char = ''

    def cb_error(self, error):
        if error & BrickletRS232.ERROR_OVERRUN:
            self.error_overrun += 1
            self.label_error_overrun.setText(str(self.error_overrun))
        if error & BrickletRS232.ERROR_PARITY:
            self.error_parity += 1
            self.label_parity_overrun.setText(str(self.error_parity))
        if error & BrickletRS232.ERROR_FRAMING:
            self.error_framing += 1
            self.label_error_framing.setText(str(self.error_framing))

    def cb_read(self, message, length):
        s = ''.join(message[:length])
        self.hextext.appendData(s)

        # check if a \r\n or \n\r was split into two messages. the first one
        # ended with \r or \n and the net one starts with \n or \r
        if len(s) > 0:
            if s[0] != self.last_char and self.last_char in ['\r', '\n'] and s[0] in ['\r', '\n']:
                s = s[1:]

            if len(s) > 0:
                self.last_char = s[-1]
            else:
                self.last_char = ''

        # QTextEdit breaks lines at \r and \n
        s = s.replace('\n\r', '\n').replace('\r\n', '\n')

        ascii = ''
        for c in s:
            if (ord(c) < 32 or ord(c) > 126) and not (ord(c) in (10, 13)):
                ascii += '.'
            else:
                ascii += c

        self.text.moveCursor(QTextCursor.End)
        self.text.insertPlainText(ascii)
        self.text.moveCursor(QTextCursor.End)

    def line_ending_changed(self):
        selected_line_ending = self.line_ending_combobox.currentText()
        self.line_ending_lineedit.setEnabled( (selected_line_ending == 'Hex:' ))

    def get_line_ending(self):
        selected_line_ending = self.line_ending_combobox.currentText()

        if selected_line_ending == '\\n':
            hex_le = '0A'
        elif selected_line_ending == '\\r':
            hex_le = '0D'
        elif selected_line_ending == '\\r\\n':
            hex_le = '0D0A'
        elif selected_line_ending == '\\n\\r':
            hex_le = '0A0D'
        elif selected_line_ending == '\\0':
            hex_le = "00"
        elif selected_line_ending == 'Hex:':
            hex_le = self.line_ending_lineedit.text()
        else:
            hex_le = ''

        try:
            line_ending = hex_le.decode('hex')
        except TypeError:
            # TODO: Handle Error!
            # Should never happen, because LineEdit has a validator applied
            line_ending = ''

        return line_ending

    def input_changed(self):
        text = self.input_combobox.currentText().encode('utf-8') + self.get_line_ending()
        c = ['\0']*60
        for i, t in enumerate(text):
            c[i] = t

        length = len(text)
        written = 0
        while length != 0:
            written = self.rs232.write(c, length)
            c = c[written:]
            c = c + ['\0']*written
            length = length - written

        self.input_combobox.setCurrentIndex(0)

    def get_configuration_async(self, conf):
        self.baudrate_combobox.setCurrentIndex(conf.baudrate)
        self.parity_combobox.setCurrentIndex(conf.parity)
        self.stopbits_spinbox.setValue(conf.stopbits)
        self.wordlength_spinbox.setValue(conf.wordlength)
        self.hardware_flowcontrol_combobox.setCurrentIndex(conf.hardware_flowcontrol)
        self.software_flowcontrol_combobox.setCurrentIndex(conf.software_flowcontrol)
        self.save_button.setEnabled(False)

    def text_type_changed(self):
        if self.text_type_combobox.currentIndex() == 0:
            self.hextext.hide()
            self.text.show()
        else:
            self.text.hide()
            self.hextext.show()

    def configuration_changed(self):
        self.save_button.setEnabled(True)

    def save_clicked(self):
        baudrate = self.baudrate_combobox.currentIndex()
        parity = self.parity_combobox.currentIndex()
        stopbits = self.stopbits_spinbox.value()
        wordlength = self.wordlength_spinbox.value()
        hardware_flowcontrol = self.hardware_flowcontrol_combobox.currentIndex()
        software_flowcontrol = self.software_flowcontrol_combobox.currentIndex()

        self.rs232.set_configuration(baudrate, parity, stopbits, wordlength, hardware_flowcontrol, software_flowcontrol)
        self.save_button.setEnabled(False)

    def start(self):
        async_call(self.rs232.get_configuration, None, self.get_configuration_async, self.increase_error_count)
        self.rs232.enable_read_callback()
#        self.cbe_read.set_period(100)

    def stop(self):
        pass
#        self.cbe_read.set_period(0)

    def destroy(self):
        pass

    def get_url_part(self):
        return 'rs232'

    @staticmethod
    def has_device_identifier(device_identifier):
        return device_identifier == BrickletRS232.DEVICE_IDENTIFIER
Exemplo n.º 6
0
class RS232(PluginBase, Ui_RS232):
    qtcb_read = pyqtSignal(object, int)

    def __init__(self, *args):
        PluginBase.__init__(self, BrickletRS232, *args)

        self.setupUi(self)
        self.text.setReadOnly(True)

        self.rs232 = self.device

        self.qtcb_read.connect(self.cb_read)
        self.rs232.register_callback(self.rs232.CALLBACK_READ_CALLBACK,
                                     self.qtcb_read.emit)

        self.input_combobox.addItem("")
        self.input_combobox.lineEdit().setMaxLength(58)
        self.input_combobox.lineEdit().returnPressed.connect(self.input_changed)

        self.line_ending_lineedit.setValidator(TFHexValidator())
        self.line_ending_combobox.currentIndexChanged.connect(self.line_ending_changed)
        self.line_ending_lineedit.editingFinished.connect(self.line_ending_changed)

        self.baudrate_combobox.currentIndexChanged.connect(self.configuration_changed)
        self.parity_combobox.currentIndexChanged.connect(self.configuration_changed)
        self.stopbits_spinbox.valueChanged.connect(self.configuration_changed)
        self.wordlength_spinbox.valueChanged.connect(self.configuration_changed)
        self.hardware_flowcontrol_combobox.currentIndexChanged.connect(self.configuration_changed)
        self.software_flowcontrol_combobox.currentIndexChanged.connect(self.configuration_changed)
        self.text_type_combobox.currentIndexChanged.connect(self.text_type_changed)

        self.hextext = QHexeditWidget(self.text.font())
        self.hextext.hide()
        self.layout().insertWidget(2, self.hextext)

        self.save_button.clicked.connect(self.save_clicked)

    def cb_read(self, message, length):
        s = ''.join(message[:length])
        self.hextext.appendData(s)

        # QTextEdit breaks lines at \r and \n
        s = s.replace('\n\r', '\n').replace('\r\n', '\n')

        ascii = ''
        for c in s:
            if (ord(c) < 32 or ord(c) > 126) and not (ord(c) in (10, 13)):
                ascii += '.'
            else:
                ascii += c

        self.text.moveCursor(QTextCursor.End)
        self.text.insertPlainText(ascii)
        self.text.moveCursor(QTextCursor.End)

    def line_ending_changed(self):
        selected_line_ending = self.line_ending_combobox.currentText()
        self.line_ending_lineedit.setEnabled( (selected_line_ending == 'Hex:' ))

    def get_line_ending(self):
        selected_line_ending = self.line_ending_combobox.currentText()

        if selected_line_ending == '\\n':
            hex_le = '0A'
        elif selected_line_ending == '\\r':
            hex_le = '0D'
        elif selected_line_ending == '\\r\\n':
            hex_le = '0D0A'
        elif selected_line_ending == '\\n\\r':
            hex_le = '0A0D'
        elif selected_line_ending == '\\0':
            hex_le = "00"
        elif selected_line_ending == 'Hex:':
            hex_le = self.line_ending_lineedit.text()
        else:
            hex_le = ''

        try:
            line_ending = hex_le.decode('hex')
        except TypeError:
            # TODO: Handle Error!
            # Should never happen, because LineEdit has a validator applied
            line_ending = ''

        return line_ending

    def input_changed(self):
        text = self.input_combobox.currentText().encode('utf-8') + self.get_line_ending()
        c = ['\0']*60
        for i, t in enumerate(text):
            c[i] = t

        length = len(text)
        written = 0
        while length != 0:
            written = self.rs232.write(c, length)
            c = c[written:]
            c = c + ['\0']*written
            length = length - written

        self.input_combobox.setCurrentIndex(0)

    def get_configuration_async(self, conf):
        self.baudrate_combobox.setCurrentIndex(conf.baudrate)
        self.parity_combobox.setCurrentIndex(conf.parity)
        self.stopbits_spinbox.setValue(conf.stopbits)
        self.wordlength_spinbox.setValue(conf.wordlength)
        self.hardware_flowcontrol_combobox.setCurrentIndex(conf.hardware_flowcontrol)
        self.software_flowcontrol_combobox.setCurrentIndex(conf.software_flowcontrol)
        self.save_button.setEnabled(False)

    def text_type_changed(self):
        if self.text_type_combobox.currentIndex() == 0:
            self.hextext.hide()
            self.text.show()
        else:
            self.text.hide()
            self.hextext.show()

    def configuration_changed(self):
        self.save_button.setEnabled(True)

    def save_clicked(self):
        baudrate = self.baudrate_combobox.currentIndex()
        parity = self.parity_combobox.currentIndex()
        stopbits = self.stopbits_spinbox.value()
        wordlength = self.wordlength_spinbox.value()
        hardware_flowcontrol = self.hardware_flowcontrol_combobox.currentIndex()
        software_flowcontrol = self.software_flowcontrol_combobox.currentIndex()

        self.rs232.set_configuration(baudrate, parity, stopbits, wordlength, hardware_flowcontrol, software_flowcontrol)
        self.save_button.setEnabled(False)

    def start(self):
        async_call(self.rs232.get_configuration, None, self.get_configuration_async, self.increase_error_count)
        self.rs232.enable_read_callback()
#        self.cbe_read.set_period(100)

    def stop(self):
        pass
#        self.cbe_read.set_period(0)

    def destroy(self):
        pass

    def get_url_part(self):
        return 'rs232'

    @staticmethod
    def has_device_identifier(device_identifier):
        return device_identifier == BrickletRS232.DEVICE_IDENTIFIER
Exemplo n.º 7
0
    def __init__(self, *args):
        COMCUPluginBase.__init__(self, BrickletRS232V2, *args)

        self.setupUi(self)

        self.text.setReadOnly(True)

        self.rs232 = self.device

        self.read_callback_was_enabled = False

        self.qtcb_read.connect(self.cb_read)
        self.rs232.register_callback(self.rs232.CALLBACK_READ,
                                     self.qtcb_read.emit)

        self.qtcb_error_count.connect(self.cb_error_count)
        self.rs232.register_callback(self.rs232.CALLBACK_ERROR_COUNT,
                                     self.qtcb_error_count.emit)

        self.input_combobox.addItem("")
        self.input_combobox.lineEdit().setMaxLength(65533)
        self.input_combobox.lineEdit().returnPressed.connect(
            self.input_changed)

        self.line_ending_lineedit.setValidator(HexValidator())
        self.line_ending_combobox.currentIndexChanged.connect(
            self.line_ending_changed)
        self.line_ending_lineedit.editingFinished.connect(
            self.line_ending_changed)

        self.baudrate_spinbox.valueChanged.connect(self.configuration_changed)
        self.parity_combobox.currentIndexChanged.connect(
            self.configuration_changed)
        self.stopbits_spinbox.valueChanged.connect(self.configuration_changed)
        self.wordlength_spinbox.valueChanged.connect(
            self.configuration_changed)
        self.flowcontrol_combobox.currentIndexChanged.connect(
            self.configuration_changed)
        self.text_type_combobox.currentIndexChanged.connect(
            self.text_type_changed)

        self.hextext = QHexeditWidget(self.text.font())
        self.hextext.hide()
        self.layout().insertWidget(2, self.hextext)

        self.button_clear_text.clicked.connect(
            lambda: self.text.setPlainText(""))
        self.button_clear_text.clicked.connect(self.hextext.clear)

        self.save_button.clicked.connect(self.save_clicked)

        self.error_stream_oos = 0

        self.last_char = ''

        if self.baudrate_spinbox.value() > BAUDRATE_MAX_RS232:
            self.label_note_baud.show()
        else:
            self.label_note_baud.hide()

        if self.flowcontrol_combobox.currentIndex() == CBOX_IDX_FC_HW:
            self.label_note_fc_hw.show()
        else:
            self.label_note_fc_hw.hide()
Exemplo n.º 8
0
class RS232V2(COMCUPluginBase, Ui_RS232_V2):
    qtcb_read = pyqtSignal(object)
    qtcb_error_count = pyqtSignal(int, int)

    def __init__(self, *args):
        COMCUPluginBase.__init__(self, BrickletRS232V2, *args)

        self.setupUi(self)

        self.text.setReadOnly(True)

        self.rs232 = self.device

        self.read_callback_was_enabled = False

        self.qtcb_read.connect(self.cb_read)
        self.rs232.register_callback(self.rs232.CALLBACK_READ,
                                     self.qtcb_read.emit)

        self.qtcb_error_count.connect(self.cb_error_count)
        self.rs232.register_callback(self.rs232.CALLBACK_ERROR_COUNT,
                                     self.qtcb_error_count.emit)

        self.input_combobox.addItem("")
        self.input_combobox.lineEdit().setMaxLength(65533)
        self.input_combobox.lineEdit().returnPressed.connect(
            self.input_changed)

        self.line_ending_lineedit.setValidator(HexValidator())
        self.line_ending_combobox.currentIndexChanged.connect(
            self.line_ending_changed)
        self.line_ending_lineedit.editingFinished.connect(
            self.line_ending_changed)

        self.baudrate_spinbox.valueChanged.connect(self.configuration_changed)
        self.parity_combobox.currentIndexChanged.connect(
            self.configuration_changed)
        self.stopbits_spinbox.valueChanged.connect(self.configuration_changed)
        self.wordlength_spinbox.valueChanged.connect(
            self.configuration_changed)
        self.flowcontrol_combobox.currentIndexChanged.connect(
            self.configuration_changed)
        self.text_type_combobox.currentIndexChanged.connect(
            self.text_type_changed)

        self.hextext = QHexeditWidget(self.text.font())
        self.hextext.hide()
        self.layout().insertWidget(2, self.hextext)

        self.button_clear_text.clicked.connect(
            lambda: self.text.setPlainText(""))
        self.button_clear_text.clicked.connect(self.hextext.clear)

        self.save_button.clicked.connect(self.save_clicked)

        self.error_stream_oos = 0

        self.last_char = ''

        if self.baudrate_spinbox.value() > BAUDRATE_MAX_RS232:
            self.label_note_baud.show()
        else:
            self.label_note_baud.hide()

        if self.flowcontrol_combobox.currentIndex() == CBOX_IDX_FC_HW:
            self.label_note_fc_hw.show()
        else:
            self.label_note_fc_hw.hide()

    def cb_error_count(self, error_count_overrun, error_count_parity):
        self.label_error_overrun.setText(str(error_count_overrun))
        self.label_error_parity.setText(str(error_count_parity))

    def cb_read(self, message):
        if message == None:
            # Increase stream error counter
            self.error_stream_oos = self.error_stream_oos + 1
            self.label_error_streaming.setText(str(self.error_stream_oos))

            return

        s = ''.join(message)

        self.hextext.appendData(s)

        # check if a \r\n or \n\r was split into two messages. the first one
        # ended with \r or \n and the next one starts with \n or \r
        if len(s) > 0:
            if s[0] != self.last_char and self.last_char in [
                    '\r', '\n'
            ] and s[0] in ['\r', '\n']:
                s = s[1:]

            if len(s) > 0:
                self.last_char = s[-1]
            else:
                self.last_char = ''

        # QTextEdit breaks lines at \r and \n
        s = s.replace('\n\r', '\n').replace('\r\n', '\n')

        ascii = ''

        for c in s:
            if (ord(c) < 32 or ord(c) > 126) and not (ord(c) in (10, 13)):
                ascii += '.'
            else:
                ascii += c

        self.append_text(ascii)

    def append_text(self, text):
        self.text.moveCursor(QTextCursor.End)
        self.text.insertPlainText(text)
        self.text.moveCursor(QTextCursor.End)

    def line_ending_changed(self):
        selected_line_ending = self.line_ending_combobox.currentText()
        self.line_ending_lineedit.setEnabled((selected_line_ending == 'Hex:'))

    def get_line_ending(self):
        selected_line_ending = self.line_ending_combobox.currentText()

        if selected_line_ending == '\\n':
            hex_le = '0A'
        elif selected_line_ending == '\\r':
            hex_le = '0D'
        elif selected_line_ending == '\\r\\n':
            hex_le = '0D0A'
        elif selected_line_ending == '\\n\\r':
            hex_le = '0A0D'
        elif selected_line_ending == '\\0':
            hex_le = "00"
        elif selected_line_ending == 'Hex:':
            hex_le = self.line_ending_lineedit.text()
        else:
            hex_le = ''

        try:
            line_ending = hex_le.decode('hex')
        except TypeError:
            # TODO: Handle Error!
            # Should never happen, because LineEdit has a validator applied
            line_ending = ''

        return line_ending

    def input_changed(self):
        pos = 0
        written = 0
        text = self.input_combobox.currentText().encode(
            'utf-8') + self.get_line_ending()
        c = ['\0'] * len(text)
        for i, t in enumerate(text):
            c[i] = t

        while pos < len(c):
            written = self.rs232.write(c[pos:])
            pos = pos + written

        self.input_combobox.setCurrentIndex(0)

    def get_configuration_async(self, conf):
        self.stopbits_spinbox.setValue(conf.stopbits)
        self.baudrate_spinbox.setValue(conf.baudrate)
        self.wordlength_spinbox.setValue(conf.wordlength)
        self.parity_combobox.setCurrentIndex(conf.parity)
        self.flowcontrol_combobox.setCurrentIndex(conf.flowcontrol)
        self.save_button.setEnabled(False)

    def text_type_changed(self):
        if self.text_type_combobox.currentIndex() == 0:
            self.hextext.hide()
            self.text.show()
        else:
            self.text.hide()
            self.hextext.show()

    def configuration_changed(self):
        if self.baudrate_spinbox.value() > BAUDRATE_MAX_RS232:
            self.label_note_baud.show()
        else:
            self.label_note_baud.hide()

        self.save_button.setEnabled(True)

        if self.flowcontrol_combobox.currentIndex() == CBOX_IDX_FC_HW:
            self.label_note_fc_hw.show()
        else:
            self.label_note_fc_hw.hide()

    def save_clicked(self):
        baudrate = self.baudrate_spinbox.value()
        stopbits = self.stopbits_spinbox.value()
        wordlength = self.wordlength_spinbox.value()
        parity = self.parity_combobox.currentIndex()
        flowcontrol = self.flowcontrol_combobox.currentIndex()

        self.rs232.set_configuration(baudrate, parity, stopbits, wordlength,
                                     flowcontrol)
        self.save_button.setEnabled(False)

    def is_read_callback_enabled_async(self, enabled):
        self.read_callback_was_enabled = enabled
        self.rs232.enable_read_callback()

    def start(self):
        self.read_callback_was_enabled = False

        async_call(self.rs232.is_read_callback_enabled, None,
                   self.is_read_callback_enabled_async,
                   self.increase_error_count)
        async_call(self.rs232.get_configuration, None,
                   self.get_configuration_async, self.increase_error_count)

    def stop(self):
        if not self.read_callback_was_enabled:
            try:
                self.rs232.disable_read_callback()
            except:
                pass

    def destroy(self):
        pass

    @staticmethod
    def has_device_identifier(device_identifier):
        return device_identifier == BrickletRS232V2.DEVICE_IDENTIFIER