-
Notifications
You must be signed in to change notification settings - Fork 3
/
ads.cpp
192 lines (154 loc) · 5.93 KB
/
ads.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
184
185
186
187
188
189
190
191
192
#include "ads.h"
Operation::Operation(QObject* parent): QObject (parent)
{
pAddr = &Addr;
nPort = AdsPortOpen();
nErr = AdsGetLocalAddress(pAddr);
pAddr->port = 851;
mValueStruct = new vStruct[6];
for (int i = 0; i <= 0; i++) {
mValueStruct[i].position = 0;
mValueStruct[i].speed = 0;
}
}
Operation::~Operation()
{
delete [] mValueStruct;
}
void Operation::setStatus(short axis, short status)
{
unsigned long lHdlVar;
char szVar1[] = { "GVL.axisNum" };
char szVar2[] = { "MAIN.CurrentJob" };
// 写入坐标轴
if (axis > -1) {
nErr = AdsSyncReadWriteReq(pAddr, ADSIGRP_SYM_HNDBYNAME, 0x0, sizeof(lHdlVar),
&lHdlVar, sizeof(szVar1), szVar1);
nErr = AdsSyncWriteReq(pAddr, ADSIGRP_SYM_VALBYHND, lHdlVar, sizeof(axis), &axis);
}
// 写入状态机
nErr = AdsSyncReadWriteReq(pAddr, ADSIGRP_SYM_HNDBYNAME, 0x0, sizeof(lHdlVar),
&lHdlVar, sizeof(szVar2), szVar2);
nErr = AdsSyncWriteReq(pAddr, ADSIGRP_SYM_VALBYHND, lHdlVar, sizeof(status), &status);
}
/* setSpeed => 设置轴的转动速度
* axisNumber: 轴编号 (-1: 表示全部轴)
* value: 设定值
*/
void Operation::setSpeed(QString axisNumber, double value)
{
unsigned long lHdlVar;
QString tmp = "GVL.AxisMoveSpeed[" + axisNumber + "]";
char* targetRegister = tmp.toLocal8Bit().data();
nErr = AdsSyncReadWriteReq(pAddr, ADSIGRP_SYM_HNDBYNAME, 0x0, sizeof(lHdlVar),
&lHdlVar, unsigned(tmp.length()+1), targetRegister);
nErr = AdsSyncWriteReq(pAddr, ADSIGRP_SYM_VALBYHND, lHdlVar,
sizeof(value), &value);
}
void Operation::readSpeed(int* value)
{
unsigned long lHdlVar;
char targetRegister[] = "GVL.AxisMoveSpeed";
double tmp[6];
nErr = AdsSyncReadWriteReq(pAddr, ADSIGRP_SYM_HNDBYNAME, 0x0, sizeof(lHdlVar),
&lHdlVar, sizeof(targetRegister), targetRegister);
nErr = AdsSyncReadReq(pAddr, ADSIGRP_SYM_VALBYHND, lHdlVar,
sizeof(tmp[0])*6, tmp);
for (int i = 0; i < 6; ++i) {
value[i] = int(tmp[i]);
}
}
void Operation::readStatus()
{
unsigned long lHdlVar;
// 读取第轴的位移
for (int i = 0; i < 6; i++) {
QString tmp = "GVL.Axis[" + QString::number(i) +"].NcToPlc.ActPos";
char* szVar = tmp.toUtf8().data();
unsigned long tSize = unsigned(tmp.length()+1);
nErr = AdsSyncReadWriteReq(pAddr, ADSIGRP_SYM_HNDBYNAME, 0x0, sizeof(lHdlVar),
&lHdlVar, tSize, szVar);
nErr = AdsSyncReadReq(pAddr, ADSIGRP_SYM_VALBYHND, lHdlVar, sizeof(double), &mValueStruct[i].position);
}
// 读取第轴的速度
// 缺少一个错误机制
for (int i = 0; i < 6; i++) {
QString tmp = "GVL.Axis[" + QString::number(i) +"].NcToPlc.ActVelo";
char* szVar = tmp.toUtf8().data();
unsigned long tSize = unsigned(tmp.length()+1);
nErr = AdsSyncReadWriteReq(pAddr, ADSIGRP_SYM_HNDBYNAME, 0x0, sizeof(lHdlVar),
&lHdlVar, tSize, szVar);
nErr = AdsSyncReadReq(pAddr, ADSIGRP_SYM_VALBYHND, lHdlVar, sizeof(double), &mValueStruct[i].speed);
}
// 读取状态机
char targetRegister[] = "MAIN.CurrentJobState";
char tmpCommand[80];
nErr = AdsSyncReadWriteReq(pAddr, ADSIGRP_SYM_HNDBYNAME, 0x0, sizeof(lHdlVar),
&lHdlVar, sizeof(targetRegister), targetRegister);
nErr = AdsSyncReadReq(pAddr, ADSIGRP_SYM_VALBYHND, lHdlVar,
sizeof(tmpCommand), &tmpCommand);
QString resCommand = tmpCommand;
emit setUiStatus(resCommand);
emit setValue(mValueStruct);
}
// 设置开关状态
void Operation::setSwitch(bool btnSwitch)
{
unsigned long lHdlVar;
char targetRegister[] = "MAIN.POWER";
nErr = AdsSyncReadWriteReq(pAddr, ADSIGRP_SYM_HNDBYNAME, 0x0, sizeof(lHdlVar),
&lHdlVar, sizeof(targetRegister), targetRegister);
nErr = AdsSyncWriteReq(pAddr, ADSIGRP_SYM_VALBYHND, lHdlVar,
sizeof(btnSwitch), &btnSwitch);
}
void Operation::setAPosition(double* value)
{
unsigned long lHdlVar;
char targetRegister[] = "GVL.MoveA";
nErr = AdsSyncReadWriteReq(pAddr, ADSIGRP_SYM_HNDBYNAME, 0x0, sizeof(lHdlVar),
&lHdlVar, sizeof(targetRegister), targetRegister);
nErr = AdsSyncWriteReq(pAddr, ADSIGRP_SYM_VALBYHND, lHdlVar,
sizeof(value[0])*6, value);
}
/* 名称:设置移动的距离
* 描述:设置本次移动距上次移动终点的距离(x,y,z)
* 该函数还未经测试
*/
void Operation::setOutMovePos(QVector<double> value)
{
unsigned long lHdlVar;
for (auto i = 0; i < value.size(); ++i) {
QString tmp = "GVL.OutMovePos[" + QString::number(i) + "]";
char* targetRegister = tmp.toLocal8Bit().data();
nErr = AdsSyncReadWriteReq(pAddr, ADSIGRP_SYM_HNDBYNAME, 0x0, sizeof(lHdlVar),
&lHdlVar, static_cast<unsigned>(tmp.length()+1), targetRegister);
nErr = AdsSyncWriteReq(pAddr, ADSIGRP_SYM_VALBYHND, lHdlVar,
sizeof(value), &value);
}
}
Ads::Ads(QObject* parent): QObject (parent)
{
mOperation = new Operation();
QThread *mThread = new QThread();
mOperation->moveToThread(mThread);
mThread->start();
QObject::connect(this, &Ads::setStatus, mOperation, &Operation::setStatus);
QObject::connect(this, &Ads::readStatus, mOperation, &Operation::readStatus);
QObject::connect(this, &Ads::setSpeed, mOperation, &Operation::setSpeed);
QObject::connect(this, &Ads::readSpeed, mOperation, &Operation::readSpeed, Qt::DirectConnection);
QObject::connect(this, &Ads::setSwitch, mOperation, &Operation::setSwitch);
QObject::connect(this, &Ads::setAPosition, mOperation, &Operation::setAPosition);
// 可以制作一个定时器,来通过定时器来触发读取数据的函数
mTimer = new QTimer();
QObject::connect(mTimer, &QTimer::timeout, this, &Ads::getData);
mTimer->start(150);
}
Ads::~Ads()
{
delete mTimer;
delete mOperation;
}
void Ads::getData()
{
emit readStatus();
}