扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
#define MESSAGELENGTH 100 class CMyDialog : public CDialog { protected: VARIANT InBuffer; VARIANT OutBuffer; CMSComm m_Com; public: ...... } BOOL CMyDiaLog::OnInitDialog() { CDialog::OnInitDialog(); m_Com.SetCommPort(1); if (!m_Com.GetPortOpen()) { m_Com.SetSettings("57600,N,8,1"); m_Com.SetPortOpen(true); m_Com.SetInBufferCount(0); SetTimer(1,10,NULL); InBuffer.bstrVal=new unsigned short[MESSAGELENGTH]; OutBuffer.bstrVal=new unsigned short[MESSAGELENGTH]; OutBuffer.vt=VT_BSTR; } return true; } void CMyDiaLog::OnTimer(UINT nIDEvent) { if (m_Com.GetInBufferCount()>=MESSAGELENGTH) { InBuffer=m_Com.GetInput(); // handle the InBuffer. // Fill the OutBuffer. m_Com.SetOutput(OutBuffer); } CDialog::OnTimer(nIDEvent); } |
用该控件传输的数据是UNICODE格式。关于UNICODE和ANSI的关系和转换请参看MSDN。
关于该控件的其他详细资料请查看MSDN关于COMM CONTROL部分。
二、直接用VC++访问串口。
在VC++中,串口和磁盘文件可以统一的方式来简单读写。这两者几乎没有什么不同,只是在WINDOWS 9X下磁盘文件只能做同步访问,而串口只能做异步访问。
CreateFile:用指定的方式打开指定的串口。通常的方式为
m_hCom = CreateFile( "COM1", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL );
m_hCom为文件句柄。GENERIC_READ | GENERIC_WRITE指定可以对串口进行读写操作。第三个参数0表示串口为独占打开。OPEN_EXISTING表示当指定串口不存在时,程序将返回失败。 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED则表示文件属性。当打开串口时,必须指定 FILE_FLAG_OVERLAPPED,它表示文件或设备不会维护访问指针,则在读写时,必须使用OVERLAPPED 结构指定访问的文件偏移量。
ReadFile:读取串口数据。
WriteFile:向串口写数据。
CloseHandle:关闭串口。
COMMTIMEOUTS:COMMTIMEOUTS主要用于串口超时参数设置。COMMTIMEOUTS结构如下:
typedef struct _COMMTIMEOUTS { DWORD ReadIntervalTimeout; DWORD ReadTotalTimeoutMultiplier; DWORD ReadTotalTimeoutConstant; DWORD WriteTotalTimeoutMultiplier; DWORD WriteTotalTimeoutConstant; } COMMTIMEOUTS,*LPCOMMTIMEOUTS; |
typedef struct _OVERLAPPED { DWORD Internal; DWORD InternalHigh; DWORD Offset; DWORD OffsetHigh; HANDLE hEvent; } OVERLAPPED; |
BOOL CSerial::Open( int nPort, int nBaud ) { if( m_bOpened ) return( TRUE ); char szPort[15]; DCB dcb; wsprintf( szPort, "COM%d", nPort ); m_hComDev = CreateFile( szPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL ); if( m_hComDev == NULL ) return( FALSE ); memset( &m_OverlappedRead, 0, sizeof( OVERLAPPED ) ); memset( &m_OverlappedWrite, 0, sizeof( OVERLAPPED ) ); COMMTIMEOUTS CommTimeOuts; CommTimeOuts.ReadIntervalTimeout = 0xFFFFFFFF; CommTimeOuts.ReadTotalTimeoutMultiplier = 0; CommTimeOuts.ReadTotalTimeoutConstant = 0; CommTimeOuts.WriteTotalTimeoutMultiplier = 0; CommTimeOuts.WriteTotalTimeoutConstant = 5000; SetCommTimeouts( m_hComDev, &CommTimeOuts ); m_OverlappedRead.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL ); m_OverlappedWrite.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL ); dcb.DCBlength = sizeof( DCB ); GetCommState( m_hComDev, &dcb ); dcb.BaudRate = nBaud; dcb.ByteSize = 8; if( !SetCommState( m_hComDev, &dcb ) || !SetupComm( m_hComDev, 10000, 10000 ) || m_OverlappedRead.hEvent == NULL || m_OverlappedWrite.hEvent == NULL ){ DWORD dwError = GetLastError(); if( m_OverlappedRead.hEvent != NULL ) CloseHandle( m_OverlappedRead.hEvent ); if( m_OverlappedWrite.hEvent != NULL ) CloseHandle( m_OverlappedWrite.hEvent ); CloseHandle( m_hComDev ); return FALSE; } m_bOpened = TRUE; return m_bOpened; } int CSerial::InBufferCount( void ) { if( !m_bOpened || m_hComDev == NULL ) return( 0 ); DWORD dwErrorFlags; COMSTAT ComStat; ClearCommError( m_hIDComDev, &dwErrorFlags, &ComStat ); return (int)ComStat.cbInQue; } DWORD CSerial::ReadData( void *buffer, DWORD dwBytesRead) { if( !m_bOpened || m_hComDev == NULL ) return 0; BOOL bReadStatus; DWORD dwErrorFlags; COMSTAT ComStat; ClearCommError( m_hComDev, &dwErrorFlags, &ComStat ); if( !ComStat.cbInQue ) return 0; dwBytesRead = min(dwBytesRead,(DWORD) ComStat.cbInQue); bReadStatus = ReadFile( m_hComDev, buffer, dwBytesRead, &dwBytesRead, &m_OverlappedRead ); if( !bReadStatus ){ if( GetLastError() == ERROR_IO_PENDING ){ WaitForSingleObject( m_OverlappedRead.hEvent, 2000 ); return dwBytesRead; } return 0; } return dwBytesRead; } DWORD CSerial::SendData( const char *buffer, DWORD dwBytesWritten) { if( !m_bOpened || m_hComDev == NULL ) return( 0 ); BOOL bWriteStat; bWriteStat = WriteFile( m_hComDev, buffer, dwBytesWritten, &dwBytesWritten, &m_OverlappedWrite ); if( !bWriteStat){ if ( GetLastError() == ERROR_IO_PENDING ) { WaitForSingleObject( m_OverlappedWrite.hEvent, 1000 ); return dwBytesWritten; } return 0; } return dwBytesWritten; } |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者