Can I query the list of variables and types - twincat-ads

Can I use twincat-ads to query a Beckhoff PLC for the variables (and their types) that are exposed on ADS.

Yes you can.
This example comes directly from the Beckhoff ads documentation which you should study a bit in order to understand the basics of the ads protocol.
/// <summary>
/// Defines the entry point of the application.
/// </summary>
/// <param name="args">The arguments.</param>
static void Main(string[] args)
{
    Console.WriteLine("");
    Console.WriteLine("Press [Enter] for start:");
    Console.ReadLine();
    // Parse the command-line arguments
    AmsAddress address = ArgParser.Parse(args);
    using (TcAdsClient client = new TcAdsClient())
    {
    // Connect the AdsClient to the device target.
    client.Connect(address);
    // Load symbolic information
    ISymbolLoader loader = SymbolLoaderFactory.Create(client, SymbolLoaderSettings.Default);
    ReadOnlySymbolCollection allSymbols = loader.Symbols;
    ISymbol bVar1 = allSymbols["GVL.bVar1"];
    ISymbol bVar2 = allSymbols["GVL.iCount"];
    ISymbol projectName = allSymbols["TwinCAT_SystemInfoVarList._AppInfo.ProjectName"];
    SymbolCollection symbols = new SymbolCollection() {bVar1, bVar2, projectName};
    // Sum Command Read
    SumSymbolRead readCommand = new SumSymbolRead(client,symbols);
    object[] values = readCommand.Read();
    for (int i = 0; i < symbols.Count; i++)
    {
        Console.WriteLine("Symbol: {0} (Value: {1}, Type: {2})",symbols[i].InstancePath,values[i].ToString(),values[i].GetType().Name);
    }
    // Sum Command Write
    SumSymbolWrite writeCommand = new SumSymbolWrite(client,symbols);
    object[] writeValues = new object[] {true, (short) 42, "MyNewProjectName"};
    writeCommand.Write(writeValues);
    }
    Console.WriteLine("");
    Console.WriteLine("Press [Enter] for leave:");
    Console.ReadLine();
}
In C++ you need to use the AdsSyncReadReq function:
#include <iostream.h>
#include <windows.h>
#include <conio.h>
#include <assert.h>
// ADS headers for TwinCAT 3
#include "C:\TwinCAT\AdsApi\TcAdsDll\Include\TcAdsDef.h"
#include "C:\TwinCAT\AdsApi\TcAdsDll\Include\TcAdsAPI.h"
void main()
{
long nErr, nPort;
char *pchSymbols = NULL;
UINT uiIndex;
AmsAddr Addr;
PAmsAddr pAddr = &Addr;
AdsSymbolUploadInfo tAdsSymbolUploadInfo;
PAdsSymbolEntry pAdsSymbolEntry;
// Open communication port on the ADS router
nPort = AdsPortOpen();
nErr = AdsGetLocalAddress(pAddr);
if (nErr) cerr << "Error: AdsGetLocalAddress: " << nErr << '\n';
// Select Port: TwinCAT 3 PLC1 = 851
pAddr->port = 851;
// Read the length of the variable declaration
nErr = AdsSyncReadReq(pAddr, ADSIGRP_SYM_UPLOADINFO, 0x0, sizeof(tAdsSymbolUploadInfo), &tAdsSymbolUploadInfo);
if (nErr) cerr << "Error: AdsSyncReadReq: " << nErr << '\n';
pchSymbols = new char[tAdsSymbolUploadInfo.nSymSize];
assert(pchSymbols);
// Read information about the PLC variables
nErr = AdsSyncReadReq(pAddr, ADSIGRP_SYM_UPLOAD, 0, tAdsSymbolUploadInfo.nSymSize, pchSymbols);
if (nErr) cerr << "Error: AdsSyncReadReq: " << nErr << '\n';
// Output information about the PLC variables
pAdsSymbolEntry = (PAdsSymbolEntry)pchSymbols;
for (uiIndex = 0; uiIndex < tAdsSymbolUploadInfo.nSymbols; uiIndex++)
{
cout << PADSSYMBOLNAME(pAdsSymbolEntry) << "\t\t"
<< pAdsSymbolEntry->iGroup << '\t'
<< pAdsSymbolEntry->iOffs << '\t'
<< pAdsSymbolEntry->size << '\t'
<< PADSSYMBOLTYPE(pAdsSymbolEntry) << '\t'
<< PADSSYMBOLCOMMENT(pAdsSymbolEntry) << '\n';
pAdsSymbolEntry = PADSNEXTSYMBOLENTRY(pAdsSymbolEntry); cout.flush();
}
getch();
// Close communication port
nErr = AdsPortClose();
if (nErr) cerr << "Fehler: AdsPortClose: " << nErr << '\n';
// Release memory
if (pchSymbols) delete(pchSymbols);
}
For more info:
https://infosys.beckhoff.com/english.php?content=../content/1033/tc3_ads_intro/9007199370899851.html&id=5869483416056481636

