Jump to content
  • Advertisement
Sign in to follow this  
Xeile

Comm port issue

This topic is 4493 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello folks, I got a serial port link with an external device. Connecting with the comm port works just fine. After sending a string to the device with the help of WriteFile(...) function, I get a response. No problems here, yet when I try to purge the receive buffer (input buffer), nothing seems to happen. The old data still appears on the same position. Any one knows what I might do wrong here? Class declaration:
class CCOMPort
{
private:
	typedef	struct
	{
		BYTE 			Port;
		UINT			Baud;
		BYTE			DataBits;
		BYTE			Parity;
		BYTE			StopBits;
	}COM_Settings;

public:
	CCOMPort();
	~CCOMPort();

	void				SetPort(BYTE Port);
	void				SetBaudRate(UINT Baud);
	void				SetDataBits(BYTE Bits);
	void				SetParity(BYTE Parity);
	void				SetStopBits(BYTE Bits);
	BYTE				GetPort();
	UINT				GetBaudRate();
	BYTE				GetDataBits();
	BYTE				GetParity();
	BYTE				GetStopBits();

	bool				Connect();
	bool				Disconnect();
	bool				Connected();
	bool				Update(bool COMPortChange = false);

	bool				Recv(void* Buffer, DWORD BytesToRecv, DWORD* BytesRecv);
	bool				Send(const void* Buffer, DWORD BytesToSend, DWORD* BytesSend);
	bool				Purge();

private:
	bool				Close();

private:
	HANDLE				hCom;
	COM_Settings			Binding; // Old and new
};

Class definition:
#pragma hdrstop
#include "COMPort.h"
#pragma package(smart_init)

CCOMPort::CCOMPort()
:	hCom(NULL)
{
	memset(&Binding, 0, sizeof(Binding));
}

CCOMPort::~CCOMPort()
{
	Disconnect();
}

void CCOMPort::SetPort(BYTE Port)
{
	Binding.Port = Port;
}

void CCOMPort::SetBaudRate(UINT Baud)
{
	Binding.Baud = Baud;
}

void CCOMPort::SetDataBits(BYTE Bits)
{
	Binding.DataBits = Bits;
}

void CCOMPort::SetParity(BYTE Parity)
{
	Binding.Parity = Parity;
}

void CCOMPort::SetStopBits(BYTE Bits)
{
	Binding.StopBits = Bits;
}

BYTE CCOMPort::GetPort()
{
	return Binding.Port;
}

UINT CCOMPort::GetBaudRate()
{
	return Binding.Baud;
}

BYTE CCOMPort::GetDataBits()
{
	return Binding.DataBits;
}

BYTE CCOMPort::GetParity()
{
	return Binding.Parity;
}

BYTE CCOMPort::GetStopBits()
{
    return Binding.StopBits;
}

bool CCOMPort::Connect()
{
	return Update(true);
}

bool CCOMPort::Disconnect()
{
	if(Connected())
	{
		Close();
		return true;
	}
	else
		return false;
}

bool CCOMPort::Connected()
{
	return hCom ? true : false;
}

bool CCOMPort::Update(bool PortChange)
{
	static char Comport[] = {	'C', 'O', 'M', '0' + Binding.Port, '\0' };
	static DCB NewBinding = {	sizeof(DCB), Binding.Baud, 	TRUE,
								Binding.Parity ? TRUE : FALSE,
								FALSE, FALSE, TRUE, FALSE, FALSE,
								FALSE, FALSE, FALSE, FALSE, FALSE,
								FALSE, FALSE, 0x0000, 0x0800, 0x0200,
								Binding.DataBits, Binding.Parity,
								Binding.StopBits, 0x11, 0x13, 0x00,
								0x00, 0x00, 0x0000 };
	COMMTIMEOUTS Timeouts = {	25, 25, 5, 25, 5 };

	if(PortChange)
	{
		Close();
		hCom = CreateFile(	Comport,GENERIC_READ | GENERIC_WRITE,
							NULL, NULL, OPEN_EXISTING,
							FILE_ATTRIBUTE_NORMAL, NULL);
		if(hCom == INVALID_HANDLE_VALUE)
		{
			hCom = NULL;
			return false;
		}
	}

	if(!SetCommState(hCom, &NewBinding))
		return false;
	if(!SetCommTimeouts(hCom, &Timeouts))
		return false;
		
	return true;
}

bool CCOMPort::Close()
{
	if(hCom)
	{
		CloseHandle(hCom);
		hCom = NULL;
	}
	return true;
}

bool CCOMPort::Recv(void* Buffer, DWORD Size, DWORD* BytesRead)
{
	return ReadFile(hCom, Buffer, Size, BytesRead, NULL) ? true : false;
}

bool CCOMPort::Send(const void* Buffer, DWORD Size, DWORD* BytesSend)
{
   	return WriteFile(hCom, Buffer, Size, BytesSend, NULL);
}

bool CCOMPort::Purge()
{
	return PurgeComm(hCom, PURGE_RXCLEAR);
}

What I do first before connecting, is setup the baud rate etc. After that I connect manualy (button press), send command in order to let the device respond. After reading the input buffer, I want to purge it to clear the room, yet nothing happens to the input buffer. What am I doing/understanding wrong? Thanks in advance Regards, Xeile

Share this post


Link to post
Share on other sites
Advertisement
im more familiar with overlapped i/o

PurgeComm(, PURGE_RXABORT | PURGE_RXCLEAR)

any difference?

Kuphryn

Share this post


Link to post
Share on other sites
I'd say kuphryn is right on the money. That was exactly what I was going to say. You want to abort and clear.

I recommend downloading Portmon if you aren't already using it. Very very handy!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!