-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmainwindow.cpp
183 lines (154 loc) · 4.9 KB
/
mainwindow.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "c_crc.h"
#include <QDebug>
#include <QMessageBox>
#include <QUuid>
#include <QFile>
#include <QTextStream>
#include <QScriptEngine>
#include <QScriptValue>
#include <QScriptValueList>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(&this->m_socket,&CTcpSock::sig_recvData,&this->m_TCP_master,&C_MB_TCP_MASTER::slot_recvData);
connect(&this->m_TCP_master,&C_MB_TCP_MASTER::sig_sendData,&this->m_socket,&CTcpSock::sendSockData);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
serialCFG cfg;
cfg.comName = "COM12";
cfg.baudRate = QSerialPort::Baud9600;
cfg.dataBits = QSerialPort::Data8;
cfg.stopBits = QSerialPort::OneStop;
cfg.parity = QSerialPort::NoParity;
cfg.flowControl = QSerialPort::NoFlowControl;
MBRequestTransInfo trans;
trans.trans.slaveAdr = 0x08;
trans.trans.funcCode = MB_func03;
trans.trans.beginAdr =0x0001;
trans.trans.paraSum = 0x01;
trans.timeGap = 5;
this->m_RTUmasterNode.AddTrans(trans);
trans.trans.slaveAdr = 0x01;
trans.trans.funcCode = MB_func03;
trans.trans.beginAdr =0x0000;
trans.trans.paraSum = 0x01;
trans.timeGap = 10;
this->m_RTUmasterNode.AddTrans(trans);
this->m_RTUmasterNode.startServ(cfg);
}
void MainWindow::on_pushButton_2_clicked()
{
}
void MainWindow::on_pushButton_3_clicked()
{
QByteArray array;
array.append((char)0x08);
array.append((char)0x83);
array.append((char)0x03);
// array.append((char)0x81);
qDebug()<<C_CRC::crc16((quint8*)array.data(),array.size());
}
void MainWindow::on_pushButton_4_clicked()
{
//this->m_RTU_master.setSlaveAdr(0x08);
//this->m_RTU_master.setTimeOut(3000);
//this->m_RTU_master.queryCMD(func_01 ,0X0001,8); //
}
void MainWindow::on_pushButton_5_clicked()
{
QByteArray array;
array.append((char)0x81);
short t1 = 0x8001;
unsigned short t2 =0x8001;
qDebug()<<t1<<t2<<(quint8)array.at(0)<<(t1==t2);
}
void MainWindow::on_pushButton_6_clicked()
{
//this->m_TCP_master.setSlaveAdr(0x08);
//this->m_TCP_master.queryCMD(MB_func03,0X0001,2,3000); //
}
void MainWindow::on_pushButton_7_clicked()
{
this->m_socket.connectSocket("127.0.0.1",9100);
}
void MainWindow::on_pushButton_8_clicked()
{
for(int i=0;i<1000;i++)
{
QUuid ID = QUuid::createUuid();
qDebug()<< ID.toString();
}
}
void MainWindow::on_pushButton_9_clicked()
{
QString result = "";
QFile scriptFile("TransCal.js");
if (!scriptFile.open(QIODevice::ReadOnly))
{
result.clear();
qWarning() << "encodePass.js open failed";
return;
}
QTextStream out(&scriptFile);
QString contents = out.readAll();
scriptFile.close();
qWarning()<<"OK";
contents.clear();
contents = "function1 trans2wCal(var1,var2)\r\n\
{\r\n\
var MAX =200;\r\n\
var res = var1+var2;\r\n\
if(res>MAX)\r\n\
return 0;\r\n\
return res;\r\n\
}\r\n\
function trans3wCal(var1,var2)\r\n\
{\r\n\
return var1*var2+5;\r\n\
}";
ui->textEdit->setText(contents);
QScriptSyntaxCheckResult res =QScriptEngine::checkSyntax (contents);
qWarning()<<"语法检查:"<<res.state();
QTextCursor cursor(ui->textEdit->textCursor());//
cursor.setPosition(res.errorLineNumber());
if(0==res.state())
{
qWarning()<<"错误行:"<<res.errorLineNumber()<<"错误列:"<<res.errorColumnNumber();
qWarning()<<"错误提示:"<<res.errorMessage();
return;
}
double var1 =100.5; // ui->lineEdit_var1->text().toDouble();
double var2 =200.6; // ui->lineEdit_var2->text().toDouble();
qDebug() << "var1:" << var1 << "var2:" << var2;
QScriptValueList args; //调用js方法时传入的参数
args << QScriptValue(var1) << QScriptValue(var2);
QScriptEngine engine;
QScriptValue js = engine.evaluate(contents); //个人理解:加载js文本到操作引擎
// js.isError()
QScriptValue func;
func = engine.globalObject().property("trans2wCal"); //调用js方法
double ret = func.call(QScriptValue(), args).toVariant().toDouble();
qDebug() << "result:" << ret;
// ui->lineEdit_sub->setText(result);
func = engine.globalObject().property("trans3wCal"); //调用js方法
result = func.call(QScriptValue(), args).toString();
qDebug() << "result:" << result;
// ui->lineEdit_mul->setText(result);
}
void MainWindow::on_pushButton_10_clicked()
{
quint8 t1= 0Xf0;
qint8 t2= 0Xf0;
double db= t1;
qDebug()<<(int)(t1*1+0)<<(uint)(t1*1+0);
qDebug()<<(int)(t2*1+0)<<(uint)(t2*(-1)+0);
}