Beckhoff / ADS

Beckhoff protocol to communicate with TwinCAT devices.
MIT License
502 stars 194 forks source link

AdsSyncReadWroteReqEx2() generates error 1827 #128

Closed tillmanott closed 3 years ago

tillmanott commented 3 years ago

The call to AdsSyncReadWroteReqEx2() generates error 1827 (ADSERR_DEVICE_ACCESSDENIED). I have entered the passed values as a function comment as an example:

/**
* @param port Port created by AdsOpenPortEx() (30000)
* @param server Ams address of the PLC (5.58.182.254.1.1)
* @param handleName Designation of the plc variable ("GvlMain.test")
uint32_t getHandleByName(std::ostream& out, long port, const AmsAddr& server, std::string handleName)
{
    uint32_t handle = 0;

    const long handleStatus = AdsSyncReadWriteReqEx2(port,
                                                     &server,
                                                     ADSIGRP_SYM_HNDBYNAME,
                                                     0,
                                                     sizeof(handle),
                                                     &handle,
                                                     handleName.size(),
                                                     handleName.c_str(),
                                                     nullptr);
    if (handleStatus) {
        out << "Create handle for '" << handleName << "' failed with: " << std::dec << handleStatus << '\n';
    }
    return handle;
}

static AmsAddr amsAddrPlc =
    {
        {5, 58, 182, 254, 1, 1}, //netId
        AMSPORT_R0_PLC_TC3        //port
};
static AmsAddr amsAddrLocal =
    {
        {169,254,237,138,1,1}, //netId
        AMSPORT_R0_PLC_TC3        //port
};
static string IpV4Plc = "169.254.237.81";

int main(int argc, char *argv[])
{

    AdsVariable adsVar;

    std::ostringstream outStream;
    AdsSetLocalAddress(amsAddrLocal.netId); //<-- this netId is set in systray/router/edit routes
    if (AdsAddRoute(amsAddrPlc.netId, IpV4Plc.c_str()))
        cout << "failed to add route";
    long port = AdsPortOpenEx();

    uint32_t handle = getHandleByName(outStream, port, amsAddrPlc, "gvlVisu.visuSystem[0].inVehModeOff");

    AdsPortCloseEx(port);

Everything should be set up according to the readme.

tillmanott commented 3 years ago

Together with Mr. Brünn the problem could be found and solved. The network nodes were assigned addresses from the Link-Local address range (169.254.#.#). By changing to a private address range the error could be solved (e.g. class C: 192.168.#.#).