CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 2008
    Posts
    4

    how to write to parallel port?

    i'm working on a project for school and i'm stuck at this point. i've searched the web using various libraries i've found but none are working work...

    my goal is to write to the parallel port on my pc( which does not have one but i have a usb to parallel converter) and i've tried -

    _outp(port, whatToWrite)

    and i get a priveledge violation of course so i'm wondering if there is a way to write to the port directly. any links or info is appreciated

    my project is to use http://www.electronickits.com/kit/co...lec/ck1601.htm that board. it will activate 8 different solenoids and it will play a game(guitar hero type game). once i get them actually working the coding shouldnt be that bad.

  2. #2
    Join Date
    Nov 2003
    Posts
    1,902

    Re: how to write to parallel port?

    http://www.eee.deu.edu.tr/~ozkurt/co.../inwindows.htm'

    Tie pins 11 and 12 to ground, then this should work:
    Code:
    int main()
    {
        ofstream lpt1("lpt1", ios::out | ios::binary);
        if (!lpt1)
        {
            cerr << "Failed to open lpt1" << endl;
            return 1;
        }//if
    
        char b = 0x11; // 00010001, turn on relays 1 & 5
        if (!lpt1.write(&b, 1))
            cerr << "Write failed" << endl;
    
        return 0;
    }
    gg
    Last edited by Codeplug; December 15th, 2008 at 01:54 PM. Reason: fixed logical OR to bitwise OR - thnx krmed

  3. #3
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: how to write to parallel port?

    Try WinIo from http://www.internals.com/
    Best regards,
    Igor

  4. #4
    Join Date
    Apr 2008
    Posts
    4

    Re: how to write to parallel port?

    i've tried both your methods maybe i'm doing something wrong.

    codeplug i used your code but it always fails to open lpt1
    and what exactly does
    Code:
    "ofstream lpt1("lpt1", ios::out || ios::binary)"
    do?
    creates an ofstream named lpt 1 but the second parameter has me confused

    and Igor i tried using the stuff you gave me the program just runs and nothing happens.


    any other information you provide is appreciated, trying to learn this stuff and its frustrating

  5. #5
    Join Date
    Nov 2003
    Posts
    1,902

    Re: how to write to parallel port?

    >> i have a usb to parallel converter
    When you plug it in, does the OS create a new virtual LPT port? Look in Device Manager under "Ports (COM and LPT)". If you see an "LPT4", for example, that corresponds to your USB to LPT converter - then you'll want to open "LPT4", not 1.

    If you are more comfortable with C file I/O (fopen, fwrite, etc) then use that. The "file" you want to open is the DOS device "LPTx", where x is the number assigned to your USB to LPT device.

    gg

  6. #6
    Join Date
    Jan 2002
    Location
    Houston, TX
    Posts
    1,421

    Re: how to write to parallel port?

    One small change to Codeplug's code is needed (I believe). Change
    Code:
    ofstream lpt1("lpt1", ios::out || ios::binary);
    to
    Code:
    ofstream lpt1("lpt1", ios::out | ios::binary);
    You need to bitwise OR the values, not a logical OR.

    Good luck.
    Be sure to rate those who help!
    -------------------------------------------------------------
    Karl - WK5M
    PP-ASEL-IA (N43CS)
    PGP Key: 0xDB02E193
    PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

  7. #7
    Join Date
    Nov 2003
    Posts
    1,902

    Re: how to write to parallel port?

    Good catch! Fixed in post.

    Try the new code. Change "1" to the number assigned to your device. Ensure pins 11 and 12 are low.

    gg

  8. #8
    Join Date
    Jul 2002
    Posts
    2,543

    Re: how to write to parallel port?


  9. #9
    Join Date
    Nov 2003
    Posts
    1,902

    Re: how to write to parallel port?

    Yes - there's 10 different DLL's that give a user-mode process permission to use the IN and OUT instructions - allowing you to do x86 port I/O.

    Under windows, there is typically already an LPT driver doing this for you. Having a user-mode application fiddling with port registers under the driver's nose isn't exactly ideal. Not to mention that this is for IBM-style LPT hardware. I highly doubt port I/O is going to affect a USB->LPT converter. You will have to use the driver that comes with that device.

    gg

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured