Accessing the parallel port via Win API
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Accessing the parallel port via Win API

  1. #1
    Join Date
    Feb 2003
    Posts
    1

    Accessing the parallel port via Win API

    I've coded a program (attempted to anyways) which detects data being recieved on the parallel port and prints it..

    Code:
    #include <windows.h>
    #include <cstdlib>
    
    void printError(char *caption) {
    
        char buffer[70];
        FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), 0, buffer, 70, NULL);
        MessageBox(NULL, buffer, caption, MB_OK);
    
        SetLastError(0);
    
    }
    
    DWORD dummy;
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                       char *psCmdLine, int iCmdShow) {
    
        SetLastError(0);
    
        HANDLE hParallel = CreateFile("LPT1", 0, 0, NULL, OPEN_EXISTING,
                                      FILE_ATTRIBUTE_NORMAL, NULL);
        printError("Felstatus efter CreateFile");
    
        SetCommMask(hParallel, EV_RXFLAG);
        printError("Felstatus efter SetCommMask");
    
        COMMTIMEOUTS parallelTimeouts;
    
        parallelTimeouts.ReadIntervalTimeout         = MAXDWORD;
        parallelTimeouts.ReadTotalTimeoutMultiplier  = 0;
        parallelTimeouts.ReadTotalTimeoutConstant    = 0;
        parallelTimeouts.WriteTotalTimeoutMultiplier = 0;
        parallelTimeouts.WriteTotalTimeoutConstant   = 0;
    
        SetCommTimeouts(hParallel, &parallelTimeouts);
    
        printError("Felstatus efter SetCommTimeouts");
    
        int svar;
    
        do {
    
            WaitCommEvent(hParallel, &dummy, NULL);
            printError("Felstatus efter WaitCommEvent");
    
            char dasByte = 0;
            ReadFile(hParallel, &dasByte, 1, &dummy, NULL);
    
            printError("Felstatus efter ReadFile");
    
            char buffer[4];
            itoa(dasByte, buffer, 10);
            MessageBox(NULL, buffer, "Recieved a byte! Got...", MB_OK);
    
            svar = MessageBox(NULL, "Read another byte?", "Hmm..", MB_YESNO);
    
        } while(svar != IDNO);
    
    }
    printError shows that no function sets any error value.

    A friend of mine has built a remote controller which interfaces thru the parallel port, and that's what I've tried the program with so far.

    As you may have guessed it doesn't work..

    His device connects to the data lines of the parallel port. WaitCommEvent detects nothing when we send data. We've verified that his hardware is correct using an old app for win95 (which probably uses direct hw access) that correctly detects data being sent.

    Need the non-data lines of the parallel port be a particular state for windows to take it that data is being sent?

    Is it just some silly mistake in my program?

    Any help appreciated..

    Ulfalizer

  2. #2
    Join Date
    Apr 2005
    Posts
    6

    Re: Accessing the parallel port via Win API (Up)

    Hello everybody,

    i found out this post and i'm disapointed because there is no answer. Because i've got the same problem.

    I use win2000, visualC++ 7.1, and the call of SetCommTimeouts() returns false. Why?

    Thanks.

  3. #3
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: Accessing the parallel port via Win API (Up)

    Quote Originally Posted by r0d
    I use win2000, visualC++ 7.1, and the call of SetCommTimeouts() returns false. Why?
    What is the error code returned? Take a look at the following FAQ...
    Ciao, Andreas

    "Software is like sex, it's better when it's free." - Linus Torvalds


    Article(s): Allocators (STL) Function Objects (STL)

  4. #4
    Join Date
    Apr 2005
    Posts
    6

    Re: Accessing the parallel port via Win API

    hello, thanks for reply.

    This is the error i got:
    87: The parameter is incorrect.

    But i call WriteFile(m_hFile,...) just before SetCommTimeouts(..), and the WriteFile work well, with no error. So i think my m_hFile handle is good.

  5. #5
    Join Date
    Apr 2005
    Posts
    6

    Re: Accessing the parallel port via Win API

    if someone can help me, i copy here the parts of my code that shows the problem (i have removed the superfluous).

    Code:
    m_hFile = CreateFile("LPT1",		// device name
    		GENERIC_READ|GENERIC_WRITE,		// access mode
    		0,		// share mode
    		0,		// security attributes
    		OPEN_EXISTING,	// creation dispositions
    		FILE_ATTRIBUTE_NORMAL,		// flags and attributes
    		0);				// template file
    						
    	DWORD dwByteWritten;
    	WriteFile(m_hFile,	// file handle 
    		(const char *)strMsgToSend,	// buffer to send 
    		(DWORD) strMsgToSend.GetLength(),   // buffer size 
    		&dwByteWritten,		// number of written bytes 
    		NULL);	// overlapped 	
    
    
    	SetCommMask(m_hFile, EV_RXFLAG);
    	COMMTIMEOUTS parallelTimeouts;
    	parallelTimeouts.ReadIntervalTimeout = MAXDWORD;
    	parallelTimeouts.ReadTotalTimeoutMultiplier = 0;
    	parallelTimeouts.ReadTotalTimeoutConstant = 0;
    	parallelTimeouts.WriteTotalTimeoutMultiplier = 0;
    	parallelTimeouts.WriteTotalTimeoutConstant = 0;
    
    	if (!SetCommTimeouts(m_hFile, &parallelTimeouts))
    	     ReportLastError("SetCommTimeouts");
    
    	DWORD dummy;
    	if (!WaitCommEvent(m_hFile, &dummy, NULL))
    		ReportLastError("WaitCommEvent");
    		
    	char szBuf1 = 0;
    	if(!ReadFile(m_hFile, &szBuf1, 1, &dummy, NULL))
    	         ReportLastError("ReadFile");
    So the Write works well (no error and the datas are well sent on the port), but i get error 87 (parameter incorrect) when i call SetCommTimeOuts. And the same error for WaitCommEvent().

    Any idea?

  6. #6
    Join Date
    Jul 2005
    Posts
    1

    Re: Accessing the parallel port via Win API

    The parallelTimeouts.WriteTotalTimeoutConstant variable has a minimum value of 2000 (2 seconds) on WinXP.
    Default value is set to 300000 (5 mins).

  7. #7
    Join Date
    Oct 2005
    Posts
    1

    Re: Accessing the parallel port via Win API

    hi all,

    (at first sorry for my english... ;o) )

    the problem i see: WaitCommEvent and also SetCommMask is couple to DCB, a struct made for serial communication with your COMx. LPTx are centronic interfaces, which use STROBE, ACKNOWLEDGE and BUSY for hardware handshake. RS-232(UART) use CTS, RTS... . you can't handle your communication over LPTx with xxxCOMMxxx.

    best regards markus
    Last edited by pinback; October 31st, 2005 at 06:46 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)