Click here to Skip to main content
Click here to Skip to main content

Hardware Interface Programming in VC++ - Part I (Port Access)

, 9 Jun 2002
Rate this:
Please Sign up or sign in to vote.
A simple port access tutorial for the hardware interface developer

Sample Image

For the students of electronics...

As a student of electronics I find that students in colleges don't have much time to learn the windows programming because they just manage to test their devices with small DOS based programs coded in C or C++ and concentrates on the electronics of their hardware. [Other reasons are also there Wink | ;) ] But after completing the hardware part they want to give it professional look by programming window based software for their hardware.

Students developing hardware can not get much help on programming side by their teacher (they both are just expert in electronics). The electronics magazines publishing such hardware projects also uses C and C++.

So, this article is for the hardware developer who have knowledge of C and C++ and who want to switch over to window based application for their device.

In this first part...

In this first part of HIPIVC++ we will see how to check the port continuously using timers. We will build a simple (and sample) dialog based application to check whether the Pin No. 10, 11, 12, 13, and 15 of LPT1 is high or low. I think it is a good starting point.

Start with Application Wizard

To Skip this section:
Just generate dialog based application with all default option if you are familiar with AppWizard or follow the procedure
  • Click on File | New; than chose MFC AppWizard(exe) and give Project name; click OK or press Enter you will see MFC AppWizard-Step 1
  • In MFC AppWizard-Step 1 chose Dialog based application type and click Next.
  • Click next for the Step 2 and 3 and click Finish in last (fourth) Step.
  • Click OK in New Project Information dialog. AppWizard will create simple dialog based application for you.
  • Press F7 to build EXE of your project.
  • Press Ctrl+F5 to execute it.
  • You will see dialog box with OK and Cancel buttons and text "TODO: Place dialog controls here."
  • 40% Done. These and next instructions are to make life easy in MFC for the new MFC programmer.

Modifying the resources

At this stage you will see dialog resource is opened in the window (AppWizard will do this as default). Now we will modify the dialog box resource as per our requirement.
  • Select and Delete "TODO:Place..." text, and Cancel button.
  • Change caption of OK button to Close (right click on button | Property > Caption)
  • From Control tool bar create five check box and caption them as Pin 10, Pin 11, Pin 12, Pin 13 and Pin 15. Also change IDs to IDC_PIN10, IDC_PIN11, IDC_PIN12, IDC_PIN13, IDC_PIN15 (note that we haven't use Pin 14 that is not for status port)
  • Size the dialog box if you need! You may also insert static text like "LPT 1" or "Status Port: 0x379" or your name.
  • Don't forget to save your work. The final dialog box should be as the screen shot given at top.

Add member variables

Now we will add member variables to check boxes which then changes their value according to high or low value of status pins of parallel port.

  • right click on any check box and click on ClassWizard... the class wizard dialog box will appear.
  • Select Member Variables tab if not. Project should be YourAppName and Class name should be CYourAppNameDlg
  • You will see IDC_PIN10 to IDC_PIN15 and IDOK.
  • Select IDC_PIN10 and click Add Variable... than Add Member Variable dialog box will appear.
  • Give Member variable name as m_Pin10
  • Do same for IDC_PIN11 to IDC_PIN15 (please forget pin 14)
Now your list of Member Variables should look like this...
Control IDs:        Type        Member
IDC_PIN10            CButton        m_Pin10
IDC_PIN11            CButton        m_Pin11
IDC_PIN12            CButton        m_Pin12
IDC_PIN13            CButton        m_Pin13
IDC_PIN15            CButton        m_Pin15
80% Done. Save your work.

Final Coding

Here you will insert your code to input form the port and update the check box view according to the status of parallel port pins.
  • Click Class View tab on Workspace window, you will see YourAppName classes.
  • Right click on CYourAppNameDlg chose Add Member Variable...
  • Enter Variable Type as int and Variable Name as in379 it would assign value of port 0x379. Keep Access type as it is.(Public)
  • Same way add more five int variable for each pin and name them as nPin10, nPin11, nPin12, nPin13, nPin15
  • Now right click on CYourAppNameDlg class and chose Add Windows Message Handler... then from New windows messages/events list select WM_TIMER and click Add Handler button. The WM_TIMER will be inserted in Existing message/event handlers, select it there if not and click Edit Existing button.
  • Paste the code given below the bold part is generated automatically by Wizard but they can help you to find the proper place for pasting the code.

void CPpscDlg::OnTimer(UINT nIDEvent) 
{
    // TODO: Add your message handler code here and/or call default
    
    in379=_inp(0x379);
    nPin10=in379 & 64;
    nPin11=in379 & 128;
    nPin12=in379 & 32;
    nPin13=in379 & 16;
    nPin15=in379 & 8;

    if(nPin10==0)
        m_Pin10.SetCheck(0);
    else if(nPin10==64)
        m_Pin10.SetCheck(1);

    if(nPin11==0)
        m_Pin11.SetCheck(0);
    else if(nPin11==128)
        m_Pin11.SetCheck(1);

    if(nPin12==0)
        m_Pin12.SetCheck(0);
    else if(nPin12==32)
        m_Pin12.SetCheck(1);

    if(nPin13==0)
        m_Pin13.SetCheck(0);
    else if(nPin13==16)
        m_Pin13.SetCheck(1);

    if(nPin15==0)
        m_Pin15.SetCheck(0);
    else if(nPin15==8)
        m_Pin15.SetCheck(1);

    CDialog::OnTimer(nIDEvent);
}
  • Now scroll upward in same file (YourAppNameDlg.cpp) and paste following single line code in BOOL CPpscDlg::OnInitDialog() function.

    // TODO: Add extra initialization here

    SetTimer(1,2,NULL);
    
    return TRUE;  // return TRUE  unless you set the focus to a control
}
  • If you try to build your project at this point you will get error C2065: '_inp' : undeclared identifier
  • So include conio.h in YourAppNameDlg.cpp file as follows...

