Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Indy exception



Permlink Replies: 1 - Last Post: Apr 9, 2018 10:26 AM Last Post By: Remy Lebeau (Te... Threads: [ Previous | Next ]
Mirco Malagoli

Posts: 4
Registered: 5/13/00
Indy exception
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 7, 2018 2:41 AM
Hi,
I used this simple code for years on C++2009

void __fastcall TThIO::Execute()   //TThread
{
	int i, j, cnt, n;
	DWORD tk;
 
	while( !Terminated && pSock )
	{
		try
		{
			if( !pSock->Connected() )
			{
				pSock->Connect();
				continue;
			}
 
			ProtocolloModbus();
 
		}catch(...)
		{
			flInit= false;
			Sleep( 200);
			pSock->Disconnect();
		}
	}//while
}
 
void __fastcall TThIO::ProtocolloModbus()
{
	int i, j, cnt, n;
 
	cnt= 0;
	do
	{
		try
		{
			bufRx[cnt]= pSock->IOHandler->ReadByte();
			cnt++;
 
			if( cnt >= 2 && bufRx[0] == bufTx[0] && bufRx[1] == bufTx[1] )
			{
				if( cnt >= 7 )
				{
					if( bufRx[7] == bufTx[7] )
					{
						n= (bufRx[4]<<8)&0xff00 | bufRx[5];
						if( cnt >= n+6 || (bufRx[7] == 15 && cnt == 11+1) )
							break;
					}
					else if( bufRx[7] == bufTx[7]+0x80 && cnt == 8+1 )
						break;
				}
			}
		}
		catch( ...)
		{
			cnt= 0;
			break;
		}
	}
	while( (unsigned int)cnt < sizeof(bufRx) && !Terminated && pSock->Connected() );
 
	if( Terminated || !pSock || !pSock->Connected() )
		return;
 
	if( cnt > 0 && bufRx[0] == bufTx[0] && bufRx[1] == bufTx[1] )
	{
		if( bufRx[7] == MB_READ_INPUTS )
		{
			for( j= 0; j < 8 && j < MAX_INGRESSI; j++ )
				ingresso[j]= (bufRx[9]>>j) & 0x01;
		}
 
		else if( bufRx[7] == MB_READ_MULTIPLE_REGISTER )
			DecodificaSonde();
	}
 
	//CONTROLLO SE AGGIORNARE LE USCITE O LEGGERE GLI INGRESSI
	if( flTx || SomeBlink() )
	{
		flTx= false;
		TxUscite();   //pSock->IOHandler->Write( memTx);
	}
	else if( GetTickCount() - timOut > 400 || timOut == 0 )
	{
		timOut= GetTickCount();
		LeggiSonde();   //pSock->IOHandler->Write( memTx);
	}
}


Now i have only recompiled on C++10S and after some second i receive an "External exception EEFFACE".
Is there anything to consider in working the indy components when changing compiler version?

Thanks!
Mirco
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Indy exception
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 9, 2018 10:26 AM   in response to: Mirco Malagoli in response to: Mirco Malagoli
Mirco Malagoli wrote:

I used this simple code for years on C++2009
<snip>
Now i have only recompiled on C++10S

What is "C++10S" supposed to be? C++Builder 10.x? Seattle, Berlin, or
Tokyo?

after some second i receive an "External exception EEFFACE".

EEFFACE means a C++ language exception was thrown and not caught in the
C++ code, it was caught by an exception handler inside the Delphi RTL,
which doesn't know how to handle C++ exceptions.

Is there anything to consider in working the indy components when
changing compiler version?

This is not a compiler issue. Your C++ code has a bug that threw an
uncaught exception. So catch it, and debug your code. Start with
using 'catch (const Exception &)' instead of 'catch (...)', the latter
doesn't always work correctly.

I would suggest simplifying your thread Execute() method:

void __fastcall TThIO::Execute()
{
	while( !Terminated )
	{
		try
		{
			pSock->Connect();
			try
			{
				while( !Terminated )
					ProtocolloModbus();
			}
			__finally
			{
				pSock->Disconnect();
			}
		}
		catch (const Exception &)
		{
			flInit = false;
			Sleep(200);
		}
	}
}


Also, Modbus TCP has structure to it, but your ProtocolloModbus()
method is not really reading that structure very well. Try something
more like this:

void __fastcall TThIO::ProtocolloModbus()
{
	TIdBytes header, data;
 
	pSock->IOHandler->ReadBytes(header, 6);
 
	int n = ((header[4] << 8) & 0xFF00) | header[5];
 
	if( (header[0] == bufTx[0]) && (header[1] == bufTx[1]) )
	{
		pSock->IOHandler->ReadBytes(data, n);
		// process data as needed...
	}
	else
		pSock->IOHandler->Discard(n);
}


--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02