Advertisement

How do you read data from the serial port under Windows?

Started by March 27, 2002 10:14 PM
3 comments, last by bob_the_third 22 years, 10 months ago
Ideally I''m looking for a solution in C... Any help would be appreciated.
I know this isn''t probably what your looking for but i used Visual Basic this summer and it was very easy. If your interest all you need to do is add an MSCOMM to your gui in Visual Basic and change the settings to waht you want.

I haven''t tried using serial streams in C or C++
sorry
Advertisement
Thanks for the help. I''m not actually the one with the question...a friend of mine is working on a senior design project and needs to write software to interface with the serial port. He''s doing it for a company and unfortunately they don''t use the full version of VB (only the Access version). However thanks again.

P.S. If anyone knows how to do it in Perl that would probably be an option.

Again thanks for the help.
It''s treated somewhat like a file
There is information in the MSDN Library under Win32, Communications on how to do this. The example code contains a few bugs, but is correct for the most part.

I have code for C++ that works on NT; it would probably need some work for 9x. It''s semi-asyncronous, writes are blocking, but reads are event driven. It seemed like the best compromise between complexity and performance to me. It wouolnd''t be too hard to retool it to be completely syncronous (polling reads) or have asyncronous writes.

Here''s the code to open a port:

  HRESULT CComPort::Open(LPCTSTR strComPort, DWORD dwBaud, BYTE byParity, BYTE byStop)	{	HRESULT hr=S_OK;	m_strComPort = strComPort;	m_hComFile = CreateFile(m_strComPort, GENERIC_READ|GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);	if(INVALID_HANDLE_VALUE==m_hComFile)		return(0);	if(FAILED(hr=GetCommState(m_hComFile, &m_dcbCom)))		{		CloseHandle(m_hComFile);		m_hComFile=INVALID_HANDLE_VALUE;		return(hr);		}	m_dcbCom.BaudRate = dwBaud;	m_dcbCom.ByteSize = 8;	m_dcbCom.Parity   = byParity;	m_dcbCom.StopBits = byStop;	m_dcbCom.EvtChar = 0;		if(FAILED(hr=SetCommState(m_hComFile, &m_dcbCom)))		{		CloseHandle(m_hComFile);		m_hComFile=INVALID_HANDLE_VALUE;		return(hr);		}		COMMTIMEOUTS CommTimeOuts;	if(FAILED(hr=GetCommTimeouts(m_hComFile, &CommTimeOuts)))		return(hr);	//all timeouts in ms	CommTimeOuts.ReadIntervalTimeout = 250;	//CommTimeOuts.ReadTotalTimeoutMultiplier = 2*8000/dwBaud;	CommTimeOuts.ReadTotalTimeoutMultiplier = MAXDWORD;	CommTimeOuts.ReadTotalTimeoutConstant = 0;	// double the expected time per character for a fudge factor.	CommTimeOuts.WriteTotalTimeoutMultiplier = 2*8000/dwBaud;	CommTimeOuts.WriteTotalTimeoutConstant = 250;	if(FAILED(hr=SetCommTimeouts(m_hComFile, &CommTimeOuts)))		return(hr);	ORead.hEvent = CreateEvent(0, TRUE, FALSE, 0);	OWrite.hEvent = CreateEvent(0, TRUE, FALSE, 0);	m_thrTranmission = AfxBeginThread(CommWatchProc, this);	m_thrTranmission->m_bAutoDelete=false; //I need to syncronize the delete, so it can''t be done automatically	return(hr);	}[/souce]And in the thread I call[code]if(SUCCEEDED(hr=WaitCommEvent(ComPort->m_hComFile, &dwEvents, &ComPort->ORead)))[/code]And handle whatever case it spits out.  
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
That looks like that should help. Thanks

This topic is closed to new replies.

Advertisement