Beckhoff / ADS

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

Value from notification #179

Closed BlooKe closed 1 year ago

BlooKe commented 1 year ago

Hello there!

I'm trying to get a value from a notification on Linux client, and as I understand it should be located after the AdsNotificationHeader in callback.

I use code like this:

void callback(const AmsAddr* pAddr, const AdsNotificationHeader* pNotification, uint32_t hUser) {
        char* data = new char[pNotification->cbSampleSize];
        memcpy(data,
               static_cast<void*>(&pNotification + sizeof(AdsNotificationHeader)),
               sizeof(char) * pNotification->cbSampleSize);

        SPDLOG_INFO("Notification data: {}",
                    std::bitset<8>(data[0]).to_string());

        delete[] data;
}

The issue is that I allways get the same value, and it does not change.

I logged the data in Notify() function and it gets the right value:

void Notify(uint64_t timestamp, RingBuffer& ring) const {
        auto header   = reinterpret_cast<AdsNotificationHeader*>(buffer.get());
        uint8_t* data = reinterpret_cast<uint8_t*>(header + 1);

        for (size_t i = 0; i < header->cbSampleSize; ++i) {
            data[i] = ring.ReadFromLittleEndian<uint8_t>();
        }
        std::cout << "Notify size: " << header->cbSampleSize
                  << " data: " << std::bitset<8>(data[0]) << std::endl;
        header->nTimeStamp = timestamp;
        callback(&connection.second, header, hUser);
    }

logs:

Notify size: 1 data: 00000001
[2022-11-25 14:00:17.126] [info] Notification data: 10000000 
Notify size: 1 data: 00000000
[2022-11-25 14:00:17.126] [info]Notification data: 10000000

little/Big Endian is not an issue at this moment, but I would like to understand how to get the value in the callback on Linux, without calling for the AdsVariable read on each notification.

BlooKe commented 1 year ago

Need to properly read the example....