Soros port CBuilder 6.0 alatt

tiszaii tiszaii at axelero.hu
Sun Mar 20 18:23:11 CET 2005


Szál kell, itt egy példa, nem gyomláltam ki, bocs:
/**************************************************************************
  *		Com class part start
*************************************************************************/
//const char** Tcom::com_error_header=com_exception_msg;

__fastcall Tcom::Tcom(const char* com,WORD Baudrate, Byte ByteSize, Byte 
Parity, Byte StopBits, bool& error /*short num */)
//    : thread_num(num)
{
     COMMTIMEOUTS  CommTimeOuts ;
     strcpy(Port,com);
     char buff[100];

//    com_exception.msg_header=com_error_header;
//    com_exception.handle=Application->Handle;
//	sprintf( com_exception.msg,"%s port error: ", com);
//	error = false;
//    try
//    {
		hPort = CreateFile (TEXT(Port), // Port Name (Unicode compatible)
                     GENERIC_READ|GENERIC_WRITE,  // Open for Read-Write
                     0,             // COM port cannot be shared
                     NULL,          // Always NULL for Windows CE
                     OPEN_EXISTING, // For communication resource
                     FILE_FLAG_OVERLAPPED,// Non-overlapped operation only
//                    0,
//                    FILE_ATTRIBUTE_NORMAL,
                     NULL);         // Always NULL for Windows CE
   		if (hPort == INVALID_HANDLE_VALUE)
         {
   /*         if (hPort == INVALID_HANDLE_VALUE)
             {
         		com_exception.set_errorcode(GetLastError());
             	com_exception.set_errorindex(COM_OPEN_ERROR);
                 com_exception.set_errorport(thread_num);
             	throw( 	com_exception );
			}
   */
             error=false;
             sprintf(buff, "%s not open !!", Port);
             XTPrintf(buff);
             return;
		}
         else
         {
             sprintf(buff, "%s open !!", Port);
             XTPrintf(buff);
         }

  		if (!GetCommState(hPort,&dDCB))
     	{
             CloseHandle(hPort);
             error=false;
             sprintf(buff, "%s open,but GetCommState() error was !!", Port);
             XTPrintf(buff);
             return;
     	}

  		dDCB.BaudRate = Baudrate;
  		dDCB.ByteSize = ByteSize;
  		dDCB.Parity   = Parity;
  		dDCB.StopBits =	StopBits;
   		dDCB.fBinary = TRUE ;
         dDCB.fParity = TRUE ;
         dDCB.fAbortOnError= TRUE;


// 	dDCB.Flags 	  = 5;;

  		if (!SetCommState( hPort,&dDCB ))
     	{
             CloseHandle(hPort);
             error=false;
             sprintf(buff, "%s open,but SetCommState() error was !!", Port);
             XTPrintf(buff);
             return;
     	}
         SetupComm( hPort, RXQUEUE, TXQUEUE ) ;
         PurgeComm( hPort, PURGE_TXCLEAR|PURGE_RXCLEAR|
         					   PURGE_TXABORT | PURGE_RXABORT );

  //      SetDCB(FBAUDRATE,FBYTESIZE,FPARITY,FSTOPBIT);

         memset( &osRead, 0, sizeof( OVERLAPPED ) ) ;
         memset( &osWrite, 0, sizeof( OVERLAPPED ) ) ;
       // set up for overlapped I/O
         osRead.hEvent = CreateEvent(NULL,// LPSECURITY_ATTRIBUTES lpsa
			    TRUE, // BOOL fManualReset
			    FALSE, // BOOL fInitialState
			    NULL); // LPTSTR lpszEventName
         if(osRead.hEvent == INVALID_HANDLE_VALUE)
	    {
             CloseHandle(hPort);
             error=false;
             sprintf(buff, "%s open,but Set event error was !!", Port);
             XTPrintf(buff);
             return;
         }

  	    osWrite.hEvent = CreateEvent(NULL,// LPSECURITY_ATTRIBUTES lpsa
			    TRUE, // BOOL fManualReset
			    TRUE, // BOOL fInitialState
			    NULL); // LPTSTR lpszEventName
         if(osWrite.hEvent == INVALID_HANDLE_VALUE)
	    {
             CloseHandle(hPort);
             error=false;
             sprintf(buff, "%s open,but Set event error was !!", Port);
             XTPrintf(buff);
             return;
         }
       	CommTimeOuts.ReadIntervalTimeout = 0;
       	CommTimeOuts.ReadTotalTimeoutMultiplier = 1 ;
       	CommTimeOuts.ReadTotalTimeoutConstant = 0 ;
       // CBR_9600 is approximately 1byte/ms. For our purposes, allow
       // double the expected time per character for a fudge factor.
//      	CommTimeOuts.WriteTotalTimeoutMultiplier = 2*CBR_9600/Baudrate ;
    //     CommTimeOuts.WriteIntervalTimeout = MAX_REC_LENGTH*2;
   		CommTimeOuts.WriteTotalTimeoutMultiplier =  2*CBR_9600/Baudrate ;
       	CommTimeOuts.WriteTotalTimeoutConstant = 20 ;
       	if (!SetCommTimeouts(hPort , &CommTimeOuts ))
         {
             CloseHandle(hPort);
             error=false;
             sprintf(buff, "%s open,but GetCommState() error was !!", Port);
             XTPrintf(buff);
             return;
         }
	  	error=true;
}/* com(...) */