// YourAppNameDlg.cpp : implementation file
//

#include "stdafx.h"
#include "YourAppName.h"
#include "YourAppNameDlg.h"

#include "conio.h"

#ifdef _DEBUG
  • Complete. Now you may build and run the Parallel Port Status Check Version 1.0 (I am using the name PPSC for short)

How it works

This is very simple program which just inputs from the port. The _inp(unsigned short) function returns the value at the port whose number is passed to it. The SetTimer() is used to generate a timer event at every 2 millisecond. The OnTimer() catches the timer event and updates the check box. Initially (when the printer is not connected or DB-25 is open) the parallel port status pins should be at high logic except Pin 11 which is hardware inverted. So initially all check boxes except for Pin 11 should be checked.

You can see that in my screenshot image of PPSC, the Pin 10 is also at logic low it is because I have damaged this pin during developing some parallel port device. By the way I have found the fault and the solution for preventing other pins, but discussion of such problem is out of scope of this site. It is just for the hobbies who have chance to damage their parallel port. Wink | ;)

I will continue if you want...

It is all upon your feedback...

In next part I want to cover parallel port fully, and than I want to write for the serial port and USB. But again it is all upon your feedback...

See you in next part, Take care.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Jaygiri
Web Developer
India India
Loving Electronic more than VC++ but getting more work in software field. Now going towards ASP and Database.

Comments and Discussions

 
NewsParallel Port Digital Oszilloscope and Logic Analyzer PinmvpElmue24-Apr-10 4:49 
Hello

If you came to this article from Google because you are looking for a way to measure digital signals via parallel port then have a look at the following article:
17 Channel Logic Analyzer!

* Digital Oscilloscope / Logic Analyzer with up to 17 input lines
* Uses the parallel (printer) port for input.
* While Logic Analyzers normally are very expensive, this one is for zero cost
* Written in speed optimized C++ to get the maximum possible sample rates
* The program is a stand alone single EXE file which neither needs any additional DLLs nor any framework. It runs out of the box
* Runs on all Windows platforms (95, 98, ME, NT, 2000, XP, 2003, Vista, 2008, Windows 7, 2008 R2)
* Runs on 32 Bit Windows and 64 Bit Windows
* When first run, installs a driver to access hardware IO ports on all NT platforms
* Plays a sound via PC speaker to prove that the driver works correctly
* Shows the actual state of the input lines life in the LED's of the GUI
* Captures up to 500.000 samples / second (depending on your hardware and OS)
* Captures to memory (rather than disk) for maximum speed
* Capturing runs always with the maximum frequency that the hardware allows
* Memory saving capture technology stores only the changes of the input lines rather than storing all lines with each sample that is taken
* The capture process is independent of data analyzing so you can try various settings to display the captured data in the best way
* The analyzer generates coloured oscilloscope diagrams with a raster grid
* The output is written into an HTML file and one or multiple GIF files
* So the analyzer results can easily be shared with other people who must not install any program to view a proprietary binary format
* You can write an individual heading into each analyzer output to distinguish them later in a multitude of files
* When analyzing data, you can choose to delete old analyzer output or keep the old files
* The time axis shows absolute or relative time with microsecond precision (Performance Counter)
* Automatic detection of inactive input lines shows only the channels that have activity
* Automatic detection of idle time cuts out long phases of inactivity
* Automatic detection of input frequency chooses the adequate raster unit like on a real oscilloscope (10 µs, 20 µs, 25 µs, 50 µs, 100 µs, etc..)
* Automatic detection of context switches of the operating system
* Decoding of serial protocols like I2C Bus, PS/2 Bus, SMBus, ModBus, SPI Bus, Asynchronous (RS232, RS422, RS485), Infrared Remote Controls, Smartcards is partially already implemented or may be added by you.
* Decoded serial data (Start Bit, Parity, Acknowledge, Stop Bit) and the Byte that they represent is written into the diagram
* Very cleanly written source code with a proper error handling and plenty of comments

