B
Hallo Michael,
danke für deine Anregungen.
Ich habe mir die Klassenerzeugung nochmal angesehen und noch ein paar Fehler gefunden.
Jetzt funktioniert es soweit - da sind sicherlich noch ein paar Dinge drin die ein Profi so nicht machen würde, aber immer mit der Ruhe.
main.h
#ifndef MAIN_H
#define MAIN_H
#define MY_API extern "C" __declspec(dllexport)
#include <QCoreApplication>
#include <QDebug>
#include "serial.h"
#include "protocol.h"
MY_API int main(int argc, char *argv[]);
#endif // MAIN_H
main.c
#include "main.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
int i;
Seriel messgeraet;
i = messgeraet.AskHardware();
a.exec();
qDebug() << "/////////////////////////";
qDebug() << "zurück zu Main";
Protocol::Messwerte mw;
mw = messgeraet.GetMeasValue();
qDebug() << "DX:" << mw.d_x;
qDebug() << "DY:" << mw.d_y;
qDebug() << "STATUS_X:" << mw.status_x;
qDebug() << "STATUS_Y:" << mw.status_y;
qDebug() << "/////////////////////////";
return i;
}
console.h
#ifndef CONSOLE_H
#define CONSOLE_H
#include <QString>
#include <QObject>
#include <QDebug>
#include <QCoreApplication>
#include <boost/circular_buffer.hpp>
#include "protocol.h"
class Console: public QObject
{
Q_OBJECT
public:
explicit Console(QObject *parent = 0);
~Console();
signals:
void getData(const QByteArray &data);
public:
void putData(const QByteArray &data, Protocol* protocol);
void setLocalEchoEnabled(bool set);
int bcc_calc(QString data);
public slots:
void GMD();
private:
bool localEchoEnabled;
int cnt_console = 0;
boost::circular_buffer<char> buffer;
QString outputString;
QString BCC;
int stx_active = 0;
int etx_active = 1;
int read_bcc = 0;
};
#endif // CONSOLE_H
console.cpp
#include "console.h"
typedef boost::circular_buffer<char> CircularBuffer;
Console::Console(QObject *parent) : QObject(parent)
{
buffer.set_capacity(100);
}
Console::~Console()
{
buffer.clear();
}
void Console::putData(const QByteArray &data, Protocol* protocol)
{
//qDebug() << "Antwort: " << QString(data);
//qDebug() << "Größe von Data: " << data.size();
// Daten in den Buffer schreiben
for (QByteArray::const_iterator it=data.begin(); it != data.end(); ++it)
{
char aa = *it;
buffer.push_back(aa);
}
QString Input;
Input.append(buffer[0]);
QString Test = QString::fromLatin1(Input.toLatin1().toHex());
// STX = Anfang finden
if (stx_active == 0){
while (Test != "02") {
buffer.pop_front();
Input = (buffer[0]);
Test = (QString::fromLatin1(Input.toLatin1().toHex()));
}
}
if (Test == "02") {
stx_active = 1;
}
// cnt sorg dafür, dass der buffer nicht leerläuft
int cnt = buffer.size();
while (Test != "03" && cnt > 1) {
--cnt;
// 2. inkl ETX
buffer.pop_front();
outputString.append(buffer[0]);
Input = (buffer[0]);
Test = (QString::fromLatin1(Input.toLatin1().toHex()));
}
// ETX finden
if (Test == "03") {
stx_active = 0;
buffer.pop_front();
Input = (buffer[0]);
BCC = (QString::fromLatin1(Input.toLatin1().toHex()));
buffer.pop_front();
bool ok;
// BCC berechnen
if (BCC.toInt(&ok, 16) == bcc_calc(outputString)) {
// Datenstream per Protokoll auslesen
protocol->GMD(outputString);
// QT Application beenden
QCoreApplication::quit();
}
} else {
BCC.clear();
}
if (stx_active == 0) {
outputString.clear();
}
}
void Console::GMD() {
QByteArray msg;
// GMD 02 47 4D 44 03 4D
msg.append(0x02);
msg.append(0x47);
msg.append(0x4D);
msg.append(0x44);
msg.append(0x03);
quint8 bcc = bcc_calc(msg.mid(1,4));
msg.append(bcc);
// Feuere anfrage an RS232
emit getData(msg);
}
int Console::bcc_calc(QString data) {
int bcc=0;
bool ok;
for (QString::iterator it= data.begin(); it != data.end(); ++it)
{
QString tmp1, tmp2;
tmp1.append(*it);
tmp2 = QString::fromLatin1(tmp1.toLatin1().toHex());
bcc ^= tmp2.toInt(&ok, 16);
}
if (bcc < 0x20) {
bcc += 0x20;
}
return bcc;
}
protocol.h
#ifndef PROTOCOL_H
#define PROTOCOL_H
#include <QPlainTextEdit>
#include <QString>
#include <QDebug>
#include <vector>
class Protocol
{
public:
Protocol();
~Protocol();
struct Messwerte {
int status_x;
int status_y;
QString d_x;
QString d_y;
};
void GMD(QString data);
Messwerte Protocol::getMesswert();
private:
Messwerte currentMW;
std::vector<Messwerte> hardware_messwerte;
static void updateMesswerte(Messwerte& mw, QString dx, QString dy, int status_x,int status_y);
QString status_xx ;
};
#endif // PROTOCOL_H
protocol.cpp
#include "protocol.h"
Protocol::Protocol() { }
Protocol::~Protocol() { }
void Protocol::GMD(QString data){
qDebug() << "Befehl" << data.mid(0,3);
qDebug() << "Pos X" << data.mid(3,8);
qDebug() << "Dim X" << data.mid(11,7);
qDebug() << "Status X" << data.mid(18,3);
qDebug() << "Pos Y" << data.mid(32,8);
qDebug() << "Dim Y" << data.mid(40,7);
qDebug() << "Status Y" << data.mid(47,3);
Messwerte mw;
updateMesswerte(mw, data.mid(03,8), data.mid(32,8), data.mid(18,3).toInt(), data.mid(47,3).toInt());
hardware_messwerte.push_back(mw);
return;
}
void Protocol::updateMesswerte(Messwerte& mw, QString dx, QString dy, int status_x, int status_y)
{
mw.d_x = dx;
mw.d_y = dy;
mw.status_x = status_x;
mw.status_y = status_y;
}
Protocol::Messwerte Protocol::getMesswert() {
Messwerte mw;
mw = hardware_messwerte.front();
return mw;
}
serial.h
#ifndef SERIEL_H
#define SERIEL_H
#include <QtCore/QtGlobal>
#include <QtSerialPort/QSerialPort>
#include "protocol.h"
class Console;
class Settings;
class Protocol;
class Seriel: public QObject
{
Q_OBJECT
signals:
void getData(const QByteArray &data);
public:
explicit Seriel(QObject *parent = 0);
~Seriel();
Protocol::Messwerte GetMeasValue(void);
private slots:
void writeData(const QByteArray &data);
public slots:
void openSerialPort();
void closeSerialPort();
void readData();
void handleError(QSerialPort::SerialPortError error);
int AskHardware(void);
private:
Console *console;
QSerialPort *serial;
Settings *setting;
Protocol *protocol;
};
#endif // SERIEL_H
serial.cpp
#include <QtSerialPort/QSerialPort>
#include <QDebug>
#include "console.h"
#include "settings.h"
#include "protocol.h"
#include "serial.h"
#include "protocol.h"
Seriel::Seriel(QObject *parent) : QObject(parent)
{
console = new Console;
serial = new QSerialPort(this);
protocol = new Protocol;
connect(serial, SIGNAL(error(QSerialPort::SerialPortError)), this,
SLOT(handleError(QSerialPort::SerialPortError)));
connect(serial, SIGNAL(readyRead()), this, SLOT(readData()));
connect(console, SIGNAL(getData(QByteArray)), this, SLOT(writeData(QByteArray)));
}
Seriel::~Seriel(){
delete console;
delete serial;
delete protocol;
}
void Seriel::writeData(const QByteArray &data)
{
serial->write(data);
qDebug() << "written" << serial->bytesToWrite();
}
void Seriel::openSerialPort()
{
setting = new Settings;
Settings::SettingsCom p = setting->localsettings();
setting->updateSettings();
p = setting->localsettings();
p.name = "COM2";
p = setting->localsettings();
qDebug() << "p.name" << p.name;
serial->setPortName(p.name);
serial->setBaudRate(p.baudRate);
serial->setDataBits(p.dataBits);
serial->setParity(p.parity);
serial->setStopBits(p.stopBits);
serial->setFlowControl(p.flowControl);
if (serial->open(QIODevice::ReadWrite)) {
qDebug() << "serial connected";
} else {
qDebug() << "Fehler" << serial->errorString();
}
}
void Seriel::closeSerialPort()
{
serial->close();
}
void Seriel::readData()
{
QByteArray data = serial->readLine();
console->putData(data, protocol);
}
void Seriel::handleError(QSerialPort::SerialPortError error)
{
if (error == QSerialPort::ResourceError) {
qDebug() << "handle Error:" << serial->errorString();
closeSerialPort();
}
}
Protocol::Messwerte Seriel::GetMeasValue(void) {
Protocol::Messwerte mw;
mw = protocol->getMesswert();
return mw;
}
int Seriel::AskHardware(void) {
int i=0;
openSerialPort();
console->GMD();
if ( serial->isOpen()) {
i++;
}
return i;
}
settings.h
#ifndef SETTINGS_H
#define SETTINGS_H
#include <QObject>
#include <QtSerialPort/QSerialPortInfo>
#include <QtSerialPort>
#include <QSerialPort>
class Settings
{
public:
Settings();
~Settings();
public:
struct SettingsCom {
QString name;
qint32 baudRate;
QString stringBaudRate;
QSerialPort::DataBits dataBits;
QString stringDataBits;
QSerialPort::Parity parity;
QString stringParity;
QSerialPort::StopBits stopBits;
QString stringStopBits;
QSerialPort::FlowControl flowControl;
QString stringFlowControl;
bool localEchoEnabled;
};
SettingsCom localsettings() const;
void updateSettings(void);
private:
SettingsCom currentSettings;
};
#endif // SETTINGS_H
settings.cpp
#include "settings.h"
Settings::Settings()
{
currentSettings.name = "COM2";
currentSettings.baudRate = QSerialPort::Baud115200;
currentSettings.dataBits = QSerialPort::Data8;
currentSettings.parity = QSerialPort::NoParity;
currentSettings.stopBits = QSerialPort::OneStop;
currentSettings.flowControl = QSerialPort::NoFlowControl;
}
Settings::~Settings()
{
}
void Settings::updateSettings(void) {
currentSettings.name = "COM4";
}
Settings::SettingsCom Settings::localsettings() const
{
return currentSettings;
}