Click here to Skip to main content
15,897,891 members
Articles / Programming Languages / C++

Serial Communication in Windows

Rate me:
Please Sign up or sign in to vote.
4.89/5 (67 votes)
2 Aug 200216 min read 621.5K   23.6K   205  
This article gives you a jump start on doing serial communication in Windows NT
// SerialBuffer.h: interface for the CSerialBuffer class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_SerialBuffer_H__FD164A5F_9F09_451B_8E42_180999B4DB9A__INCLUDED_)
#define AFX_SerialBuffer_H__FD164A5F_9F09_451B_8E42_180999B4DB9A__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <string>


class CSerialBuffer  
{
	std::string m_szInternalBuffer;
	CRITICAL_SECTION	m_csLock;
	bool	m_abLockAlways;
	long	m_iCurPos;
	long  m_alBytesUnRead;
	void  Init();
	void	ClearAndReset(HANDLE& hEventToReset);
public:
	inline void LockBuffer() {::EnterCriticalSection(&m_csLock );}
	inline void UnLockBuffer() {::LeaveCriticalSection(&m_csLock );} 
	 
	
	CSerialBuffer( );
	virtual ~CSerialBuffer();

	//---- public interface --
	void AddData( char ch ) ;
	void AddData( std::string& szData ) ;
	void AddData( std::string& szData,int iLen ) ;
	void AddData( char *strData,int iLen ) ;
	std::string GetData() {return m_szInternalBuffer;}

	void		Flush();
	long		Read_N( std::string &szData,long alCount,HANDLE& hEventToReset);
  bool		Read_Upto( std::string &szData,char chTerm,long  &alBytesRead ,HANDLE& hEventToReset);
	bool		Read_Available( std::string &szData,HANDLE & hEventToReset);
	inline long GetSize() {return m_szInternalBuffer.size();}
	inline bool IsEmpty() {return m_szInternalBuffer.size() == 0; }

	bool Read_Upto_FIX( std::string &szData,char chTerm,long  &alBytesRead,HANDLE & hEventToReset);
};

#endif // !defined(AFX_SerialBuffer_H__FD164A5F_9F09_451B_8E42_180999B4DB9A__INCLUDED_)

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions