⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 newcomthread.cpp

📁 这是一个西门子 ModBus 协议的通讯调试软件。(原码)
💻 CPP
字号:
// NewComThread.cpp: implementation of the CNewComThread class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
//#include "ModBusToolDlg.h"
#include "NewComThread.h"


#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//extern CHVALUE  p_CHValue;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CNewComThread::CNewComThread()
{
    lpThread.m_hCom = NULL;
    lpThread.m_Rdos.hEvent  = NULL;
	lpThread.m_Wdos.hEvent  = NULL; 
}

CNewComThread::~CNewComThread()
{

   if ( lpThread.m_Rdos.hEvent != NULL )
   {
	   CloseHandle( lpThread.m_Rdos.hEvent );
	   lpThread.m_Rdos.hEvent = NULL;
   }
   if ( lpThread.m_Wdos.hEvent != NULL )
   {
       CloseHandle( lpThread.m_Wdos.hEvent );
	   lpThread.m_Wdos.hEvent = NULL;
   }
   if ( lpThread.m_hCom != NULL )
   {
       CloseHandle( lpThread.m_hCom ); 
	   lpThread.m_hCom = NULL;
   }

}

BOOL CNewComThread::SendData_Command( unsigned char *sendBuff, int sendLen ) 
{

	DWORD         dwLen = sendLen; 
	DWORD         dwLenght = 0;
	DWORD         bt = ZERO;
    BYTE          sByte[100];
    BOOL          fs;
	BOOL          m_Result;
    short         sH = 0,sL = 0; 
    
	if ( blcommunicationMode )
	{
	   for ( int n=0; n<sendLen; n++ )
           sByte[n] = sendBuff[n];

       dwLen = sendLen;
	   fs = WriteFile( lpThread.m_hCom,&sByte,dwLen,&dwLenght,&(lpThread.m_Wdos) );
    }
    else
	{
	   dwLen = sendLen;
	   fs = WriteFile( lpThread.m_hCom,sendBuff,dwLen,&dwLenght,&(lpThread.m_Wdos) );
	}

	if (!fs)
	{
	  m_Result = GetOverlappedResult( lpThread.m_hCom,&lpThread.m_Wdos,&bt,TRUE ); 
	}
		
	Sleep( uiInterval );
	pszReadValue = "";
    return ( TRUE );

}

CString CNewComThread::Get_ReceiveValue()
{
	return pszReadValue;
} 
void CNewComThread::Set_TimeInterval( UINT nInterval ) 
{
	uiInterval = nInterval;
}
void CNewComThread::Set_Communication_Mode( BOOL blMode ) 
{
	blcommunicationMode = blMode;
}
BOOL CNewComThread::ReceiveData_Command()
{

        COMSTAT       comStat;     	
	    DWORD         dwRV = 0 ;
		DWORD         dwLen = 0;
		DWORD         dwLenght = 0;
        DWORD         dwError = 0; 
	    DWORD         bt = ZERO;
        BOOL          blRead = TRUE;
		BOOL          fs;
		BOOL          m_Result;
        CString       strRead;
        
		pszReadValue = "";
        while( blRead )
		{
		  Sleep(2);
		  ::ClearCommError( lpThread.m_hCom,&dwError,&comStat ); 

		   dwLen = min((DWORD)1,comStat.cbInQue);
		   if ( dwLen>0 )
		   {
			  //fs=ReadFile(cThread.m_hCom,&iBuffe,1,&dwLenght,&(cThread.m_Rdos));

		 	  fs = ReadFile( lpThread.m_hCom,&dwRV,1,&dwLenght,&(lpThread.m_Rdos) );
			  if ( !fs )
			  {
			   	 m_Result = GetOverlappedResult( lpThread.m_hCom,&lpThread.m_Rdos,&bt,TRUE );
			  }
			  if ( blcommunicationMode )
			     strRead.Format( "%02X",dwRV );
			  else
                 strRead.Format( "%c",dwRV );

			  pszReadValue += strRead;
		   }
		   else
		   {
			   blRead = FALSE;
		   } 
		}
		return ( TRUE );

}
		
BOOL CNewComThread::InitNumber(BYTE  bPort,DWORD dwBuad,    //9600
				           char  cParity,   //N
				           BYTE  bDataBit,  //8
				           BYTE  bStepBit)  //1

