Click here to Skip to main content
16,015,583 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have developed two dialog based application One for reading from COM1 port and other
to write on COM1 port.Read application waits for com event.Problem is write application able to open com port if read applcation is not running.but not able to
open com port if read application is running.This is not threading problem,
Its something is regarding serial Port Basic concepts.
My Read and write functions are as follows.I need to extend this basic functionality
furthur,but before this simple read and write needs to work
C++
int CReadDlg::Readme()
{
	CString PortSpecifier = "COM1";
	DCB dcb;
	int retVal;
	BYTE Byte;
	DWORD dwBytesTransferred;
	DWORD dwCommModemStatus;
	HANDLE hPort = CreateFile(
	PortSpecifier,
	GENERIC_READ,
	0,
	NULL,
	OPEN_EXISTING,
	0,
	NULL
	);
	if (!GetCommState(hPort,&dcb))
	return 0x100;
	dcb.BaudRate = CBR_9600; //9600 Baud
	dcb.ByteSize = 8; //8 data bits
	dcb.Parity = NOPARITY; //no parity
	dcb.StopBits = ONESTOPBIT; //1 stop
	if (!SetCommState(hPort,&dcb))
	return 0x100;
	SetCommMask (hPort, EV_RXCHAR | EV_ERR); //receive character event
	WaitCommEvent (hPort, &dwCommModemStatus, 0); //wait for character
	if (dwCommModemStatus & EV_RXCHAR)
	ReadFile (hPort, &Byte, 1, &dwBytesTransferred, 0); //read 1
	else if (dwCommModemStatus & EV_ERR)
	retVal = 0x101;
	retVal = Byte;
	CloseHandle(hPort);
	return retVal;
}

void CSimulatorDlg::OnWrite() 
{
	
	CString PortSpecifier = "COM1";
	CString data = "A";// "0xAA1g/L8H8L12.30xF5";
	DCB dcb;
	DWORD byteswritten;
HANDLE hPort = CreateFile(PortSpecifier,GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
	if (!GetCommState(hPort,&dcb))
	return ;
	dcb.BaudRate = CBR_9600; //9600 Baud
	dcb.ByteSize = 8; //8 data bits
	dcb.Parity = NOPARITY; //no parity
	dcb.StopBits = ONESTOPBIT; //1 stop
	if (!SetCommState(hPort,&dcb))
	return ;
	bool retVal = WriteFile(hPort,data,1,&byteswritten,NULL);
	CloseHandle(hPort); //close the handle	
}
Posted
Updated 20-Jun-12 17:42pm
v2
Comments
Sergey Alexandrovich Kryukov 20-Jun-12 23:54pm    
There is a wrapper class around Windows API you use, why not using it? -- please see my answer.
Did you run it under debugger to pin-point the problem? Your "not able to open" is not very descriptive. What goes wrong, exactly?
--SA

I hope this CodeProject article can help you:
Serial library for C++[^].

Basically, you need to use the class CSerial. Do you use it? This CodeGuru article provides a short overview of its usage:
http://www.codeguru.com/cpp/i-n/network/serialcommunications/article.php/c2503/CSerial--A-C-Class-for-Serial-Communications.htm[^].

—SA
 
Share this answer
 
v2
Comments
adityarao31 20-Jun-12 23:55pm    
Will you please Tell me what is going wrong in my code ?
Sergey Alexandrovich Kryukov 21-Jun-12 0:28am    
Hard to say exactly, but the code looks very wrong because you mix up UI and serial port communications. You need to isolate it thoroughly. First of all, use pure blocking (sync) calls but run all communications is a separate thread.
--SA
A serial port can be opened by only one thread/application at time. So an attempt to open COM1 by your write application will fail if the port is already opened by the read application. It is always good practice to check the return value of CreateFile() for INVALID_HANDLE_VALUE and call GetLastError() upon failure.

The only solution is to put the read and write code into one application that opens the serial port upon start providing the handle for reading and writing.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900