Serial.cpp
上传用户:like263
上传日期:2017-04-25
资源大小:2k
文件大小:4k
源码类别:

Windows编程

开发平台:

Visual C++

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