Example #1
0
    def __init__(self, port_name=None, baud_rate=None,
                 data_bits=None, stop_bits=None):
        # Read config
        config = read_config()[port_name]
        self.port_name = config['port_name']
        self.baud_rate = config['baud_rate']
        self.data_bits = config['data_bits']
        self.stop_bits = config['stop_bits']

        # Initial
        self.weigher = QSerialPort()  # It is the subclass of the QIODevice class;
        self.weigher.setPortName(self.port_name)  # passing name such as 'COM1'
        self.weigher.setBaudRate(int(self.baud_rate))
        self.weigher.setDataBits(QSerialPort.DataBits(int(self.data_bits)))
        self.weigher.setStopBits(QSerialPort.StopBits(int(self.stop_bits)))

        # Logging module
        self.mylogging = MyLogging(logger_name='user')
        self.mylogger = self.mylogging.logger
Example #2
0
def weigherCheck(**kwargs):
    check_status = False
    port_name = kwargs['weigher']['port_name']
    baud_rate = int(kwargs['weigher']['baud_rate'])
    data_bits = int(kwargs['weigher']['data_bits'])
    stop_bits = int(kwargs['weigher']['stop_bits'])
    list_serialPort = QSerialPortInfo.availablePorts()
    # print(list_serialPort)  # the result is like <PyQt5.QtSerialPort.QSerialPortInfo object at 0x00000261AA5C1908>
    if len(list_serialPort) < 1:
        sys.exit('Weigher problem: weigher is missing')
    else:
        # try to use weigher to see whether there is some unknown issues.
        serial = QSerialPort()  # It is the subclass of the QIODevice class;
        serial.setPortName(port_name)  # passing name such as 'COM1'
        serial.setBaudRate(int(baud_rate))
        serial.setDataBits(QSerialPort.DataBits(int(data_bits)))
        serial.setStopBits(QSerialPort.StopBits(int(stop_bits)))
        if serial.open(QIODevice.ReadOnly):
            # The QSerial.waitForReadyRead function blocks until new data is available for reading and the readyRead() signal has been emitted.
            # This function returns true if the readyRead() signal is emitted and there is new data available for reading;
            # Otherwise this function returns false (if an error occurred or the operation timed out).
            if serial.waitForReadyRead(8000):  # the unit is millisecond;
                data1 = serial.readLine()
                data2 = data1.data().decode(
                    'ascii'
                )  # converting the data type to str (the original string);
                data3 = float(
                    data2[1:-4]
                )  # This will raise error if the data can not be converted successfully.
            else:
                sys.exit('Weigher problem: no data received from the weigher.')
            serial.close()
        else:
            sys.exit(
                'Weigher problem: the connection to the specific weigher fails. Please check the port name.'
            )

    check_status = True
    return check_status
Example #3
0
def weigherCheck_linux01(**kwargs):
    """
    Compared with weigherCheck, the main difference is the way to try converting data;
    :param kwargs:
    :return:
    """
    check_status = False
    port_name = kwargs['weigher']['port_name']
    baud_rate = int(kwargs['weigher']['baud_rate'])
    data_bits = int(kwargs['weigher']['data_bits'])
    stop_bits = int(kwargs['weigher']['stop_bits'])
    list_serialPort = QSerialPortInfo.availablePorts()
    # print(list_serialPort)  # the result is like <PyQt5.QtSerialPort.QSerialPortInfo object at 0x00000261AA5C1908>
    if len(list_serialPort) < 1:
        sys.exit('Weigher problem: weigher is missing')
    else:
        # try to use weigher to see whether there is some unknown issues.
        serial = QSerialPort()  # It is the subclass of the QIODevice class;
        serial.setPortName(port_name)  # passing name such as 'COM1'
        serial.setBaudRate(int(baud_rate))
        serial.setDataBits(QSerialPort.DataBits(int(data_bits)))
        serial.setStopBits(QSerialPort.StopBits(int(stop_bits)))
        if serial.open(QIODevice.ReadOnly):
            # The QSerial.waitForReadyRead function blocks until new data is available for reading and the readyRead() signal has been emitted.
            # This function returns true if the readyRead() signal is emitted and there is new data available for reading;
            # Otherwise this function returns false (if an error occurred or the operation timed out).
            if serial.waitForReadyRead(8000):  # the unit is millisecond;
                discard = serial.readAll()
                status_data_convert = True
                convert_times = 0
                can_readline_times = 0
                while status_data_convert:
                    if serial.waitForReadyRead(500):
                        if serial.canReadLine():
                            data1 = serial.readLine()
                            data2 = data1.data().decode(
                                'ascii'
                            )  # converting the data type to str (the original string);
                            try:
                                data3 = float(
                                    data2[1:-4]
                                )  # This will raise error if the data can not be converted successfully.
                                status_data_convert = False
                            except BaseException:
                                convert_times = convert_times + 1
                                print(
                                    'Trying to convert the data fails with total %s times.'
                                    % convert_times)
                                if convert_times > 29:
                                    sys.exit(
                                        'Weigher problem: the string data given by weigher can not be converted to the float data successfully.'
                                    )
                        else:
                            can_readline_times = can_readline_times + 1
                            print('canReadLine fails with total %s times.' %
                                  can_readline_times)
                            if can_readline_times > 29:
                                sys.exit(
                                    'Weigher problem: there is no readLine signal too many times.'
                                )
                    else:
                        convert_times = convert_times + 1
                        print('waitForReadyRead fails with total %s times.' %
                              convert_times)
                        if convert_times > 29:
                            sys.exit(
                                'Weigher problem: the string data given by weigher can not be converted to the float data successfully.'
                            )
            else:
                sys.exit('Weigher problem: no data received from the weigher.')
            serial.close()
        else:
            sys.exit(
                'Weigher problem: the connection to the specific weigher fails. Please check the port name.'
            )

    check_status = True
    return check_status