{
	DCB    m_dcb;
    char   szBaud[50];
	char   fPort[10];
	DWORD  dwEvents;
	
	if ( bPort<0 || bPort>4 )
	{
	   AfxMessageBox( "设置的端口号有误,请更改......" );
	   return ( FALSE );
	}
 
	lpThread.cParam.bPort    = bPort;
	lpThread.cParam.dwBuad   = dwBuad;
	lpThread.cParam.cParity  = cParity;
	lpThread.cParam.bDataBit = bDataBit;
	lpThread.cParam.bStepBit = bStepBit;

	::GetCommState( lpThread.m_hCom,&m_dcb ); 

	m_dcb.DCBlength = sizeof(m_dcb);
	m_dcb.BaudRate  = lpThread.cParam.dwBuad;
	m_dcb.ByteSize  = lpThread.cParam.bDataBit;
	m_dcb.Parity    = NOPARITY;
	m_dcb.StopBits  = lpThread.cParam.bStepBit;   
	
	::SetCommState( lpThread.m_hCom,&m_dcb );
    
	lpThread.m_Rdos.Offset     = 0;
    lpThread.m_Rdos.OffsetHigh = 0; 
	lpThread.m_Rdos.hEvent = CreateEvent( NULL,TRUE,FALSE,NULL ); 

    if ( lpThread.m_Rdos.hEvent==NULL )
	{
		CloseHandle( lpThread.m_Rdos.hEvent );
	    return ( FALSE );
	}

	lpThread.m_Wdos.Offset     = 0;
    lpThread.m_Wdos.OffsetHigh = 0; 
	lpThread.m_Wdos.hEvent = CreateEvent( NULL,TRUE,FALSE,NULL ); 
    if ( lpThread.m_Wdos.hEvent == NULL )
	{
		CloseHandle( lpThread.m_Wdos.hEvent );
	    return ( FALSE );
	}
    
	if( lpThread.m_hCom != NULL)
	{
		CloseHandle( lpThread.m_hCom );
		lpThread.m_hCom = NULL;
	}
    wsprintf( fPort,"COM%d",lpThread.cParam.bPort ); 	
	lpThread.m_hCom = CreateFile( fPort,
		                      GENERIC_READ|GENERIC_WRITE,
							  ZERO,
							  NULL,
							  OPEN_EXISTING,
							  FILE_FLAG_OVERLAPPED,
							  NULL );

    if ( lpThread.m_hCom == INVALID_HANDLE_VALUE )
	{
		//LeaveCriticalSection(&cThread.m_Lock);
		return ( FALSE );
	}
	else
	{
		::SetupComm( lpThread.m_hCom,2048,2048 );
		::PurgeComm( lpThread.m_hCom,PURGE_RXABORT|PURGE_TXABORT|PURGE_RXCLEAR|PURGE_TXCLEAR );
			
	}
    
	lpThread.m_Commtimeouts.ReadIntervalTimeout         = 1000;
	lpThread.m_Commtimeouts.ReadTotalTimeoutConstant    = 1000;
	lpThread.m_Commtimeouts.ReadTotalTimeoutMultiplier  = 1000;
	lpThread.m_Commtimeouts.WriteTotalTimeoutConstant   = 1000;
	lpThread.m_Commtimeouts.WriteTotalTimeoutMultiplier = 1000;
	
	::SetCommTimeouts( lpThread.m_hCom,&lpThread.m_Commtimeouts ); 

	dwEvents = EV_RXFLAG|EV_RXCHAR;
	::SetCommMask( lpThread.m_hCom,dwEvents );
	
	sprintf( szBaud,"baud=%d parity=%c data=%d stop=%d",
		                         lpThread.cParam.dwBuad,
								 lpThread.cParam.cParity,
								 lpThread.cParam.bDataBit,
								 lpThread.cParam.bStepBit );
    
	::GetCommState( lpThread.m_hCom,&m_dcb ); 
	::BuildCommDCB( szBaud,&m_dcb ); 	
	::SetCommState( lpThread.m_hCom,&m_dcb ); 
    ::PurgeComm( lpThread.m_hCom,PURGE_RXABORT|PURGE_TXABORT|PURGE_RXCLEAR|PURGE_TXCLEAR ); 
  
    lpThread.lpCHValue.commandIndex = 0;  
    return ( TRUE );

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -