Beckhoff / ADS

Beckhoff protocol to communicate with TwinCAT devices.
MIT License
491 stars 193 forks source link

Run ADS Client on WSL through localhost #190

Open captain-yoshi opened 1 year ago

captain-yoshi commented 1 year ago

Is it possible to run the ADS Client on a windows subsystem linux (WSL) which communicates through the localhost ? FYI it works when installing the client on a remote PC using the same linux distribution as the WSL (Ubuntu 20.04).

Maybe related to #93 and #141.

ADS Route

image

Client Config

static void runExample(std::ostream& out)
{
    static const AmsNetId remoteNetId { 192, 168, 0, 193, 1, 1 };
    static const char remoteIpV4[] = "192.168.0.193";

    // uncomment and adjust if automatic AmsNetId deduction is not working as expected
    // Tried with and without this line below
    // bhf::ads::SetLocalAddress({192, 168, 0, 193, 1, 1});
    ...
}

Running the Client Example

~$ ./example/build/example
# Nothing for 5 seconds and then :
2023-02-22T15:04:20-0500 Info: connection closed by remote
Error: 1861
AdsException message: Ads operation failed with error code 1861.

Wireshark Debug (loopback adapter) wireshark-capture.zip

image

pbruenn commented 1 year ago

TwinCAT doesn't expect TCP connections from localhost. Especially, when you use the same AMS NetId as Target and Sender address. So at least you have to use different AMS NetIds for the TwinCAT server (in outside Windows) and your ADS Client (inside WSL). Then it depends on WSL networking, which I have no clue about. If on the TwiNCAT side the connections looks like a normal remote host, then it should work as soon as you configured the AMS route correctly. If TwinCAT receives a TCP connection attempt from an IP it is listening itself I doubt it will work.

captain-yoshi commented 1 year ago

No luck with different AMS NetId... WSL networking has it's own considerations.

I think WSL1 gives you a localhost network only. WSL2 gives you a virtual network that you can configure but needs Hyper-V which is not compatible with TwinCAT in run-mode.

I did not try changing my TwinCAT Local NetId to localhost... Maybe that would work.

It works through VirtualBox and remotly, so really not a big issue for me. Thanks for the library ! Very handy 👍

klauer commented 8 months ago

I think the author of a nodejs ADS client figured out what's necessary for localhost communication - reservation of a port on the router. This has since been at least partially propagated to the rust client ads-rs (though I'm not sure if it cleared the reserved port after use). It should work for WSL1 with localhost networking, though WSL2 is a different story as you've noted.

Since this is technically possible over ADS, I wonder if it's something that might be accepted for Beckhoff's open source library here. Even some official acknowledgment/documentation of the underlying mechanism would be nice, too.

Links: