Ich hoffe ihr könnt mir weiterhelfen.
Erstmal die Umgebung
Windows XP aktuelle Updates
Qt aktuelle Version
QextserialPort 1.2 und 1.1
Mit folgender Code funktioniert einfach nicht so wie er soll. Die Variable erg wird nicht inkrementiert obwohl sie das theoretisch mit ablauf des Porttimers machen sollte. Der Port kann nicht per Hyperterminal angesprochen werden, daher ist er scheinbar korrekt vom Programm erkannt und auch belegt.
Hier der Code
serialport.cpp
Code: Alles auswählen
#include <serialport.h>
//******************************************************************************
SerialPort::SerialPort(QWidget *parent)
{
erg=0;
timerPort=new QTimer(this);
connect(timerPort, SIGNAL(timeout()), this, SLOT(fetchData()));
serialPort = new QextSerialPort();
}
//******************************************************************************
void SerialPort::changeParas(QString &name, PortSettings ¶)
{
serialPort->setPortName(name);
serialPort->setBaudRate(para.BaudRate);
serialPort->setFlowControl(FLOW_OFF);
serialPort->setParity(PAR_NONE);
serialPort->setDataBits(DATA_8);
serialPort->setStopBits(STOP_1);
}
//******************************************************************************
void SerialPort::openPort()
{
serialPort->open(QIODevice::ReadWrite);
timerPort->start(30);
emit(portOpened());
}
//******************************************************************************
void SerialPort::closePort()
{
serialPort->close();
timerPort->stop();
emit(portClosed());
}
//******************************************************************************
void SerialPort::fetchData(void)
{
char buff[1024];
int numBytes;
erg++;
numBytes = serialPort->bytesAvailable();
if(numBytes > 0)
{
if(numBytes > 1024) numBytes = 1024;
while(serialPort->bytesAvailable()>12){ //readLine macht nicht exklusiv beim \n Stopp
serialPort->readLine(buff, numBytes);
QString msg = buff;
erg++;
if(msg=="t"){
erg++;
}
}
}
emit(dataReady());
}
//******************************************************************************
double SerialPort::giveValue()
{
return erg;
}
//******************************************************************************
void SerialPort::clearerg()
{
erg=0;
}
//******************************************************************************
void SerialPort::sendData(QByteArray befehl)
{
if(serialPort->isOpen()){
serialPort->write(befehl, 1); //Steuerung soll nur ein Char sein
}
}
//******************************************************************************
Hier die Headerdatei serialport.h
Code: Alles auswählen
#ifndef SERIALPORT_H
#define SERIALPORT_H
#include <qextserialport.h>
#include <QTimer.h>
class SerialPort : public QextSerialPort
{
Q_OBJECT
private:
QextSerialPort* serialPort;
QTimer *timerPort;
double erg;
public:
SerialPort(QWidget *parent = 0);
void changeParas(QString &name, PortSettings ¶);
double giveValue(void);
void sendData(QByteArray befehl);
void clearerg(void);
private slots:
void fetchData(void);
public slots:
void openPort();
void closePort();
signals:
void portOpened();
void portClosed();
void dataReady();
};
#endif
kartgui.cpp
Code: Alles auswählen
#include "kartgui.h"
#include "ui_kartgui.h"
#include "SerialPort.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
timerLCD=new QTimer(this);
timerZZP=new QTimer(this);
timerRPM=new QTimer(this);
port=new SerialPort();
verbunden = "Verbindung wurde hergestellt";
getrennt = "Verbindung wurde getrennt";
connect(timerLCD, SIGNAL(timeout()), this, SLOT(updatedrehzahl_LCD()));
connect(timerZZP, SIGNAL(timeout()), this, SLOT(updatezzp_LCD()));
connect(timerZZP, SIGNAL(timeout()), this, SLOT(updatevorz_LCD()));
connect(timerRPM, SIGNAL(timeout()), this, SLOT(drehzahl()));
connect(ui->pushButtonTrennen, SIGNAL(clicked(bool)), this, SLOT(trennenButtonClicked()));
connect(ui->pushButtonVerbinden, SIGNAL(clicked(bool)), this, SLOT(verbindenButtonClicked()));
connect(ui->spinBox, SIGNAL(valueChanged(int)), this, SLOT(maxdrehzahl()));
connect(port, SIGNAL(portOpened()), this, SLOT(connected()));
connect(port, SIGNAL(portClosed()), this, SLOT(disconnected()));
maxrpm=ui->spinBox->value()+100;
ui->progressBar->setMaximum(maxrpm);
ui->progressBar->setMinimum(0);
ui->statusBar->showMessage(getrennt);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
void MainWindow::updatedrehzahl_LCD(void)
{
ui->drehzahlLCD->display(rpm);
ui->progressBar->setValue(rpm);
}
void MainWindow::updatezzp_LCD(void)
{
}
void MainWindow::updatevorz_LCD(void)
{
}
void MainWindow::verbindenButtonClicked(void)
{
ui->drehzahlLCD->display(rpm);
ui->progressBar->setValue(rpm);
timerRPM->start(100);
timerLCD->start(13);
timerZZP->start(500);
port->closePort();
PortSettings para;
QString name;
name=ui->comboBox->currentText();
switch(ui->comboBox_2->currentIndex())
{
case 0:
para.BaudRate=(BAUD300);
break;
case 1:
para.BaudRate=(BAUD600);
break;
case 2:
para.BaudRate=(BAUD1200);
break;
case 3:
para.BaudRate=(BAUD2400);
break;
case 4:
para.BaudRate=(BAUD4800);
break;
case 5:
para.BaudRate=(BAUD9600);
break;
case 6:
para.BaudRate=(BAUD14400);
break;
case 7:
para.BaudRate=(BAUD19200);
break;
case 8:
para.BaudRate=(BAUD38400);
break;
case 9:
para.BaudRate=(BAUD56000);
break;
case 10:
para.BaudRate=(BAUD57600);
break;
case 11:
para.BaudRate=(BAUD76800);
break;
case 12:
para.BaudRate=(BAUD115200);
break;
case 13:
para.BaudRate=(BAUD128000);
break;
case 14:
para.BaudRate=(BAUD256000);
}
para.Timeout_Millisec=100;
para.Timeout_Sec=0;
port->changeParas(name, para);
port->openPort();
}
void MainWindow::trennenButtonClicked(void)
{
rpm=0;
timerRPM->stop();
port->closePort();
}
void MainWindow::connected()
{
ui->statusBar->showMessage(verbunden);
}
void MainWindow::disconnected()
{
ui->statusBar->showMessage(getrennt);
}
void MainWindow::maxdrehzahl(void)
{
maxrpm=ui->spinBox->value();
ui->progressBar->setMaximum(maxrpm);
ui->progressBar->reset();
}
void MainWindow::drehzahl(void)
{
int zzp_wert,vorz=125;
float zzp;
double tick;
zzp=rpm*3/400;
vorz=vorz/5;
zzp_wert=vorz+(zzp/5);
tick=port->giveValue();
rpm=tick*114.285714286;
//port->clearerg();
ui->vorzuendungLCD->display(vorz);
ui->zzpLCD->display(vorz+(zzp/5));
}
Theretisch sollte ja jetzt nach dem connecten die Funktion fetchdata alle paar ms ausgeführt werden und der angezeigte Wert in der Progressbar und dem Drehzahl LCD sollte sich verändern.
Leider passiert das aber nur wenn ERG direkt in givevalue gesetzt wird ( return erg+10)
Nicht aber über den Timer ( erg++ in fetchData)
Wie kommt es dazu?
Ich hoffe ihr könnt mir helfen.
mfg Manax