Serial Programming the Win32 api

This is a discussion on Serial Programming the Win32 api within the Windows Programming forums, part of the Platform Specific Boards category; I am having a lot of trouble opening up a serial communications port with win32 in VC++ 6.0. This is ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    1

    Serial Programming the Win32 api

    I am having a lot of trouble opening up a serial communications port with win32 in VC++ 6.0.


    This is the following code I have. It is in a very standard win32 application, one that just displays hello world (at this point, I am just trying to get the thing opened).
    The following is under a WM_CREATE message. The handlePort and config are instantiated before the switch messsage in the WndProcedure as a HANDLE and DCB respectively.

    Code:
    handlePort = CreateFile("COM1", GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);     
    		if (handlePort = INVALID_HANDLE_VALUE){
    			if(GetLastError()==ERROR_FILE_NOT_FOUND){
    				MessageBox(hWnd,"MAy","May",MB_OK);
    			}else{
    			MessageBox(hWnd,"OKAY","OKAY",MB_OK);
    			}
    		}
    		GetCommState(handlePort,&config);
    		config.BaudRate = 38400;
    		config.Parity = NOPARITY;
    		config.ByteSize = 8;
    		config.StopBits = 1;
    		SetCommState(handlePort, &config);
    
    		// instance an object of COMMTIMEOUTS.
    		COMMTIMEOUTS comTimeOut;                   
    		// Specify time-out between charactor for receiving.
    		comTimeOut.ReadIntervalTimeout = 3;
    		// Specify value that is multiplied 
    		// by the requested number of bytes to be read. 
    		comTimeOut.ReadTotalTimeoutMultiplier = 3;
    		// Specify value is added to the product of the 
    		// ReadTotalTimeoutMultiplier member
    		comTimeOut.ReadTotalTimeoutConstant = 2;
    		// Specify value that is multiplied 
    		// by the requested number of bytes to be sent. 
    		comTimeOut.WriteTotalTimeoutMultiplier = 3;
    		// Specify value is added to the product of the 
    		// WriteTotalTimeoutMultiplier member
    		comTimeOut.WriteTotalTimeoutConstant = 2;
    		// set the time-out parameter into device control.
    		SetCommTimeouts(handlePort,&comTimeOut);
    Can anyone tell me what is wrong with my code?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,676
    So what's the problem you are having?

    I wouldn't mess with the timeouts - since you're using overlapped I/O, you are already in control of any timeouts by virtue of waiting on read/write completion and "giving up" when you want.

    gg

  3. #3
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Code:
     
    FILE* pWrite = NULL;
    FILE* pRead = NULL;
     
    pWrite = fopen("COM1:" , "w+b");
    pRead = fopen("COM1:" , "r+b");
     
    // use them as normal streams
     
    fclose(pRead);
    fclose(pWrite);
    its just that simple.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    What exactly is failing?

    I see two things off the top of my head.....

    Not setting the size of the DCB DCBlength prior to getting the port settings. Tells GetCommState which version of the structure you are using.

    Setting the timeouts very low, they are in millisec, not seconds. I do not think yours will work.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,676
    You're also not setting the flow control.

    gg

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,676
    In case you're trying to write, you only open for reading.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Win32 API or Win32 SDK?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 07-20-2005, 03:26 PM
  2. FILES in WinAPI
    By Garfield in forum Windows Programming
    Replies: 46
    Last Post: 10-02-2003, 06:51 PM
  3. Serial Communications with win32 api
    By Blackthorne in forum C Programming
    Replies: 1
    Last Post: 01-26-2003, 11:45 AM
  4. OLE Clipboard :: Win32 API vs. MFC
    By kuphryn in forum Windows Programming
    Replies: 3
    Last Post: 08-11-2002, 05:57 PM
  5. Thread Synchronization :: Win32 API vs. MFC
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 08-09-2002, 09:09 AM

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