QT 5.0 raspberry error with QSerialPort



  • Hi All,

    I have this code for manage a fingerprint reader :

    secugen.h

    @class SecugenSda04 : public IFingerprint
    {
    Q_OBJECT
    Q_INTERFACES(IFingerprint)
    Q_ENUMS(ErrorReader)

    public:
    SecugenSda04(const QString &serialPort);
    void setSerialPort(qint32 baudRate);

    enum ErrorReader{
        _NONE = 0x00, // Performs the command received from main controller or host (no error)
    };
    

    private:
    QSerialPort serial;
    QByteArray response;
    QString serialPort;
    QTimer *timer;
    QString characterToHexQString(const char character);
    std::vector<int> intToHex(int id);
    void executeCommand(const char cmd, DataContainer &dataContainer, const char param1Hight = 0x00, const char param1Low = 0x00, const char param2Hight = 0x00, const char param2Low = 0x00,const char lwExtraDataHight = 0x00,const char lwExtraDataLow = 0x00,const char hwExtraDataHight = 0x00,const char hwExtraDataLow = 0x00, quint32 baudRate = QSerialPort::Baud9600, QByteArray data= QByteArray());

    //Test avec QextSerialPort
    //QextSerialPort *port;
    

    private slots:
    void checkFingerTouch();

    public slots:
    void waitForFinger();

    signals:
    void resultReady(DataContainer *data);
    void fingerDetected();

    };
    @

    Constructor with some command of test

    @SecugenSda04::SecugenSda04(const QString &serialPort): IFingerprint() {

    serial.setPortName(serialPort);
    // This command work and show the firmware version
    executeCommand(0x05,dataContainer);
    
    
    // This command change the com speed
    DataContainer dataContainer;
    executeCommand(0x21,dataContainer,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,QSerialPort::Baud115200);
    

    [...]@

    @void SecugenSda04::executeCommand(const char cmd, DataContainer &dataContainer, const char param1Hight, const char param1Low, const char param2Hight, const char param2Low ,const char lwExtraDataHight,const char lwExtraDataLow,const char hwExtraDataHight,const char hwExtraDataLow, quint32 baudRate, QByteArray data)
    {
    setSerialPort(baudRate);

    const char channel = 0x00;
    const char stub = 0x00;
    
    int cks = ((int)cmd + (int)param1Hight + (int)param1Low + (int)param2Hight + (int)param2Low +(int)lwExtraDataLow + (int)lwExtraDataHight + (int)hwExtraDataLow + (int)hwExtraDataHight) % 256;
    const char checkSum[] = { cks };
    
    serial.write(&channel,1); // channel 1 byte (alway the same)
    serial.write(&cmd,1); // command 1 byte
    serial.write(&param1Low,1); // param1 2 bytes (byte low)
    serial.write(&param1Hight,1); // param1 2 bytes (byte hight)
    serial.write(&param2Low,1); // param2 2 bytes (byte low)
    serial.write(&param2Hight,1); // param2 2 bytes (byte hight)
    serial.write(&lwExtraDataLow,1); // lwExtraData 2 bytes (byte low)
    serial.write(&lwExtraDataHight,1); // lwExtraData 2 bytes (byte hight)
    serial.write(&hwExtraDataLow,1); // hwExtraData 2 bytes (byte low)
    serial.write(&hwExtraDataHight,1); // hwExtraData 2 bytes (byte hight)
    serial.write(&stub,1); // ErrorCode 1 byte
    serial.write(&checkSum[0],1); // Checksum 1 byte
    
    if(!data.isEmpty())
        serial.write(data.constData(),data.size());
    
    if (serial.waitForBytesWritten(1000)) {
    
        qDebug() << "Serial data written on port" << serial.portName();
    
        QByteArray ack;
    
        while(ack.size() < 12)
            if(serial.waitForReadyRead(100))
                ack += serial.readAll();
    
        dataContainer.setAck(ack);
    
        if(dataContainer.error() == SecugenSda04::_NONE && dataContainer.packetSize() > 0)
        {
            qDebug() << "get the data packet on the serial port ...";
    
            QByteArray data = ack.right(ack.size() - 12);
    
            while(data.size() < dataContainer.packetSize())
                if(serial.waitForReadyRead(100))
                    data += serial.readAll();
     
            if (data.size() > 0)
                dataContainer.setPacket(data);
        }
    }
    

    }
    @

    Initalise port like this :

    @void SecugenSda04::setSerialPort(qint32 baudRate)
    {
    serial.setPortName(serialPort);

    if(!serial.isOpen())
    {
        if (!serial.open(QIODevice::ReadWrite)) {
            qCritical() << "error open : " << serial.errorString() << "(code : " << serial.error() << ")";
        }
    }
    
    if (!serial.setBaudRate(baudRate)) {
        qCritical() << "Can't set baud rate " << baudRate << " baud to port " << serialPort << ", error code " << serial.error();
    }
    
    if (!serial.setDataBits(QSerialPort::Data8)) {
        qCritical() << "Can't set 8 data bits to port " << serialPort << ", error code " << serial.error();
    }
    
    if (!serial.setParity(QSerialPort::NoParity)) {
        qCritical() << "Can't set no patity to port " << serialPort << ", error code " << serial.error();
    }
    
    if (!serial.setStopBits(QSerialPort::OneStop)) {
        qCritical() << "Can't set 1 stop bit to port " << serialPort << ", error code " << serial.error();
    }
    
    if (!serial.setFlowControl(QSerialPort::NoFlowControl)) {
        qCritical() << "Can't set no flow control to port " << serialPort << ", error code " << serial.error();
    }
    

    }@

    problem : When i change the variable "quint32 baudRate" from QSerialPort::Baud9600 to QSerialPort::Baud115200 the fingerprint reader is blocked on reading ... i known the serial port is not closed in this version but it's the same thing with a serial.close()

    I have try a LOT OF THING :

    • close the port and reopen with other baud value
    • update to the last QSerialPort
    • reset the port
    • clear the port
    • try to another baud rate (work only in 9600)

    sorry for this code i´m a noob in C++, if you have som advise for a new code, or something to help me ...
    the same command 0x21 work like a charm on windows to change the com speed


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.