-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdllmain.cpp
217 lines (162 loc) · 4.97 KB
/
dllmain.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include <windows.h> // Include Windows headers first
#include "pch.h"
#include "TcAdsDef.h"
#include "TcAdsAPI.h" // Include the ADS library
#include <iostream>
// Your existing code...
long nErr;
AmsAddr Addr;
PAmsAddr pAddr = &Addr;
using namespace std;
void InitAds()
{
// Set AMS Net ID and port of the PLC
pAddr->netId.b[0] = 5;
pAddr->netId.b[1] = 143;
pAddr->netId.b[2] = 125;
pAddr->netId.b[3] = 242;
pAddr->netId.b[4] = 1;
pAddr->netId.b[5] = 1;
pAddr->port = 851; // Default port for TwinCAT 3 PLC
std::cout << "ADS initialized successfully." << std::endl;
// Initialize ADS
nErr = AdsPortOpen();
if (nErr)
{
std::cerr << "Error: AdsPortOpen: " << nErr << std::endl;
return;
}
// Get local AMS Net ID
nErr = AdsGetLocalAddress(pAddr);
if (nErr)
{
std::cerr << "Error: AdsGetLocalAddress: " << nErr << std::endl;
return;
}
}
void SetVariable(const char* varName, bool value)
{
long nErr;
uint32_t hVar;
nErr = AdsSyncReadWriteReq(pAddr, ADSIGRP_SYM_HNDBYNAME, 0x0, sizeof(hVar), &hVar, strlen(varName), (void*)varName);
if (nErr)
{
std::cerr << "Error: AdsSyncReadWriteReq (handle): " << nErr << std::endl;
return;
}
nErr = AdsSyncWriteReq(pAddr, ADSIGRP_SYM_VALBYHND, hVar, sizeof(value), &value);
if (nErr)
{
std::cerr << "Error: AdsSyncWriteReq: " << nErr << std::endl;
return;
}
std::cout << "Variable " << varName << " set to " << (value ? "True" : "False") << std::endl;
// Release the handle
AdsSyncWriteReq(pAddr, ADSIGRP_SYM_RELEASEHND, 0, sizeof(hVar), &hVar);
}
//GET THE LIVE VALUE FROM THE PLC
bool GetVariable(const char* varName, bool& value)
{
long nErr;
uint32_t hVar;
nErr = AdsSyncReadWriteReq(pAddr, ADSIGRP_SYM_HNDBYNAME, 0x0, sizeof(hVar), &hVar, strlen(varName), (void*)varName);
if (nErr)
{
std::cerr << "Error: AdsSyncReadWriteReq (handle): " << nErr << std::endl;
return false;
}
nErr = AdsSyncReadReq(pAddr, ADSIGRP_SYM_VALBYHND, hVar, sizeof(value), &value);
if (nErr)
{
std::cerr << "Error: AdsSyncReadReq: " << nErr << std::endl;
AdsSyncWriteReq(pAddr, ADSIGRP_SYM_RELEASEHND, 0, sizeof(hVar), &hVar);
return false;
}
std::cout << "Variable " << varName << " is reading " << (value ? "True" : "False") << std::endl;
// Release the handle
AdsSyncWriteReq(pAddr, ADSIGRP_SYM_RELEASEHND, 0, sizeof(hVar), &hVar);
return true;
}
// EXPORT THE FUNCTIONS SO THE CALLER APP (exe), Can Access them
extern "C" __declspec(dllexport) void SetAxis1Enable()
{
InitAds();
SetVariable("Main.fbAxis1.bEnable", true);
AdsPortClose();
}
extern "C" __declspec(dllexport) void SetAxis1Disable()
{
InitAds();
SetVariable("Main.fbAxis1.bEnable", false);
AdsPortClose();
}
/*
extern "C" __declspec(dllexport) void ReadAxis1Enable()
{
// Initialize ADS communication
if (AdsPortOpen() == 0)
{
std::cerr << "Failed to open ADS port" << std::endl;
return;
}
// Initialize the ADS address (replace with your actual address)
AmsAddr pAddr;
memset(&pAddr, 0, sizeof(pAddr));
pAddr.netId.b[0] = 169; // Example IP address
pAddr.netId.b[1] = 254;
pAddr.netId.b[2] = 250;
pAddr.netId.b[3] = 195;
pAddr.port = 851;
bool value;
if (GetVariable("Main.fbAxis1.bEnable", value))
{
std::cout << "Axis 1 Enable is " << (value ? "True" : "False") << std::endl;
}
else
{
std::cerr << "Failed to read Axis 1 Enable" << std::endl;
}
// Close ADS communication
AdsPortClose();
}
*/
// All Goes wrong when doing a Batch Build with the below included.....!?!?
/*
//READ AN INTEGER
//Define a new GetVariable function for int values:
extern "C" __declspec(dllexport) bool GetVariable(const char* varName, int& value)
{
long nErr;
uint32_t hVar;
nErr = AdsSyncReadWriteReq(pAddr, ADSIGRP_SYM_HNDBYNAME, 0x0, sizeof(hVar), &hVar, strlen(varName), (void*)varName);
if (nErr)
{
std::cerr << "Error: AdsSyncReadWriteReq (handle): " << nErr << std::endl;
return false;
}
nErr = AdsSyncReadReq(pAddr, ADSIGRP_SYM_VALBYHND, hVar, sizeof(value), &value);
if (nErr)
{
std::cerr << "Error: AdsSyncReadReq: " << nErr << std::endl;
AdsSyncWriteReq(pAddr, ADSIGRP_SYM_RELEASEHND, 0, sizeof(hVar), &hVar);
return false;
}
std::cout << "Variable " << varName << " is " << value << std::endl;
// Release the handle
AdsSyncWriteReq(pAddr, ADSIGRP_SYM_RELEASEHND, 0, sizeof(hVar), &hVar);
return true;
}
//Use the new GetVariable function in your code :
void Get_nIntToRead_Function()
{
int value;
if (GetVariable("Main.nIntToRead", value))
{
std::cout << "nIntToRead is reading a value of " << value << std::endl;
}
else
{
std::cerr << "Failed to read nIntToRead" << std::endl;
}
}
*/