Hi
Könnte mir bitte jemand weiterhelfen.
Ich komm einfach nicht dahinter wie es richtig gemacht gehört.
Vielleicht hilft es ja wenn ich mal den ganzen Code poste:
test.pro
Code: Alles auswählen
TARGET = test2
TEMPLATE = app
SOURCES += main.cpp \
mainwindow.cpp \
readrs232.cpp
HEADERS += mainwindow.h \
readrs232.h
FORMS += mainwindow.ui
main.cpp
Code: Alles auswählen
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.h:
Code: Alles auswählen
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
protected:
void changeEvent(QEvent *e);
private:
Ui::MainWindow *ui;
void init_objects();
private slots:
void on_pushButton_clicked();
};
#endif // MAINWINDOW_H
mainwindow.cpp:
Code: Alles auswählen
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include "readrs232.h"
#include "readrs232.cpp"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
init_objects();
}
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::init_objects()
{
qDebug() << "test1";
}
void MainWindow::on_pushButton_clicked()
{
ReadRS232 MyReader();
}
readrs232.h:
Code: Alles auswählen
#ifndef READRS232_H
#define READRS232_H
#include <QObject>
#include <QDebug>
#include <QByteArray>
#include <abstractserial.h>
using namespace std;
class ReadRS232 : public QObject
{
Q_OBJECT
public:
ReadRS232(QObject *parent);
~ReadRS232();
void ReadRS232open();
AbstractSerial *MyDevice;
int readBytes;
private slots:
void slotRead();
};
#endif // READRS232_H
readrs232.cpp
Code: Alles auswählen
#include "readrs232.h"
#include <abstractserial.h>
static void printDataToHex(const QByteArray &data)
{
QByteArray baTmp;
baTmp.clear();
#if QT_VERSION >= 0x040300
baTmp = (data.toHex()).toUpper();
#else
quint8 n = 0;
for (int i=0;i<data.size();i++) {
n = data.at(i);
if ((n >= 0) && (n <= 15)) baTmp.append(QByteArray::number(0, 16).toUpper());
baTmp.append(QByteArray::number(n, 16).toUpper());
}
#endif
for (int i=0;i<baTmp.size();i+=2) {
qDebug() << "[" << baTmp.at(i) << baTmp.at(i + 1) << "]";
}
qDebug() << endl;
}
ReadRS232::ReadRS232(QObject *parent = 0)
: QObject(parent)
{
}
void ReadRS232::ReadRS232open()
{
MyDevice = new AbstractSerial();
connect(MyDevice, SIGNAL(readyRead()), this, SLOT(slotRead()));
MyDevice->setDeviceName("COM6");
MyDevice->setBaudRate(AbstractSerial::BaudRate128000);
//MyDevice->setBaudRate(AbstractSerial::BaudRate115200);
if (!MyDevice->open(AbstractSerial::ReadOnly)) {
qDebug() << "Serial device by default: " << MyDevice->deviceName() << " open fail.";
return;
}
if (!MyDevice->setDataBits(AbstractSerial::DataBits8)) {
qDebug() << "Set data bits " << AbstractSerial::DataBits8 << " error.";
return;
}
if (!MyDevice->setParity(AbstractSerial::ParityNone)) {
qDebug() << "Set parity " << AbstractSerial::ParityNone << " error.";
return;
}
if (!MyDevice->setStopBits(AbstractSerial::StopBits1)) {
qDebug() << "Set stop bits " << AbstractSerial::StopBits1 << " error.";
return;
}
if (!MyDevice->setFlowControl(AbstractSerial::FlowControlOff)) {
qDebug() << "Set flow " << AbstractSerial::FlowControlOff << " error.";
return;
}
qDebug() << "= Default parameters =";
qDebug() << "Device name : " << MyDevice->deviceName();
qDebug() << "Baud rate : " << MyDevice->baudRate();
qDebug() << "Data bits : " << MyDevice->dataBits();
qDebug() << "Parity : " << MyDevice->parity();
qDebug() << "Stop bits : " << MyDevice->stopBits();
qDebug() << "Flow : " << MyDevice->flowControl();
qDebug() << "Char timeout, msec : " << MyDevice->charIntervalTimeout();
readBytes = 5;
}
ReadRS232::~ReadRS232() {
MyDevice->close();
delete MyDevice;
MyDevice = 0;
}
void ReadRS232::slotRead() {
QByteArray ba = MyDevice->read(readBytes);
qDebug() << "Rx : \n";
printDataToHex(ba);
qDebug() << " \n Readed is : " << ba.size() << " bytes";
}
abstractserial.h und zugehörige Daten liegen in einem eigenen Ordner"qserialdevice"
Momentan bekomme ich eine Menge Fehlermeldungen wie diese hier:
debug/mainwindow.o:mainwindow.cpp:

error: undefined reference to `AbstractSerial::AbstractSerial(QObject*)'
bedeutet wohl dass die AbstractSerial.h nicht korrekt included ist oder?
Ist eigentlich ReadRS232 MyReader(); das selbe wie MyReader = new ReadRS232();? Wenn ich das umschreibe kommt die Meldung: H:/tricopter/qt/test2/test2/mainwindow.cpp:39: error: 'MyReader' was not declared in this scope
Ich blick da einfach nicht durch -.-
Großes Dankeschön für Eure Mühe
lg Lukas