-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcandatabank.cpp
140 lines (120 loc) · 3.92 KB
/
candatabank.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "candatabank.h"
#include "ui_candatabank.h"
#include <QDebug>
#include <QSqlError>
CANdatabank::CANdatabank(QWidget *parent) :
QWidget(parent),
ui(new Ui::CANdatabank)
{
ui->setupUi(this);
}
CANdatabank::~CANdatabank()
{
mydb.commit();
mydb.close();
delete ui;
}
void CANdatabank::DBC_browsefile()
{
fileName = QFileDialog::getOpenFileName(this, tr("Open CAN data bank file"),"/home",tr("sqlite (*.sqlite)"));
fileName;
mydb.setDatabaseName(fileName);
if(!mydb.open()) { QMessageBox::warning(this,tr("ERROR with DataBase"),tr("CAN data bank don't work"));
OKdataBankFlag=-1; }
else {
mydb.transaction();
OKdataBankFlag =1;
}
//readfileDB();
}
quint16 CANdatabank::BitsToUINT16(quint8 bitstart, quint8 bitend, quint64 data)
{
quint8 bits[64];
// Convert from quint64 to Bit Array
for(int i=0; i<64; ++i){
int f=i; bool bit = (data&(1<<(63-i)));
if(bit) bits[f]=1; else bits[f]=0;
}
quint16 dec=0;
for(int i=bitstart; i<bitend+1;i++) {
//dec += pow(2,i)*bits[i];
dec |= ((bits[i])<<i);
}
return dec;
}
QString CANdatabank::CANFrameName(quint32 CANid)
{
QString string;
QString CANframeName=string.setNum(CANid,16).toUpper();
QSqlQuery query(mydb);
//db queries "+temp[0].toString()+"
query.prepare("SELECT name FROM CAN_Frame WHERE CANID='"+string.setNum(CANid)+"' ");
//query.bindValue(":id", 1001);
//query.bindValue(":name", "first_name");
//query.bindValue(":CANID", 150);
if( !query.exec() )
qDebug() << query.lastError();
else
qDebug() << "Table created!";
while (query.next()) {
CANframeName =query.value(0).toString();
}
return CANframeName;
}
QVector<QStringList> CANdatabank::CANSignalList(quint32 CANid, quint64 data, quint8 dlc)
{
QString string;
QString CANframeName=string.setNum(CANid,16);
QVector<QStringList> temporary; temporary.reserve(20);
QSqlQuery query(mydb);
QStringList results,emptylist; emptylist<<" ";
bool flag=false;
query.prepare("SELECT * FROM Signals WHERE CANID='"+string.setNum(CANid)+"' ");
if( !query.exec() )
qDebug() << query.lastError();
else
qDebug() << "Table created!";
while (query.next()) {
flag=true;
//******* DECLARATION
float offset,faktor; quint16 bitofstart,bitofend; QString value; bool text_values;
//******Store the infos from DB to variables
results << query.value(1).toString(); //Name of signal in DB
faktor=query.value(2).toFloat();
offset=query.value(3).toFloat();
bitofstart=query.value(4).toUInt();
bitofend=query.value(5).toUInt();
value=query.value(6).toString();
text_values=query.value(7).toBool();
//**************************//
//******Execute the function for the Data value
if(text_values) {/*psaxe xana sthn DB gia ta values */}
else { results.append(QString::number(BitsToUINT16(bitofstart,bitofend,data)));/* return the bitnumber*/ }
results << value;
temporary.push_back(results);
}
if(flag) return temporary;
else temporary.push_back(emptylist); return temporary;
}
int CANdatabank::returnOKdataBankFlag()
{
return OKdataBankFlag;
}
quint32 CANdatabank::returnCANIdfromName(QString name)
{
quint32 result=0;
QSqlQuery query(mydb);
//db queries "+temp[0].toString()+"
query.prepare("SELECT CANID FROM CAN_Frame WHERE name='"+name+"' ");
//query.bindValue(":id", 1001);
//query.bindValue(":name", "first_name");
//query.bindValue(":CANID", 150);
if( !query.exec() )
qDebug() << query.lastError();
else
qDebug() << "Table created!";
while (query.next()) {
result =query.value(0).toUInt();
}
return result;
}