Пример #1
0
bool labPort::openPort( const QString& portName )
{
    if( _closeTimer.isActive() )
    {
        emit portError( "Port is still closing, please retry!" );
        return false;
    }

    if( _port.isOpen() )
    {
        _port.clear();
        _port.close();
    }    

    _port.clearError();
    _closing = false;
    _port.setPortName( portName );
    _msgToSend.clear();

    if( !_port.open( QSerialPort::ReadWrite ) )
    {
        emit portError( "Unable to open port!" );
        return false;
    }

    if( _initTimeoutMs > 0 )
    {
        _initTimer.start( _initTimeoutMs );
    }
    _initValueCounter = 0;
    getInitValues();
    return true;
}
Пример #2
0
void elFlowPort::decodeMsg( const QByteArray& msg )
{
    elFlowAnswer answer = _protocol.interpretAnswer( msg.toStdString() );

    if( answer.type == elFlowAnswerType::elFlowError )
    {
        emit portError( "Invalid answer!" );
    }
    else if( answer.type == elFlowAnswerType::protocolError )
    {
        emit portError( "Protocol error!" );
    }
    else if( answer.type == elFlowAnswerType::elFlowStatus )
    {
        if( answer.text.find( "no error" ) == std::string::npos )
        {
            emit portError( QString::fromStdString( answer.text ) );
        }
    }
    else if( answer.type == elFlowAnswerType::elFlowValue )
    {
        showParameter( answer.paramNum, answer.value, answer.text );
    }
    else
    {
        emit portError( "Warning: unhandled answer type!" );
    }
}
Пример #3
0
void eaps8000UsbUICharWindow::connectPortFunctions()
{
    connect( &_port, SIGNAL( initSuccessful( QString ) ), this,
             SLOT( initFinished( QString ) ) );
    connect( &_port, SIGNAL( portError( QString ) ), this,
             SLOT( portError( QString ) ) );
    connect( &_port, SIGNAL( newVoltage( double ) ), this,
             SLOT( voltageUpdate( double ) ) );
    connect( &_port, SIGNAL( newCurrent( double ) ), this,
             SLOT( currentUpdate( double ) ) );
    connect( &_port, SIGNAL( newPower( double ) ), this,
             SLOT( powerUpdate( double ) ) );
}
Пример #4
0
void elFlowWindow::connectPortFunctions()
{
    connect( &_port, SIGNAL( initSuccessful( QString ) ), this,
             SLOT( initFinished( QString ) ) );
    connect( &_port, SIGNAL( portError( QString ) ), this,
             SLOT( portError( QString ) ) );
    connect( &_port, SIGNAL( newFlow( double ) ), this,
             SLOT( flowUpdate( double ) ) );
    connect( &_port, SIGNAL( newTemperature( double ) ), this,
             SLOT( temperatureUpdate( double ) ) );
    connect( &_port, SIGNAL( newPressure( double ) ), this,
             SLOT( pressureUpdate( double ) ) );
}
Пример #5
0
void thermocouplePort::noAnswerReceived()
{
    if( _msgToSend.size() == 0
            || _expectedAnswer.size() == 0 )
    {
        _msgToSend.clear();
        _expectedAnswer.clear();
        return;
    }

    if( _sendCounter < TRIES_SEND_MSG )
    {
        _sendCounter++;
        sendMsg( _msgToSend[0].toStdString().c_str(), _msgToSend[0].size(),
                 false );
        _checkForAnswerTimer.start( _writingPauseMs*2 );
    }
    else
    {
        if( !_closing )
        {
            emit portError( "No answer after several requests!" );
        }
        _sendCounter = 0;
        _msgToSend.erase( _msgToSend.begin() );
        _expectedAnswer.erase( _expectedAnswer.begin() );
        _answerPending = -1;
        if( _msgToSend.size() > 0 )
        {
            _checkForAnswerTimer.start( _writingPauseMs*2 );
        }
    }
}
Пример #6
0
void labPort::timeToSendMsg()
{
    if( _closing )
    {
        return;
    }

    if( !_port.isOpen() )
    {
        emit portError( "Port not open (writing failed)!" );
        return;
    }

    if( _msgToSend.size() > 0 )
    {
        LOG(INFO) << "write " << _msgToSend[0].size() << " bytes:";
        for( int i = 0; i < _msgToSend[0].size(); i++ )
        {
            char c = _msgToSend[0].at( i );
            LOG(INFO) << "write (dec): " << static_cast<int>(
                             reinterpret_cast<unsigned char&>( c ) );
        }
        _port.write( _msgToSend[0].data(), _msgToSend[0].size() );
        _msgToSend.erase( _msgToSend.begin() );
    }

    if( _msgToSend.size() > 0 )
    {
        _sendTimer.start( _writingPauseMs );
    }
    else
    {
        _sendTimer.stop();
    }
}
Пример #7
0
/**
 * Read a number of bytes from the interface.
 *
 * @param data Pointer to the data byte array to write the bytes to
 * @param maxLength The maximum number of bytes to write
 **/
void SerialLink::readBytes()
{
    if (!validateConnection())
        return;

    if (mode_port)  // ESC32 special data read mode
        return readEsc32Tele();

    const qint64 maxLength = 2048;
    char data[maxLength];
    qint64 numBytes = 0, rBytes = 0;

    dataMutex.lock();
    while ((numBytes = port->bytesAvailable())) {
        /* Read as much data in buffer as possible without overflow */
        rBytes = numBytes;
        if(maxLength < rBytes) rBytes = maxLength;

        if (port->read(data, rBytes) == -1) { // -1 result means error
            emit portError();
            if (!m_linkLossExpected)
                emit communicationError(this->getName(), tr("Could not read data - link %1 is disconnected!").arg(this->getName()));
            return;
        }

        QByteArray b(data, rBytes);
        emit bytesReceived(this, b);
        bitsReceivedTotal += rBytes * 8;

    }
    dataMutex.unlock();
}
Пример #8
0
SerialLink::SerialLink(QString portname, int baudRate, bool hardwareFlowControl, bool parity,
                       int dataBits, int stopBits) :
    port(0),
    portSettings(PortSettings()),
    portOpenMode(QIODevice::ReadWrite),
    portVendorId(0),
    portProductId(0),
    bitsSentTotal(0),
    bitsSentShortTerm(0),
    bitsSentCurrent(0),
    bitsSentMax(0),
    bitsReceivedTotal(0),
    bitsReceivedShortTerm(0),
    bitsReceivedCurrent(0),
    bitsReceivedMax(0),
    connectionStartTime(0),
    ports(new QVector<QString>()),
    waitingToReconnect(0),
    m_reconnectDelayMs(0),
    m_linkLossExpected(false),
    m_stopp(false),
    mode_port(false),
    countRetry(0),
    maxLength(0),
    rows(0),
    cols(0),
    firstRead(0)
{
    portEnumerator = new QextSerialEnumerator();
    portEnumerator->setUpNotifications();
    QObject::connect(portEnumerator, SIGNAL(deviceDiscovered(const QextPortInfo &)), this, SLOT(deviceDiscovered(const QextPortInfo &)));
    QObject::connect(portEnumerator, SIGNAL(deviceRemoved(const QextPortInfo &)), this, SLOT(deviceRemoved(const QextPortInfo &)));

    getCurrentPorts();

    // Setup settings
    this->porthandle = portname.trimmed();
    if (!ports->contains(porthandle))
        porthandle = "";

    // Set unique ID and add link to the list of links
    this->id = getNextLinkId();

    int par = parity ? (int)PAR_EVEN : (int)PAR_NONE;
    int fc = hardwareFlowControl ? (int)FLOW_HARDWARE : (int)FLOW_OFF;

    setBaudRate(baudRate);
    setFlowType(fc);
    setParityType(par);
    setDataBitsType(dataBits);
    setStopBitsType(stopBits);
    setTimeoutMillis(-1);  // -1 means do not block on serial read/write. Do not use zero.
    setReconnectDelayMs(10);  // default 10ms before reconnecting to M4, after detecting that COM port is back

    // Set the port name
    name = this->porthandle.length() ? this->porthandle : tr("Serial Link ") + QString::number(getId());

    QObject::connect(this, SIGNAL(portError()), this, SLOT(disconnect()));

}
Пример #9
0
void labPort::initTimeout()
{
    _initTimer.stop();
    if( _initValueCounter < _numInitValues )
    {
        emit portError( "Initialization timeout!" );
    }
}
Пример #10
0
// add connection (canvas request)
bool model::Network::connect(XPort *outport, XPort *inport) {

    // check outport
    if (!outport) {
        return portError(outport, "[Network::connect()] outport is null: ");
    }
    if (!inport) {
        return portError(inport, "[Network::connect()] inport is null.");
    }
    // obtain and check xmas inport and outport
    Output *xmas_outport = dynamic_cast<Output *>(outport->getPort());
    Input *xmas_inport = dynamic_cast<Input *>(inport->getPort());
    bool success = connect(xmas_outport, xmas_inport);
    emit outport->connectedChanged();
    emit inport->connectedChanged();
    return success;
}
Пример #11
0
void labPort::checkInTimeCount()
{
    if( _closing )
    {
        return;
    }

    if( _inTimeValueCounter < _numInTimeValues )
    {
        emit portError( "Value not read!" );
    }
    _inTimeValueCounter = 0;
}
Пример #12
0
// remove connection (canvas request)
bool model::Network::disconnect(XPort *outport, XPort *inport) {

    // check outport
    if (!outport) {
        return portError(outport, "[Network::disconnect()] outport is null: ");
    }
    if (!inport) {
        return portError(inport, "[Network::disconnect()] inport is null.");
    }
    //  check outport for being xmas outport to inport
    Output *xmas_outport = dynamic_cast<Output *>(outport->getPort());
    Input *xmas_inport = dynamic_cast<Input *>(inport->getPort());
    if (xmas_outport != nullptr && xmas_inport != nullptr) {
        if (disconnect(xmas_outport, xmas_inport)) {
            emit outport->connectedChanged();
            emit inport->connectedChanged();
            return true;
        }
    }
    emit outport->connectedChanged();
    emit inport->connectedChanged();
    return false;
}
Пример #13
0
void SerialLink::deviceRemoved(const QextPortInfo &pi)
{
    bool isValid = isPortHandleValid();

    if (!isValid && pi.vendorID == SERIAL_AQUSB_VENDOR_ID && pi.productID == SERIAL_AQUSB_PRODUCT_ID)
        waitingToReconnect = MG::TIME::getGroundTimeNow();

    if (!port || !port->isOpen() || isValid)
        return;

    emit portError();
    if (!m_linkLossExpected)
        emit communicationError(this->getName(), tr("Link %1 unexpectedly disconnected!").arg(this->porthandle));
}
Пример #14
0
void SerialLink::writeBytes(const char* data, qint64 size)
{
    if (!validateConnection())
        return;
    int b = port->write(data, size);

    if (b > 0) {
        // Increase write counter
        bitsSentTotal += b * 8;
    } else if (b == -1) {
        emit portError();
        if (!m_linkLossExpected)
            emit communicationError(this->getName(), tr("Could not send data - error on link %1").arg(this->porthandle));
    }
}
Пример #15
0
bool SerialLink::validateConnection() {
    bool ok = this->isConnected() && (!port->lastError() || (port->lastError() == E_READ_FAILED && SERIAL_IS_BUGGY_CP210x));
    if (ok && (portOpenMode & QIODevice::ReadOnly) && !port->isReadable())
        ok = false;
    if (ok && (portOpenMode & QIODevice::WriteOnly) && !port->isWritable())
        ok = false;
    if(!ok) {
        emit portError();
        if (!m_linkLossExpected)
            emit communicationError(this->getName(), tr("Link %1 unexpectedly disconnected!").arg(this->porthandle));
        qWarning() << ok << port->lastError() << port->errorString();
        return false;
    }
    return true;
}
Пример #16
0
void elFlowPort::setValue( setValueType type, double value, bool autoAdjust )
{
    _autoAdjust = autoAdjust;

    if( type == setValueType::setTypeNone )
    {
        return;
    }
    else if( type == setValueType::setTypeFlow )
    {
        sendCmd( _protocol.getFlowValueSetCmd( value ) );
    }
    else
    {
        emit portError( "Unknown value to set!" );
    }
}
Пример #17
0
void thermocouplePort::updateValues()
{
    checkInTimeCount();
    if( _expectedAnswer.size() != 0 )
    {
        _expectedAnswer.clear();
        if( !_closing )
        {
            emit portError( "Answer missing!" );
        }
    }

    if( _emitProbeTemperature || _emitAmbientTemperature )
    {
        getTemperatures();
    }
}
Пример #18
0
bool eapsUta12Port::checkAnswerFormat( QByteArray msg, unsigned char* msgValues )
{
    if( msg.size() != MESSAGE_LENGTH )
    {
        emit portError( "Invalid answer length!" );
        return false;
    }
    for( int i = 0; i < MESSAGE_LENGTH; i++ )
    {
        msgValues[i] = (unsigned char) msg.at( i );
    }
    if( !checkMsgBytes( msgValues ) )
    {
        LOG(INFO) << "eaps uta 12 check bytes wrong!";
        return false;
    }
    return true;
}
Пример #19
0
void eapsUta12Port::setValue( setValueType type, double value, bool autoAdjust )
{    
    _setValueType = type;
    if( type == setValueType::setTypeNone )
    {
        return;
    }
    _autoAdjust = autoAdjust;

    if( type == setValueType::setTypeVoltage )
    {
        LOG(INFO) << "eaps uta 12 set voltage: " << value
                  << " adjust: " << autoAdjust;
        _setVoltage = value;
        _lastVoltage = value;
        setVoltage( value );
    }
    else if( type == setValueType::setTypeCurrent )
    {
        LOG(INFO) << "eaps uta 12 set current: " << value
                  << " adjust: " << autoAdjust;
        _setCurrent = value;
        _lastCurrent = value;
        setCurrent( value );
    }
    else if( type == setValueType::setTypePowerByVoltage
             || type == setValueType::setTypePowerByCurrent )
    {
        LOG(INFO) << "eaps uta 12 set power: " << value
                  << " adjust: " << autoAdjust << " by voltage: "
                  << (type == setValueType::setTypePowerByVoltage);
        _setPower = value;
        _lastPower = value;
        if( _lastVoltage > 0.0 && _lastCurrent > 0.0 )
        {
            double resistance = _lastVoltage/_lastCurrent;
            if( type == setValueType::setTypePowerByVoltage )
            {
                setVoltage( std::sqrt( value*resistance ) );
            }
            else if( type == setValueType::setTypePowerByCurrent )
            {
                setCurrent( std::sqrt( value/resistance ) );
            }
        }
        else
        {
            emit portError( "Measure before setting power!" );
        }
    }
    else if( type == setValueType::setTypeResistanceByVoltage
             || type == setValueType::setTypeResistanceByCurrent )
    {
        LOG(INFO) << "eaps uta 12 set resistance: " << value << " adjust: "
                  << autoAdjust << " by voltage: "
                  << (type == setValueType::setTypeResistanceByVoltage);
        _setResistance = value;
        if( _lastVoltage > 0.0 && _lastCurrent > 0.0 && _lastResistance > 0.0 )
        {
            if( type == setValueType::setTypeResistanceByVoltage )
            {
                setVoltage( _lastVoltage*
                            std::sqrt( _setResistance/_lastResistance ) );
            }
            else if( type == setValueType::setTypeResistanceByCurrent )
            {
                setCurrent( _lastCurrent*
                            std::sqrt( _setResistance/_lastResistance ) );
            }
        }
        else
        {
            emit portError( "Measure before setting power!" );
        }
    }
}
Пример #20
0
void labPort::signalError( const QSerialPort::SerialPortError& error )
{
    switch( error )
    {
        case 0: return; // no error
        case 1: emit portError( "Device not found!" );
            break;
        case 2: emit portError( "No Permission!" );
            break;
        case 3: emit portError( "Error when opening an already opened device!" );
            break;
        case 4: emit portError( "Parity error!" );
            break;
        case 5: emit portError( "Framing error!" );
            break;
        case 6: emit portError( "Break condition error!" );
            break;
        case 7: emit portError( "Writing error!" );
            break;
        case 8: emit portError( "Reading error!" );
            break;
        case 9: emit portError( "A resource is no longer available!" );
            break;
        case 10: emit portError( "Unsupported operation!" );
            break;
        case 11: emit portError( "Unknown error!" );
            break;
        case 12: emit portError( "Timeout!" );
            break;
        case 13: emit portError( "Device not open!" );
            break;
        default: emit portError( "Unspecified error!" );
            break;
    }
}
Пример #21
0
    ui->CB_int2Atten->addItems(SL_ext_atten);
    SL_ext_atten.clear();
    SL_ext_atten << "0.0" << "3.0" << "6.0" << "9.0";
    ui->CB_int1Atten->addItems(SL_ext_atten);

    QRegExp fRegex ("^[1]{0,1}[0-9]{4,4}[.]{1,1}[0-9]{0,6}$");
    QRegExpValidator *fValidator = new QRegExpValidator(fRegex, this);
    ui->LE_freq->setValidator(fValidator);


    connect(ui->PB_start,SIGNAL(clicked()),this,SLOT(portStart()));
    connect(ui->PB_stop,SIGNAL(clicked()),this,SLOT(protStop()));
    connect(ui->PB_refresh,SIGNAL(clicked()),this,SLOT(protRefresh()));
    connect(ui->PB_freq, SIGNAL(clicked()), this, SLOT(send_freq()));
    connect(protocol_inst, SIGNAL(portInfo(QString)), this, SLOT(setInfo(QString)));
    connect(protocol_inst, SIGNAL(portError(QString)), this, SLOT(setError(QString)));
}

Generator::~Generator()
{
    delete ui;
}

void Generator::portStart(void)
{
    if(protocol_inst->start(ui->CB_comList->currentText(),ui->CB_baudList->currentText().toUInt()))
    {
        setInfo("Device started.");
    }
    else
    {
Пример #22
0
void thermocouplePort::receivedMsg( QByteArray msg )
{
    if( msg.isNull() )
    {
        return;
    }
    msg.replace( 0x0A, "" ).replace( 0x0B, "" ).replace( 0x0D, "" )
            .replace( 0x11, "" ).replace( 0x13, "" )
            .replace( 0x3C, "" ).replace( 0x3E, "" ); // < and > - dont know why this is sent ...
    if( msg.isEmpty() )
    {
        return;
    }

    if( _expectedAnswer.size() == 0 )
    {
        if( _sendCounter != 0 )
        {
            return;
        }
        emit portError( "Unexpected answer!" );
        return;
    }
    _answerPending = 0;

    if( _expectedAnswer[0] == CMD_ID )
    {
        if( _idStringSet )
        {
            bool conversionSuccessful = false;
            int version = msg.toInt( &conversionSuccessful );
            if( conversionSuccessful && version > 90615 )
            {
                _probeTemperatureOnly = false;
            }
            _initValueCounter++;
            emit initSuccessful( _idString );
        }
        else
        {
            _idStringSet = true;
            _idString = msg;
            _initValueCounter++;
        }
    }
    else if( _expectedAnswer[0] == CMD_PROBE_TEMPERATURE )
    {
        bool conversionSuccessful = false;

        double probeTemperatureCelsius = msg.toDouble( &conversionSuccessful );

        if( conversionSuccessful )
        {
            if( _emitProbeTemperature )
            {
                if( probeTemperatureCelsius > MAX_TEMPERATURE
                        || probeTemperatureCelsius < -MAX_TEMPERATURE )
                {
                    emit portError( "Out of range!" );
                }
                else
                {
                    emit newProbeTemperature( probeTemperatureCelsius );
                }
            }
        }
        else
        {
            emit portError( "Could not get temperature from answer!" );
        }
    }
    else if( _expectedAnswer[0] == CMD_BOTH_TEMPERATURES )
    {
        bool conversion1Successful = false;
        bool conversion2Successful = false;

        double probeTemperatureFahrenheit = msg.left( msg.indexOf( "," ) )
                .toDouble( &conversion1Successful );
        double ambientTemperatureFahrenheit = msg.mid( msg.indexOf( "," ) + 1 )
                .toDouble( &conversion2Successful );

        if( conversion1Successful && conversion2Successful )
        {
            int tProbeCelsius = fahrenheitToCelsius(
                        probeTemperatureFahrenheit );
            int tAmbientCelsius = fahrenheitToCelsius(
                        ambientTemperatureFahrenheit );
            if( _emitProbeTemperature )
            {
                if( tProbeCelsius > MAX_TEMPERATURE
                        || tProbeCelsius < -MAX_TEMPERATURE )
                {
                    emit portError( "Out of range!" );
                }
                else
                {
                    emit newProbeTemperature( tProbeCelsius );
                }
            }
            if( _emitAmbientTemperature )
            {
                if( tAmbientCelsius > MAX_TEMPERATURE
                        || tAmbientCelsius < -MAX_TEMPERATURE )
                {
                    emit portError( "Out of range!" );
                }
                else
                {
                    emit newAmbientTemperature( tAmbientCelsius );
                }
            }
        }
        else
        {
            emit portError( "Could not get temperatures from answer!" );
        }
    }
    else if( _expectedAnswer[0] == CMD_OTHER )
    {
    }
    else
    {
        emit portError( "Undefined answer!" );
    }

    _expectedAnswer.erase( _expectedAnswer.begin() );
}
Пример #23
0
void eapsUta12Port::receivedMsg( QByteArray msg )
{
    unsigned char msgValues[MESSAGE_LENGTH];
    if( !checkAnswerFormat( msg, msgValues ) )
    {
        return;
    }

    switch( msgValues[3] )
    {
        case 0x1F:
        {
            answerSetVoltage();
            break;
        }
        case 0x2F:
        {
            answerGetVoltage( msgValues );
            break;
        }
        case 0x3F:
        {
            answerSetCurrent();
            break;
        }
        case 0x4F:
        {
            answerGetCurrent( msgValues );
            break;
        }
        case 0x5F:
        {
            answerSetStatus();
            break;
        }
        case 0x6F:
        {
            answerGetStatus( msgValues );
            break;
        }
        case 0x7F:
        {
            answerSetIdString();
            break;
        }
        case 0x8F:
        {
            answerGetIdString( msg );
            break;
        }
        case 0x9F:
        {
            answerEcho( msgValues );
            break;
        }
        case 0xAF:
        {
            answerError( msgValues );
            break;
        }
        case 0xEF:
        {
            answerSetIdn();
            break;
        }
        case 0xFF:
        {            
            answerGetIdn( msgValues );
            break;
        }
        case 0x48:
        {
            answerGetMaxValues( msgValues );
            break;
        }
        case 0xC8:
        {
            answerSetUserText();
            break;
        }
        case 0xD8:
        {
            answerGetUserText( msg );
            break;
        }
    default: emit portError( "Unknown answer!" );
            break;
    }
}
Пример #24
0
void eapsUta12Port::answerError( unsigned char* msgValues )
{
    emit portError( "Communication error! ("
                    + QString::number( msgValues[5] ) + ")" );
}