Remote control trough COM port.

This is a discussion on Remote control trough COM port. within the C++ Programming forums, part of the General Programming Boards category; I'm trying to write a program that listens on the COM port. I have a remote control and an IR ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    20

    Remote control trough COM port.

    I'm trying to write a program that listens on the COM port. I have a remote control and an IR that connects to the COM port.
    I wonder if it's possible to use CreateFile() and ReadFile() for this.

    My current code are below but it does'nt present any information to screen (as it should).
    It seems that ReadFile() does'nt get any data. Perhaps it has to be done at a lower level?

    Code:
    int main(void)
    {
    	HANDLE hcom;
    	DCB dcb;
    	LPDWORD nBytesRead = NULL; 
    	OVERLAPPED OverLapped;
    	//DWORD dwEvtMask=0;
    	
    	char inBuffer[128] = {0};
    	const char *pcComPort = "COM4";
    
    	
    	// Create a handle to COM port
    	hcom = CreateFile(pcComPort,		// Using pcComPort
    		GENERIC_READ,	
    		0,								// Opened with exclusive access
    		NULL,							// No security options
    		OPEN_EXISTING,					// Must use OPEN_EXISTING with COM ports
    		FILE_FLAG_OVERLAPPED,			// Overlapped I/O
    		NULL);							// Must be NULL with COM ports
    
    	if (hcom == INVALID_HANDLE_VALUE) {
    		GetError();
    		return 0;
    	}
    
    	// ----------START CONFIGURATION----------
    	if (!GetCommState(hcom, &dcb)) {
    		GetError();
    		return 0;
    	}
    	
    	// Fill in DCB structure.
    	dcb.BaudRate = CBR_9600;
    	dcb.ByteSize = 8;
    	dcb.Parity = NOPARITY;
    	dcb.StopBits = ONESTOPBIT;
    
    	// Update configuration.
    	if (!SetCommState(hcom, &dcb)) {
    		GetError();
    		return 0;
    	}
    	// ----------END CONFIGURATION----------
    
    	while (1) {
    		/*
    		SetCommMask(hcom, EV_RXCHAR);
    		WaitCommEvent(hcom, &dwEvtMask, &OverLapped);
    		*/
    		ReadFile(hcom, inBuffer, sizeof(inBuffer), nBytesRead, &OverLapped);
    		
    		if (nBytesRead)
    			printf("%d, %s", *nBytesRead, inBuffer);
    		_sleep(5);
    	}
    	
    	CloseHandle(hcom);
    	return 0;
    }

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,669
    I posted some code for working with a serial port here: Simple RS232 Question
    You can use it or use it as a reference. If you going to do it yourself, you'll need to read this article (several times): http://msdn.microsoft.com/library/de...sdn_serial.asp

    gg

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    20
    Nice code. I've tried your example code for reading one byte at a time, but it won't detect anything either.
    I've downloaded winlirc and I have to use Data Carrier Detected (DCD) to receive anything. It seems that I have to read up some more to reach my goal.
    Thanks for the link, it seems to include all I need to know.

    PS. Perhaps you could include FormatMessage() in your source to get some usefull out of the return code from GetLastError().

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,669
    >> Nice code.
    Thanks - not too shaby for code that was written almost 10 years ago

    The code does have a bug where CancelIO() is never called when Read() or Write() decides to give up on an overlapped result. This is also true for the example code given in the MSDN article (probably because it was written in, and for 95).

    >> I have to use Data Carrier Detected (DCD)
    SFile is not very flexible to different types of configurations in this regard. You can configure this by in SFile::Open() by setting up the dcb structure to suite you needs. The other mode settings for the port come from the MODESETTINGS global.

    gg

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    20
    I'm having difficulties finding information on DCD(Data Carrier Detected). All I know is that it's a more sofisticated way in recieving data than using RX. The DCB structure does'nt have a member that could relate to DCD either. Would really like to know more on how to use it.

    Your SFile class does'nt have a function called PrivoxyWindowOpen(). I can use SetCommState() to update the DCB structure.

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,669
    DCD is the name of pin one in a 9-pin RS 232 serial port connection. It's not a protocol or anything like that.

    >>Your SFile class does'nt have a function called PrivoxyWindowOpen().
    That is correct.

    >>I can use SetCommState() to update the DCB structure.
    That is also correct.

    gg

  7. #7
    Registered User
    Join Date
    Nov 2004
    Posts
    20
    >>If you going to do it yourself, you'll need to read this article (several times): >>http://msdn.microsoft.com/library/d...msdn_serial.asp
    Well I read the above link and it was a good introduction to windows serial port API. However I found no information that could help me to resolve my problem.

    >>DCD is the name of pin one in a 9-pin RS 232 serial port connection.
    Ok, but how can I use the Windows API to fetch data from a device that uses that pin?

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,669
    What IR device is this?.....model#/manufacturer ect..?

    gg

  9. #9
    Registered User
    Join Date
    Nov 2004
    Posts
    20
    I'ts an 10 in 1 universal remote control from Medion (Germany). Model: MD 4689, but I don't think i'm facing a hardware problem. As I said in my earlier post it does work with winlirc but I have to choose DCD device and not the default RX device as reciever type. Before I can recieve any Raw data.

  10. #10
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,669
    I meant the receiver....but now that I've read the WinLirc website and source code, I've awnsered my own question.

    Take a look at the WinLirc source code and see how it works.

    In short, it monitors changes to the DCD line using SetCommMask(EV_RLSD).

    gg

  11. #11
    Registered User
    Join Date
    Nov 2004
    Posts
    20
    >>Take a look at the WinLirc source code and see how it works.
    I realized that the winlirc code has to hold the answer, so I have already read the source. And I found out that winlirc does'nt use ReadFile(). It does as you said it uses WaitCommEvent() and measure the time between each trigger from EV_RLSD.
    I'm trying to implement a similary approach.
    No wonder I could'nt get ReadFile() to retrieve data.

    Thanks for all the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-29-2008, 12:29 PM
  2. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  3. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 05:23 AM
  4. Which port used MS Win XP remote control?
    By gicio in forum Tech Board
    Replies: 0
    Last Post: 10-23-2002, 08:45 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21