__fastcall Tcom::~Tcom(void)
{
	CloseHandle(hPort);
}/* ~com(void) */

//--------------------------------------------------------------------------
DWORD __fastcall  Tcom::ReadCom(LPSTR lpszBlock, int nMaxLength )
{
    DWORD iBytesReadThisTime=0;
    DWORD iBytesRead=0;
    BOOL       fReadStat ;
    COMSTAT    ComStat ;
    DWORD      dwErrorFlags;
    DWORD   dwError,dwTr;

    dwTr = nMaxLength;

    if (dwTr <= 0)
     return 0;

    while(dwTr)
    {
         fReadStat = ReadFile( hPort, &lpszBlock[iBytesRead], dwTr, 
&iBytesReadThisTime, &osRead ) ;

         if (!fReadStat && (GetLastError() == ERROR_IO_PENDING))
         {
                 if(!GetOverlappedResult( hPort,&osRead, &dwTr, TRUE ))
                 {
                     dwError = GetLastError();
                     if(dwError == ERROR_IO_INCOMPLETE)
                         continue;
                     else
                     {
                         ClearCommErr( hPort, &dwErrorFlags, &ComStat, 
true ) ;
                         dwLength=0;
                         return 0;
                     }
                 }
                 iBytesRead+=dwTr;
         }
         else
         {
                 ClearCommErr( hPort, &dwErrorFlags, &ComStat, true ) ;
                 iBytesRead+=iBytesReadThisTime;
                 break;
         }
     }
    ClearCommErr( hPort, &dwErrorFlags, &ComStat, true ) ;
    dwLength=iBytesRead;

    if (dwLength)
         dump(  lpszBlock, dwLength);
    return dwLength ;
}/*  DWORD __fastcall  Tcom::ReadCommBlock(..)*/

//--------------------------------------------------------------------------
DWORD __fastcall Tcom::WriteCom(char* buf,int len)
{
     DWORD write;
//	DWORD dwBytesSent=0;
	DWORD dwErrorFlags;
    	COMSTAT     ComStat;


	if (len <= 0)
		return -1;

     if(!WriteFile(hPort,buf,len,&write,&osWrite))
  	{
		if(GetLastError() == ERROR_IO_PENDING)
		{
          	while(GetOverlappedResult( hPort, &osWrite, &write, TRUE ))
          	{
            		if (GetLastError() == ERROR_IO_INCOMPLETE)
             	{
                		continue;
             	}
             	else
             	{
                		ClearCommError( hPort, &dwErrorFlags, &ComStat ) ;
                		break;
             	}
			}
		}
		else
		{
             ClearCommError( hPort, &dwErrorFlags, &ComStat ) ;
		}
     }
     return write;
};


Majd a szál Execute-ja.:
void __fastcall Vad::Execute()
{
     COMSTAT     ComStat ;
     DWORD       dwLength;
     DWORD       dwErrorFlags;
     HANDLE      hPort=com->Get_hPort();
     DWORD       dwEvtMask;
	bool	    result=true;

     if (hPort==NULL)
         return;
	while(result && !Terminated)
	{
       	dwEvtMask = 0 ;
		if (WaitCommEvent(hPort, &dwEvtMask, /*&com->osRead*/NULL))
         {

     		switch (dwEvtMask)
         	{
          		case EV_ERR :
                		com->ClearCommErr(hPort, &dwErrorFlags, &ComStat, true );
             		break;
				case EV_RXCHAR:
                     if (!com->ReadCom( com->recbuff, 
/*MAX_REC_LENGTH*/rxBytes))
                     {
  //                         XTPrintf("Rec Error");

                         break;
                     }
                     Synchronize(CompleteRequest);
             		break;
                 case EV_TXEMPTY:
                 {
/*                   if (Closeflg)
                     {
                         if (com)
                             delete com;
                         Closeflg=false;
                         Sleep(100);
                     }
*/
                     break;
                 }
				default:
          	   		break;
         	}
  		}
     	else
		{
         	error = GetLastError();
         	strcpy(errorstr,"WaitCommEvent");
             result = false;
             DoTerminate();
     	}
	}
	if (!error)
     	DoTerminate();
}//Execute()

-- 
Tiszai Istvan
http://www.tiszaii.tk
http://www.3dhistech.com




More information about the Elektro mailing list