Related

Can't delete a pointer in C++

I got this code from a book. When I ran on Visual Studio, it said to switch strcpy() to strcpy_s(), and after I did that, it seems the program terminated at the delete pointer. I tried to run on Dev-C++, and it works fine. Anyone knows why? Thank you.
#include "pch.h"
#include <iostream>
#include <cstring>
int main()
{
cout << "Enter a kind of animal: ";
cin >> animal; // ok if input < 20 chars
ps = animal; // set ps to point to string
cout << ps << "!\n"; // ok, same as using animal
cout << "Before using strcpy():\n";
cout << animal << " at " << (int *)animal << endl;
cout << ps << " at " << (int *)ps << endl;
ps = new char[strlen(animal) + 1]; // get new storage
strcpy_s(ps, sizeof(animal), animal); // copy string to new storage
cout << "After using strcpy():\n";
cout << animal << " at " << (int *)animal << endl;
cout << ps << " at " << (int *)ps << endl;
delete[] ps;
return 0;
}

OpenCL Vector add program

I'm absolutely new to OpenCL programming. I have a working installation of OpenCL library and drivers. But the program I'm trying to run is not producing expected output (Output is all zeros). It is just a simple vector_add program.
Thanks in advance for suggestions.
int main(int argc, char** argv)
{
cout << "Hello OpenCL" << endl;
vector<Platform> all_platforms;
int err = Platform::get(&all_platforms);
cout << "Getting Platform ... Error code " << err << endl;
if (all_platforms.size()==0)
(cout << "No platforms" << endl, exit(0));
cout << "Platform info : " << all_platforms[0].getInfo<CL_PLATFORM_NAME>() << endl;
Platform default_platform = all_platforms[0];
cout << "Defaulting to it ..." << endl;
vector<Device> all_devices;
err = default_platform.getDevices(CL_DEVICE_TYPE_GPU, &all_devices);
cout << "Getting devices ... Error code : " << err << endl;
if (all_devices.size()==0)
(cout << "No devices" << endl, exit(0));
Device default_device = all_devices[0];
cout << all_devices.size() << " devices & " << "Device info : " << all_devices[0].getInfo<CL_DEVICE_NAME>() << endl;
cout << "Defaulting to it ..." << endl;
Context context(default_device);
Program::Sources sources;
std::string kernel_code=
" void kernel simple_add(global const int* A, global const int* B, global int* C){"
" unsigned int i = get_global_id(0); "
" C[i]=A[i]+B[i]; "
" } ";
sources.push_back(make_pair(kernel_code.c_str(), kernel_code.length()+1));
Program program(context, sources);
if (program.build(all_devices)==CL_SUCCESS)
cout << "Built Successfully" << endl;
Buffer buffer_A(context,CL_MEM_READ_WRITE,sizeof(int)*10);
Buffer buffer_B(context,CL_MEM_READ_WRITE,sizeof(int)*10);
Buffer buffer_C(context,CL_MEM_READ_WRITE,sizeof(int)*10);
int A[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int B[] = {0, 1, 2, 0, 1, 2, 0, 1, 2, 0};
CommandQueue queue(context,default_device);
queue.enqueueWriteBuffer(buffer_A,CL_TRUE,0,sizeof(int)*10,A); // load data from host to device
queue.enqueueWriteBuffer(buffer_B,CL_TRUE,0,sizeof(int)*10,B);
Kernel kernel(program, "vector_add");
kernel.setArg(0, buffer_A);
kernel.setArg(1, buffer_B);
kernel.setArg(2, buffer_C);
queue.enqueueNDRangeKernel(kernel,cl::NullRange,cl::NDRange(10),cl::NullRange);
queue.finish();
int *C = new int[10];
queue.enqueueReadBuffer(buffer_C, CL_TRUE, 0, 10 * sizeof(int), C);
for (int i=0;i<10;i++)
std::cout << A[i] << " + " << B[i] << " = " << C[i] << std::endl;
return 0;
}
As pointed out in the comments, you should always check the error codes when using OpenCL API functions. This can be achieved by enabling exception handling in the C++ wrapper:
#define __CL_ENABLE_EXCEPTIONS // with cl.hpp
//#define CL_HPP_ENABLE_EXCEPTIONS // with cl2.hpp
#include <CL/cl.hpp>
int main(int argc, char *argv[])
{
try
{
// OpenCL code here
}
catch (cl::Error& err)
{
cout << err.what() << " failed with error code " << err.err() << endl;
}
}
If you do this, you will receive useful information about a couple of issues with your code.
The clCreateKernel function returns CL_INVALID_NAME. In your kernel, you define the kernel function with the name simple_add, but then you try and create a kernel object using the name vector_add.
If you have an OpenCL platform with multiple devices, you may also receive an error when building your kernel program. This is because you are creating an OpenCL context with a single device, but then trying to build the program for a list of devices:
Context context(default_device);
// ...
if (program.build(all_devices)==CL_SUCCESS)
cout << "Built Successfully" << endl;
The simplest fix is just to remove the argument from the build function, since by default it will build the program for all devices in the context (which is almost always what you actually want):
if (program.build()==CL_SUCCESS)
cout << "Built Successfully" << endl;

iostream and No_delay option

I am trying to disable the Nagle Algorithm using the answer for the same question: ASIO ip::tcp::iostream and TCP_NODELAY:
boost::asio::ip::tcp::iostream socketStream;
const boost::asio::ip::tcp::no_delay option( true );
socketStream.rdbuf()->set_option( option );
boost::asio::io_service io_service;
tcp::endpoint endpoint (tcp::v4 (), 6666);
tcp::acceptor acceptor (io_service, endpoint);
std::cout << "Waiting for connection.." << std::endl;
acceptor.accept (*socketStream.rdbuf ());
std::cout << "Connected!" << std::endl;
and when running the code this error appears:
set_option: Bad file descriptor
How can I solve this problem?
Where you set the option, the stream is still invalid (not open).
Wait until the socket is open, before setting the option:
Live On Coliru
#include <boost/asio.hpp>
#include <iostream>
static boost::asio::ip::tcp::no_delay const no_delay_option (true);
int main() {
using boost::asio::ip::tcp;
tcp::iostream socketStream;
boost::asio::io_service io_service;
tcp::endpoint endpoint (tcp::v4(), 6666);
tcp::acceptor acceptor (io_service, endpoint);
std::cout << "Waiting for connection.." << std::endl;
acceptor.accept (*socketStream.rdbuf ());
socketStream.rdbuf()->set_option(no_delay_option);
std::cout << "Connected!" << std::endl;
std::cout << socketStream.rdbuf() << "\n";
}
(We send main.cpp to port 6666 using netcat there)

C++ 11 correct way to pass a functional object by reference or pointer?

I am trying to pass a random number generator around my program (I want to use a single one), however I cannot seem to find a way to pass it by reference.
Here is what I have tried thus far:
#include <iostream>
#include <random>
#include <functional>
using namespace std;
void test(const function<int()> arng0, const function<int()> & arng1, const function<int()> & arng2,
const function<int()> frng0, const function<int()> & frng1, const function<int()> & frng2)
{
cerr << "Auto - std::_bind size: " << sizeof(arng0) << endl;
cerr << "Auto - std::_bind & size: " << sizeof(arng1) << endl;
cerr << "Auto - std::_bind ref size: " << sizeof(arng2) << endl;
cerr << "Functional size: " << sizeof(frng0) << endl;
cerr << "Functional & size: " << sizeof(frng1) << endl;
cerr << "Functional ref size: " << sizeof(frng2) << endl;
}
void main()
{
default_random_engine e;
uniform_int_distribution<int> dist(0, 100);
auto autoRng = bind(ref(dist), ref(e));
function<int()> funcRng = bind(ref(dist), ref(e));
test(autoRng, autoRng, ref(autoRng),
funcRng, funcRng, ref(funcRng));
system("Pause");
}
The output is 24 bytes for all of them. I know functional is a heavy wrapper, however passing by reference still should be 4 bytes?
Answer:
void testValue(const function<int()> arng0)
{
arng0();
}
void testRef(const function<int()> & arng0)
{
arng0();
}
{// Pass by value
auto start = high_resolution_clock::now();
for (unsigned int i = 0; i < 100000; i++)
{
testValue(funcRng);
}
auto end = high_resolution_clock::now();
auto total = duration_cast<milliseconds>(end - start).count();
cerr << "TestVal time: " << total << endl;
}
{// Pass by ref
auto start = high_resolution_clock::now();
for (unsigned int i = 0; i < 100000; i++)
{
testRef(funcRng);
}
auto end = high_resolution_clock::now();
auto total = duration_cast<milliseconds>(end - start).count();
cerr << "TestRef time: " << total << endl;
}
Results:
Test Value Time: 179 ms
Test Ref Time: 74 ms
I think you've got it right, passing by reference should be 4 bytes if you're on a 32 bit machine. sizeof is giving you the size of the thing being referenced, not the size of the reference. I don't think you need to use 'ref' at the callsite.
See sizeof documentation here:
http://en.cppreference.com/w/cpp/language/sizeof

WebRTC playing webcam with Qt

I use WebRTC for webcam playing (it's for testing webcam at my application before video calling). I do these next some WebRTC function:
////////////////////////
//initialization
{
webrtc::VoiceEngine* voe = VoiceEngine::Create();
webrtc::VoEBase* voeBase = VoEBase::GetInterface( voe );
voeBase->Init();
webrtc::VideoEngine* vie = VideoEngine::Create();
webrtc::ViEBase* vieBase = ViEBase::GetInterface( vie );
vieBase->Init();
webrtc::ViECapture* vieCapture = ViECapture::GetInterface( vie );
}
////////////////////////
// start webcam playing
void MainWindow::StartWebcamTest()
{
// creating window for playing webcam video
videoWidget = new Phonon::VideoWidget();
videoWidget->setFixedSize( 640, 480 );
videoWidget->setWindowTitle( "Test Webcam" );
char captDevName[1024], captGuidName[1024];
VoEEngineSingleton::Instance()->GetViECapture()->GetCaptureDevice( 0, captDevName, 1024, captGuidName, 1024 );
VoEEngineSingleton::Instance()->GetViECapture()->AllocateCaptureDevice( captGuidName, 1024, captureID );
testVideoChannel = VoEEngineSingleton::Instance()->GetViEBase()->CreateChannel( testVideoChannel );
if( testVideoChannel == -1 )
LOGERR << "Creating video channel error: " << VoEEngineSingleton::Instance()->GetViEBase()->LastError() << endl;
else LOGDEBUG << "Video channel created, ID = " << testVideoChannel << endl;
testAudioChannel = VoEEngineSingleton::Instance()->GetVoEBase()->CreateChannel();
if( testAudioChannel == -1 )
LOGERR << "Creating audio channel error: " << VoEEngineSingleton::Instance()->GetVoEBase()->LastError() << endl;
else LOGDEBUG << "Audio channel created, ID = " << testAudioChannel << endl;
if( VoEEngineSingleton::Instance()->GetViEBase()->ConnectAudioChannel( testVideoChannel, testAudioChannel ) != 0 )
LOGERR << "ConnectAudioChannel() error: " << VoEEngineSingleton::Instance()->GetViEBase()->LastError() << endl;
else LOGINFO << "Audio channel connected successful " << endl;
if( VoEEngineSingleton::Instance()->GetViECapture()->ConnectCaptureDevice( captureID, testVideoChannel ) != 0 )
LOGERR << "ConnectCaptureDevice() error: " << VoEEngineSingleton::Instance()->GetViEBase()->LastError() << endl;
else LOGINFO << "Capture device connected successful " << endl;
if( VoEEngineSingleton::Instance()->GetViERender()->StartRender( testVideoChannel ) != 0 )
LOGERR << "StartRender() error: " << VoEEngineSingleton::Instance()->GetViEBase()->LastError() << endl;
else LOGINFO "StartRender() successed " << endl;
if( VoEEngineSingleton::Instance()->GetViECapture()->StartCapture( captureID ) != 0 )
LOGERR << "StartCapture() error: " << VoEEngineSingleton::Instance()->GetViEBase()->LastError() << endl;
else LOGINFO << "StartCapture() successed " << endl;
videoWidget->show();
}
When I click button for playing video - webcam starts playing but videoWidget window is black empty.
What am I doing not true? And maybe I must use other Qt widget for it?
You have to implement video renderer in Qt. subclass webrtc::ExternalRenderer and draw the frame
#include <QApplication>
#include <QUrl>
#include <QWebEngineView>
class WebEnginePage: public QWebEnginePage{
Q_OBJECT
public:
WebEnginePage(QObject *parent = Q_NULLPTR):QWebEnginePage(parent){
connect(this, &WebEnginePage::featurePermissionRequested, this, &WebEnginePage::onFeaturePermissionRequested);
}
private Q_SLOTS:
void onFeaturePermissionRequested(const QUrl &securityOrigin, QWebEnginePage::Feature feature){
if(feature == QWebEnginePage::MediaAudioCapture
|| feature == QWebEnginePage::MediaVideoCapture
|| feature == QWebEnginePage::MediaAudioVideoCapture)
setFeaturePermission(securityOrigin, feature, QWebEnginePage::PermissionGrantedByUser);
else
setFeaturePermission(securityOrigin, feature, QWebEnginePage::PermissionDeniedByUser);
}
};
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication app(argc, argv);
QWebEngineView view;
view.setPage(new WebEnginePage);
QUrl url = "https://appr.tc/r/386424467";
// view.setUrl(commandLineUrlArgument());
view.setUrl(url);
view.resize(1024, 750);
view.show();
return app.exec();
}
#include "main.moc"

Resources