Elmü
QuestionHow do you run this code on windows XP? PinmemberBimbrahw9-Mar-10 9:45 
GeneralVC++ Pinmemberkunle isakin9-Mar-09 2:26 
Generalcode doesnt work Pinmemberndeshevans28-Jul-08 23:32 
GeneralSerial port accesssing --Plzzz help me PinmemberMember 450892220-Jul-08 19:14 
Questioni want ur help in my project Pinmemberaatish12320-Jul-08 2:58 
AnswerRe: i want ur help in my project PinmemberEric Strain24-Aug-09 12:22 
Generalhi....i want help Pinmemberaditya pareek4-Jul-08 7:17 
Generalhi Pinmemberamalahema30-May-08 2:33 
Questionproject proposals Pinmemberfrieddy034-Apr-08 2:35 
Generalplzzzzzzzzzzz help Pinmembervickyz14-Feb-08 2:16 
Generalplz help me quite new Pinmembervickyz13-Feb-08 4:08 
QuestionProblem with adding members Pinmemberkingofwestern26-Nov-07 7:40 
Questionhi Daygiri Pinmemberluixilva10-Oct-07 14:03 
Questionwrite assemby code Pinmemberfar_zad321200024-Feb-07 13:05 
QuestionHelp Required : USB Programming Pinmemberwahiajay18-Sep-06 23:10 
AnswerRe: Help Required : USB Programming PinmemberDanial Kahani4-Feb-07 12:37 
Questionhow same can be done for serial port Pinmemberkeenalex5-Feb-06 21:44 
Questionreading the parallel port Pinmemberjama68728-Oct-05 13:44 
Generalprogramming EEPROM in VC++ Pinmemberplknam200325-Oct-05 1:21 
Questionhow do I know whether a device is attached to my USB port or not PinsussAnonymous3-Jun-05 5:20 
Generalaccess to database through VC++ PinsussSugima6-May-05 0:56 
GeneralIs It Possible PinmemberSilent Warrior31-Mar-05 11:32 
GeneralUSB again PinsussShabayek4-Nov-04 13:34 
GeneralProgram Crashes Pinmemberwicked_guy30-Sep-04 6:22 
GeneralRe: Program Crashes PinmemberSreekanth Muralidharan19-May-05 19:47 
GeneralRe: Program Crashes Pinmembershibukv26-Sep-05 20:32 
GeneralRe: Program Crashes PinmemberAnil K P19-Dec-07 20:36 
GeneralDoesn't work in NT, 2k, ... PinmemberJomim30-Sep-04 4:43 
GeneralRe: Doesn't work in NT, 2k, ... PinsussAnonymous1-Jan-05 7:35 
GeneralParallel port LPT Pinmembersajmon16-Jul-04 2:11 
GeneralUSB port acess Pinmemberiliyas pasha3-May-04 19:31 
GeneralRe: USB port acess Pinmemberkanthi pavan16-Aug-04 3:12 
GeneralWriteFile function can't send data to parallel port Pinsussdongfeng zhang29-Apr-04 6:45 
GeneralRe: WriteFile function can't send data to parallel port PinmemberMichele Solazzi14-Oct-04 11:02 
GeneralRe: WriteFile function can't send data to parallel port PinmemberLEDUC yves12-Apr-06 21:36 
GeneralRe: WriteFile function can't send data to parallel port Pinmembervirajitha21-Dec-06 0:38 
GeneralIt won't work under WinNT PinmemberBlueMonk9-Feb-04 1:16 
Generaldetect all the h/w and s/w in the system PinsussNAGESH.N5-Feb-04 21:33 
GeneralError Box Pinmemberkovacsi30-Dec-03 23:32 
GeneralRe: Error Box PinmemberJomim30-Sep-04 4:45 
Generalthanks but.... PinsussAnonymous4-Oct-04 2:00 
Generaltext to print Pinmemberramya8313-Nov-03 2:29 
GeneralPlease help me PinmemberAbu Ismail4-Sep-03 0:15 
QuestionUSB!? PinmemberCarlos Souza14-Jul-03 5:25 
GeneralHardware Access in VC++ PinsussAnonymous5-Jul-03 12:42 
GeneralRe: Hardware Access in VC++ Pinmemberhojjat_ollah pakzad16-Aug-05 17:05 
GeneralECP/EPP problems!!! PinmemberGeorgi27-Jun-03 2:02 
Generalfeedback on hardware... Pinmembernikoladsp12-Jun-03 14:09 
GeneralHardware Interface Programming in VC++ - Part I (Port Access) PinmemberMigkikas11-Jun-03 2:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150414.1 | Last Updated 10 Jun 2002
Article Copyright 2002 by Jaygiri
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid