Skip to content

Connection Close by remote while opening twincat project file at the same time #274

@hasnulaidit95

Description

@hasnulaidit95

Hi All

I successfully compiled and run the project. But when I open twincat file, some times the data intermittent and connection becoming close. I need to close twincat file then I can read the value. Btw i am just using "notificationByNameExample" only. Below is the error:

_notificationByNameExample():
Hit ENTER to stop by name notifications
2025-11-03T12:11:34 Warning: No dispatcher found for notification
 hUser 0xbeefdead sample time: 134066168684460000 sample size: 4 value: 0x51.2464
2025-11-03T12:11:35 Warning: No dispatcher found for notification
 hUser 0xbeefdead sample time: 134066168688460000 sample size: 4 value: 0x74.2235
 hUser 0xbeefdead sample time: 134066168692460000 sample size: 4 value: 0x49.0491
2025-11-03T12:11:35 Warning: No dispatcher found for notification
 hUser 0xbeefdead sample time: 134066168696460000 sample size: 4 value: 0x33.3125
2025-11-03T12:11:36 Warning: No dispatcher found for notification
 hUser 0xbeefdead sample time: 134066168700460000 sample size: 4 value: 0x29.3574
2025-11-03T12:11:36 Warning: No dispatcher found for notification
 hUser 0xbeefdead sample time: 134066168704460000 sample size: 4 value: 0x16.3216
2025-11-03T12:11:37 Warning: No dispatcher found for notification
 hUser 0xbeefdead sample time: 134066168708460000 sample size: 4 value: 0x11.8156
 hUser 0xbeefdead sample time: 134066168712460000 sample size: 4 value: 0x83.4659
2025-11-03T12:11:37 Warning: No dispatcher found for notification
 hUser 0xbeefdead sample time: 134066168716460000 sample size: 4 value: 0x28.3899
2025-11-03T12:11:38 Warning: No dispatcher found for notification
 hUser 0xbeefdead sample time: 134066168720460000 sample size: 4 value: 0x68.6783
2025-11-03T12:11:38 Warning: No dispatcher found for notification
 hUser 0xbeefdead sample time: 134066168724460000 sample size: 4 value: 0x56.5138
2025-11-03T12:11:39 Warning: No dispatcher found for notification
 hUser 0xbeefdead sample time: 134066168728460000 sample size: 4 value: 0x12.3619
2025-11-03T12:11:39 Info: connection closed by remote_

Can I know how I can read the value without connection closed by remote?

I also refer to this link: https://infosys.beckhoff.com/english.php?content=../content/1033/tc3_adsdll2/index.html&id=

i believe it can be done by AdsSyncAddDeviceNotificationReq, AdsSyncDelDeviceNotificationReq and AdsPortClose(), means
if got error while reading the value, so i can refresh connection by delete notfication, port close and add device notification request again.

I tried from sample link above but seems no respond:

`Error: AdsSyncReadWriteReq: 7
Error: AdsSyncAddDeviceNotificationReq: 7
Notification: 32758

`

Below is my full code, i only modified example/example.cpp

#include "AdsLib.h"
#include "AdsNotificationOOI.h"
#include "AdsVariable.h"

#include <array>
#include <cstring>
#include <iostream>
#include <iomanip>
#include <thread>
#include <conio.h>
using ExampleDatatype = float; //uint8_t

		//Callback(AmsAddr* pAddr, AdsNotificationHeader* pNotification, ULONG hUser )
void NotifyCallback(const AmsAddr *pAddr, const AdsNotificationHeader *pNotification, uint32_t hUser)
{
	// std::this_thread::sleep_for(std::chrono::seconds(1));
	ExampleDatatype data{};
	std::memcpy(&data, pNotification + 1,
		    std::min<size_t>(sizeof(data),
				     pNotification->cbSampleSize));
	std::cout 
		//   << std::setfill('0') << "NetId: " << pAddr->netId
		  << " hUser 0x" << std::hex << hUser
		  << " sample time: " << std::dec << pNotification->nTimeStamp
		  << " sample size: " << std::dec << pNotification->cbSampleSize
		  << " value:" << " 0x" << std::hex << +data << '\n';
}

static void notificationExample(std::ostream &out, const AdsDevice &route)
{
	const AdsNotificationAttrib attrib = {
		sizeof(ExampleDatatype), ADSTRANS_SERVERCYCLE, 0, { 4000000 }
	};
	AdsNotification notification{ route,  0x4020,	       4,
				      attrib, &NotifyCallback, 0xDEADBEEF };

	out << "Hit ENTER to stop notifications\n";
	std::cin.ignore();
}

static void notificationByNameExample(std::ostream &out, const AdsDevice &route)
{
	const AdsNotificationAttrib attrib = {
		sizeof(ExampleDatatype), ADSTRANS_SERVERONCHA, 0, { 4000000 }
	};

	out << __FUNCTION__ << "():\n";
	AdsNotification notification{ route, "GVL.dummy_flow1", attrib,
				      &NotifyCallback, 0xBEEFDEAD };

	out << "Hit ENTER to stop by name notifications\n";
	std::cin.ignore();
}

static void readExample(std::ostream &out, const AdsDevice &route)
{
	AdsVariable<uint8_t> readVar{ route, 0x4020, 0 };

	out << __FUNCTION__ << "():\n";
	for (size_t i = 0; i < 8; ++i) {
		out << "ADS read " << std::hex << (uint32_t)readVar << '\n';
	}
}

static void readByNameExample(std::ostream &out, const AdsDevice &route)
{
	AdsVariable<uint8_t> readVar{ route, "MAIN.byByte[4]" };

	out << __FUNCTION__ << "():\n";
	for (size_t i = 0; i < 8; ++i) {
		out << "ADS read " << std::hex << (uint32_t)readVar << '\n';
	}
}

static void readWriteExample(std::ostream &out, const AdsDevice &route)
{
	AdsVariable<uint8_t> simpleVar{ route, "MAIN.byByte[0]" };
	AdsVariable<uint8_t> validation{ route, "MAIN.byByte[0]" };

	out << __FUNCTION__ << "():\n";
	simpleVar = 0xA5;
	out << "Wrote " << 0xA5 << " to MAIN.byByte and read "
	    << (uint32_t)validation << " back\n";
	simpleVar = 0x5A;
	out << "Wrote " << (uint32_t)simpleVar << " to MAIN.byByte and read "
	    << (uint32_t)validation << " back\n";
}

static void readWriteArrayExample(std::ostream &out, const AdsDevice &route)
{
	static const std::array<uint8_t, 4> arrayToWrite = { 1, 2, 3, 4 };
	AdsVariable<std::array<uint8_t, 4> > arrayVar{ route, "MAIN.byByte" };
	arrayVar = arrayToWrite;
	std::array<uint8_t, 4> readArray = arrayVar;
	out << "Wrote array with first value " << (uint32_t)arrayToWrite[0]
	    << " and last value " << (uint32_t)arrayToWrite[3] << "\n";
	out << "Read back array with first value " << (uint32_t)readArray[0]
	    << " and last value " << (uint32_t)readArray[3] << "\n";
}

static void readStateExample(std::ostream &out, const AdsDevice &route)
{
	const auto state = route.GetState();

	out << "ADS state: " << std::dec << (uint16_t)state.ads
	    << " devState: " << std::dec << (uint16_t)state.device << '\n';
}

static void runExample(std::ostream &out)
{
	static const AmsNetId remoteNetId{ 172,17,10,5,1,1 }; 
	static const char remoteIpV4[] = "192.168.100.154"; 
	
	// uncomment and adjust if automatic AmsNetId deduction is not working as expected
	bhf::ads::SetLocalAddress({192,168,232,1,1,1}); 
	
	AdsDevice route{ remoteIpV4, remoteNetId, AMSPORT_R0_PLC_TC3 };

	// AmsAddr* pAddr;
	// AdsGetLocalAddressEx(AMSPORT_R0_PLC_TC3, pAddr);
	//notificationExample(out, route);
	notificationByNameExample(out, route);
	//readExample(out, route);
	// readByNameExample(out, route);
	// readWriteExample(out, route);
	// readWriteArrayExample(out, route);
	// readStateExample(out, route);
}

/*
int main()
{
	try {
		runExample(std::cout);
	} catch (const AdsException &ex) {
		std::cout << "Error: " << ex.errorCode << "\n";
		std::cout << "AdsException message: " << ex.what() << "\n";
	} catch (const std::runtime_error &ex) {
		std::cout << ex.what() << '\n';
	}
	std::cout << "Hit ENTER to continue\n";
	std::cin.ignore();
}
//*/

///*
int main()
{ 
long         nErr, nPort; 
AmsAddr        *pAddr; 
AmsNetId remoteNetId{ 172,17,10,5,1,1 };
pAddr->netId = remoteNetId;
//PAmsAddr     pAddr = &Addr; 
uint32_t         hNotification, hUser, cbReturn; 
AdsNotificationAttrib adsNotificationAttrib;
char                 szVar []={"GVL.dummy_flow1"};

// open communication port on the ADS router
nPort = AdsPortOpenEx();
// nErr = AdsGetLocalAddressEx(851, pAddr);
//if (nErr) std::cerr << "Error: AdsGetLocalAddress: " << nErr << '\n';

// TwinCAT 3 RTS1 Port = 851
pAddr->port = 851;


// set the attributes of the notification
adsNotificationAttrib.cbLength = 4;
adsNotificationAttrib.nTransMode = ADSTRANS_SERVERONCHA;
adsNotificationAttrib.nMaxDelay = 0;
adsNotificationAttrib.nCycleTime = 10000000; // 1sec 

// get handle
// AdsSyncReadWriteReqEx2
nErr = AdsSyncReadWriteReqEx2(pAddr->port, pAddr, ADSIGRP_SYM_HNDBYNAME, 0x0, sizeof(hUser), &hUser, sizeof(szVar), szVar, &cbReturn);
if (nErr) std::cerr << "Error: AdsSyncReadWriteReq: " << nErr << '\n';

// initiate the transmission of the PLC-variable 
nErr = AdsSyncAddDeviceNotificationReqEx(pAddr->port, pAddr, ADSIGRP_SYM_VALBYHND, hUser, &adsNotificationAttrib, NotifyCallback, hUser, &hNotification);

if (nErr) std::cerr << "Error: AdsSyncAddDeviceNotificationReq: " << nErr << '\n';
std::cout << "Notification: " << hNotification << "\n\n";
std::cout.flush();

// wait for user intraction (keystroke)
getch();

// finish the transmission of the PLC-variable 
nErr = AdsSyncDelDeviceNotificationReqEx(pAddr->port, pAddr, hNotification);
if (nErr) std::cerr << "Error: AdsSyncDelDeviceNotificationReq: " << nErr << '\n';

// release handle
nErr = AdsSyncWriteReqEx(pAddr->port,pAddr, ADSIGRP_SYM_RELEASEHND, 0, sizeof(hUser), &hUser); 
if (nErr) std::cerr << "Error: AdsSyncWriteReq: " << nErr << '\n';

// Close the communication port
nErr = AdsPortCloseEx(pAddr->port);
if (nErr) std::cerr << "Error: AdsPortClose: " << nErr << '\n';
}
//*/

Can some one help me on this? thanks :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions