Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Library running on Beckhoff IPC not working #141

Open
A-ndre-S opened this issue Apr 23, 2021 · 5 comments
Open

Library running on Beckhoff IPC not working #141

A-ndre-S opened this issue Apr 23, 2021 · 5 comments

Comments

@A-ndre-S
Copy link

Hi,
I've tryied to compile and use this library in a C++/WinRT project for using WinUI 3 API.
TcAdsDll does not work on this enviroment, giving me 0 when calling AdsPortOpen.
I've followed the example provided with this repo and i get it working when i run the app in my developing PC connected to a Beckhoff IPC running a PLC project. Here is the code:

void MainWindow::myButton_Click(IInspectable const&, RoutedEventArgs const&){
	std::string message = "Clicked";
	try {
		static const AmsNetId remoteNetId{ 192, 168, 2, 200, 1, 1 };
		static const char remoteIpV4[] = "192.168.2.200";
		
		// uncomment and adjust if automatic AmsNetId deduction is not working as expected
		AdsSetLocalAddress(AmsNetId{ 172, 22, 50, 158, 1, 1 });
		AdsDevice route{ remoteIpV4, remoteNetId, AMSPORT_R0_PLC_TC3 };
		route.SetTimeout(5000);
		const auto state = route.GetState();
		std::stringstream stream;
		stream << "\tADS state: " << std::dec << (uint16_t)state.ads << " devState: " << std::dec << (uint16_t)state.device;
		message += stream.str();
		AdsVariable<bool> readVar{ route, ".i_main_power_on" };
		message += "\t i_main_power_on: " + std::to_string((bool)(readVar));	
		AdsVariable<int32_t> readenc{ route, ".encoder_AX07" };
		message += "\t encoder_AX07: " + std::to_string((int32_t)(readenc));
	}
	catch (const AdsException& ex) {
		message += "\t";
		message += ex.what();
		std::cout << "Error: " << ex.errorCode << "\n";
		std::cout << "AdsException message: " << ex.what() << "\n";
	}
	catch (const std::runtime_error& ex) {
		std::cout << ex.what() << '\n';
	}
	myButton().Content(box_value(winrt::to_hstring(message)));
}

When i try to run the app on the IPC itself i cannot make it working: i can create succesfully the AdsDevice "route" object (following the example) but i get Error 1861 at every call i make.
The changes for the code are on the lines

static const AmsNetId remoteNetId{ 192, 168, 2, 200, 1, 1 };
static const char remoteIpV4[] = "192.168.2.200";
		
// uncomment and adjust if automatic AmsNetId deduction is not working as expected
AdsSetLocalAddress(AmsNetId{ 192, 168, 2, 200, 1, 1 });

I also tryed to set the remoteIpV4 to localhost.

Every call i make to interact with route (like route.GetState()) raises an exception giving error 1861.

Is it possible to use this library in a PC running a PLC project? Am i setting something wrong?

Thank you,
Andres

@pbruenn
Copy link
Member

pbruenn commented Apr 23, 2021

On localhost you have to use the TwinCAT router. You, would need to link against TcAdsLib.dll not AdsLib.dll. Call meson with an environment variable pointing to the TwinCAT TcAdsDll.dll similar to this:

- $env:BHF_CI_MESON_OPTIONS = '-Dtcadsdll_include=c:\TwinCAT\AdsApi\TcAdsDll\Include', '-Dtcadsdll_lib=c:\TwinCAT\AdsApi\TcAdsDll\x64\lib'

@A-ndre-S
Copy link
Author

Hello,
i've build the library with the option -Dtcadsdll_include=c:\TwinCAT\AdsApi\TcAdsDll\Include, that gives the libTcAdsLib.a. Then i've used this library and TcAdsDll.lib in the same WinUI 3 test project (template with the modifications of the post above) but i have AdsPortOpenEx returning 0 and an exception while calling route.SetTimeout(5000); (code 1864 ADSERR_CLIENT_PORTNOTOPEN).

Have i done something wrong?
Thank you for your help and patience!

Andres

@pbruenn
Copy link
Member

pbruenn commented Apr 26, 2021

Hmm, if you don't get a valid port from AdsPortOpenEx() maybe you didn't add a TwinCAT route earlier? If you use TcAdsDll directly, does AdsPortOpenEx() work in that case?

@A-ndre-S
Copy link
Author

I've added the route earlier and when using a MFC project TcAdsDll works fine. When i try to use WinUI 3 unluckly i get 0 as return value from AdsPortOpen() and AdsPortOpenEx() using directly TcAdsDll, that's why i tryied to use AdsLib library to connect to a plc running in local.

(Following "Create a WinUI 3 desktop app for C++/Win32" in https://docs.microsoft.com/en-us/windows/apps/winui/winui3/get-started-winui3-for-desktop)

Thank you!

@marcus-sonestedt
Copy link
Contributor

See #161 for some tweaks I've done to use this lib more easily with TwinCAT router on Windows build.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants