SerialCom.cpp
上传用户:sarrow1992
上传日期:2022-07-29
资源大小:2k
文件大小:4k
源码类别:

串口编程

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "SerialCom.h"
  3. CSerial::CSerial()
  4. {
  5. m_hComDev=NULL; 
  6. m_bOpened=FALSE;
  7. memset(dataRx,'',50);
  8. }
  9. CSerial::~CSerial()
  10. {
  11. m_hComDev=NULL; 
  12. m_bOpened=FALSE;
  13. }
  14. BOOL CSerial::Open(int nPort, int nBaud)
  15. {
  16. if( m_bOpened == TRUE) 
  17. {
  18. MessageBox(0,"The Com Port has been opened.","",MB_OK);
  19. return( TRUE );
  20. }
  21. char szPort[15];
  22. DCB dcb;
  23. wsprintf( szPort, "COM%d", nPort );
  24. m_hComDev=CreateFile( "COM1", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL );
  25. if(m_hComDev == INVALID_HANDLE_VALUE)
  26. {
  27. MessageBox(0,"The Com Port has been occupied!","",MB_OK);
  28. return  FALSE;
  29. }
  30. memset( &m_OverlappedRead, 0, sizeof( OVERLAPPED ) );
  31. memset( &m_OverlappedWrite, 0, sizeof( OVERLAPPED ) );
  32. COMMTIMEOUTS CommTimeOuts;
  33. CommTimeOuts.ReadIntervalTimeout = 0xFFFFFFFF;
  34. CommTimeOuts.ReadTotalTimeoutMultiplier = 0;
  35. CommTimeOuts.ReadTotalTimeoutConstant = 0;
  36. CommTimeOuts.WriteTotalTimeoutMultiplier = 0;
  37. CommTimeOuts.WriteTotalTimeoutConstant = 5000;
  38. SetCommTimeouts(m_hComDev, &CommTimeOuts);
  39. m_OverlappedRead.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL);
  40. m_OverlappedWrite.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL);
  41. dcb.DCBlength = sizeof( DCB );
  42. GetCommState( m_hComDev, &dcb );
  43. dcb.BaudRate = nBaud;
  44. dcb.ByteSize = 8;
  45. dcb.Parity = 0;
  46. if( !SetCommState( m_hComDev, &dcb) || !SetupComm( m_hComDev, 512,512) ||
  47. m_OverlappedRead.hEvent == NULL || m_OverlappedWrite.hEvent == NULL )
  48. {
  49. DWORD dwError = GetLastError();
  50. if( m_OverlappedRead.hEvent != NULL) CloseHandle( m_OverlappedRead.hEvent);
  51. if( m_OverlappedWrite.hEvent != NULL) CloseHandle( m_OverlappedWrite.hEvent);
  52. CloseHandle( m_hComDev );
  53. return FALSE;
  54. }
  55. m_bOpened = TRUE;
  56. return m_bOpened;
  57. }
  58. //***************************************************************************
  59. int CSerial::InBufferCount( void )
  60. {
  61. if( !m_bOpened || m_hComDev == NULL ) return( 0 );
  62. DWORD dwErrorFlags;
  63. COMSTAT ComStat;
  64. ClearCommError( m_hComDev, &dwErrorFlags, &ComStat );
  65. return (int)ComStat.cbInQue;
  66. }
  67. //***************************************************************************
  68. DWORD CSerial::ReadData( void *buffer, DWORD dwBytesRead)
  69. {
  70. if( !m_bOpened || m_hComDev == NULL ) return 0;
  71. BOOL bReadStatus;
  72. DWORD dwErrorFlags;
  73. COMSTAT ComStat;
  74. ClearCommError( m_hComDev, &dwErrorFlags, &ComStat );
  75. if( !ComStat.cbInQue ) return 0;
  76. dwBytesRead = min(dwBytesRead,(DWORD) ComStat.cbInQue);
  77. // SetFilePointer(m_hComDev,NULL,NULL,FILE_BEGIN);
  78. bReadStatus = ReadFile( m_hComDev, buffer, dwBytesRead, &dwBytesRead, &m_OverlappedRead );
  79. if( !bReadStatus )
  80. {
  81. if( GetLastError() == ERROR_IO_PENDING )
  82. {
  83. WaitForSingleObject( m_OverlappedRead.hEvent, 2000 );
  84. return dwBytesRead;
  85. }
  86. return 0;
  87. }
  88. return dwBytesRead;
  89. }
  90. //****************************************************************************
  91. DWORD CSerial::SendData( const char *buffer, DWORD dwBytesWritten)
  92. {
  93. if( !m_bOpened || m_hComDev == NULL ) return( 0 );
  94. BOOL bWriteStat;
  95. // SetFilePointer(m_hComDev,NULL,NULL,FILE_BEGIN);
  96. bWriteStat = WriteFile( m_hComDev, buffer, dwBytesWritten, &dwBytesWritten, &m_OverlappedWrite );
  97. if( !bWriteStat)
  98. {
  99. if ( GetLastError() == ERROR_IO_PENDING ) 
  100. {
  101. WaitForSingleObject( m_OverlappedWrite.hEvent, 1000 );
  102. return dwBytesWritten;
  103. }
  104. return 0;
  105. }
  106. return dwBytesWritten;
  107. }
  108. //****************************************************************************
  109. BOOL CSerial::Close(void)
  110. {
  111. m_bOpened = FALSE;
  112. return CloseHandle(m_hComDev);
  113. }