Click here to Skip to main content
Email Password   helpLost your password?

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 ;) ] 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

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.

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.

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.

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);
}

    // TODO: Add extra initialization here


    SetTimer(1,2,NULL);
    
    return TRUE;  // return TRUE  unless you set the focus to a control

}

// YourAppNameDlg.cpp : implementation file

//


#include "stdafx.h"

#include "YourAppName.h"

#include "YourAppNameDlg.h"


#include "conio.h"


#ifdef _DEBUG

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. ;)

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.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
QuestionHow do you run this code on windows XP?
Bimbrahw
9:45 9 Mar '10  
Great tutorial. Thanks for posting. However, Windows XP does not allow the code to run - instead it returns an error upon entering the timer event:

"Unhandled exception at 0x00412ec8 in ppsc.exe: 0xC0000096: Privileged instruction." This is because XP, 2000, NT do not allow applications to access the ports.

Any suggestions to get around this?

Thanks,

justin
GeneralVC++
kunle isakin
2:26 9 Mar '09  
Hello, can someone help me with articles that could help me learn and understand programming in vc++ from the scratch. i'm an electronics undergraduate
Generalcode doesnt work
ndeshevans
23:32 28 Jul '08  
after implementing the project on port access it gives me an errror :OnTimer something ....
GeneralSerial port accesssing --Plzzz help me
Member 4508922
19:14 20 Jul '08  
i want to that will the same code will work if i want to access the serail port pin(COM1) of my system by specifying the address as 0x3E8(standard address for COM1).

Plzzzz help me.
Questioni want ur help in my project
aatish123
2:58 20 Jul '08  
I am working on on project. in that software works properly on win98 but the same program compiled in xp but not working or execut hole project as it execute in win98.what is the region behind it.Is this of problem of including prots in it becuse in that project we have to out through ports.And geting input from port.
so please help me. give me region and solution of this.
AnswerRe: i want ur help in my project
Eric Strain
12:22 24 Aug '09  
Hi
In Windows XP, 2000 and other 'recent' microsoft operating systems you have to hack the windows IO sub-system in order to access the ports. I found this out the hard way when I was working on a robotics project. MS seems to think that the public has no reason to directly control their own hardware... Try googling for something like 'hardware port access on XP' or something, that may be helpful

none

Generalhi....i want help
aditya pareek
7:17 4 Jul '08  
pls if any body knows the code for how to extract the hardware key in VC++....
pls mail it to me.....
as it is urgent......
Generalhi
amalahema
2:33 30 May '08  
hi,
i do project to control the led(light emiting diode) in the circuit.
I expect to switch on the particular light at clicking the particular check box. The light is switched off when I take out off the tick mark from the particular check box .If I click the check box 1 and 4 means then they are only staying in the on condition.If I take out the 4th Ticks means then the remaining bulb 1 must be remaining in the on position.



This is the normal codings for check box.it is not worked .if anybody knows please send me

Private Sub Check1_click()
If val(Check1.Value) = 1 Then
port1.OutPort &H378, 1 = True
ElseIf val(Check1.Value) = 0 Then
port1.OutPort &H378, 1 = False
Else
MsgBox "nothing occur"
End If
End Sub

if you know the codings in vb,please reply for me.
Questionproject proposals
frieddy03
2:35 4 Apr '08  
haloz! am a computer and electronics student.I have got no idea about the kinds of projects that I can design. Any body with ideas or websites which can help with insights about projects can feel free to help.
Generalplzzzzzzzzzzz help
vickyz
2:16 14 Feb '08  
hi i have already post messages butnobody replied me.
i m doing project on hardwares and thier softwares for my BS project can anybody help meeeeeeeeeeeeeeeeeeeeeee?Frown
Generalplz help me quite new
vickyz
4:08 13 Feb '08  
hi daygiri me quite new member.i belongs to A.K.Infact i m working on p[roject of home automation in order to design a device that controls the voltage level and developing its software as well.will u help me during my project? Also i have tried program for accessing parallel port but it gives runtime error.......Frown Frown me anxiously waiting for ur reply.
QuestionProblem with adding members
kingofwestern
7:40 26 Nov '07  
Thanks for the explanation. You explained how to enter the class wizard but I don't seem to get, what do you exactly mean by right click of check boxes? I can't find the option of entering the class wizard. Any further details will be very helpful. Thanks again.
Questionhi Daygiri
luixilva
14:03 10 Oct '07  
i need to replace a printer for another computer to send the "output" of the lpt1 to a file.

I have some problems to read the data, status and control ports, I mean, i can't manage the busy and acknowledge in the second computer, in fact, i can't read what the first computer is sending to the printer. Could you please help me? or telling me what can i do for solve my problem?

If u want, i can send u the code i have written at this point.

Thank you in advance for your attention.

Regards
Questionwrite assemby code
far_zad3212000
13:05 24 Feb '07  
hi,i have a problem:how can i write assembly code in c# like this:
mov ax,bx
mov cl,2
..
..
..
QuestionHelp Required : USB Programming
wahiajay
23:10 18 Sep '06  
Hi Jay,

Just few days back I got an opportunity to develop a tool in MFC which will fetch data from USB device(mouse,keyboard,dongles) and display it on the GUI.
I am required to use windows HID class for the same.

Please help me out if you have any idea about this stuff.
In case there is any specific set of APIs which are required to be invoked then please help me out.

You can mail me at wahiajay@yahoo.com

Thanks,
Ajay
AnswerRe: Help Required : USB Programming
Danial Kahani
12:37 4 Feb '07  
i don;t thik that u can find api.
THERE IS A BOOK: USB complete
just read this.

Generalhow same can be done for serial port
keenalex
21:44 5 Feb '06  
hi,

i want to develop same for serial port. do u hav any hint, i m confused abt how to get pin values, all statistics etc

waiting for reply
Questionreading the parallel port
jama687
13:44 28 Oct '05  
Hi, I can read the input data from the parallel port. I have a thread that is reading continously the parallel port and updating a variable with the data.

I want to know if there is a better way to do it, because I need a quick response in my application (software side).

I have two push buttons connected to the parallel port (parallel port is already configured as bi-direccional) so, I want to detect the order in which I press the buttons, if the combination is correct it will display a message.

Could you please help me?

Thanks in advance.
Generalprogramming EEPROM in VC++
plknam2003
1:21 25 Oct '05  
i am building a application to program EEPROM 24CXX in VC++.can you help me to control IIC bus?i do not know to adjust Timer in microsecond.
Generalhow do I know whether a device is attached to my USB port or not
Anonymous
5:20 3 Jun '05  
I'm facig a problem. One application is developed where we are taking a person's photograph programetically using a webcam. Now the problem is if the clients computer does not have a webcam it is showing an error. so my requirement is I want to write a code through which it will detect whether a device is attached to USB or not. I want to write it in VC++.
Generalaccess to database through VC++
Sugima
0:56 6 May '05  
how to acess the data from MS Access through VC++?
GeneralIs It Possible
Silent Warrior
11:32 31 Mar '05  
:-DPlease can we get an introduction of com1 and com2 introduction for how we can take controle, i hope you can.

thanks,
GeneralUSB again
Shabayek
13:34 4 Nov '04  
i have the same problem
i need it bad for my graduation project
plzzzzzzzzzzzzzzzzzzzzzz any body help

Shabayek
plz mail me if anything new
shabayek@walla.com
GeneralProgram Crashes
wicked_guy
6:22 30 Sep '04  
hi,

I have created the program shown to control the parallel port. The problem is that the program kept asking for the file 'MFC42D.dll', so I downloaded it. Afterwards, it kept crashing. plz help.

Thanx in advance
GeneralRe: Program Crashes
Sreekanth Muralidharan
19:47 19 May '05  
Hello
I doubt the program would work gracefully. Windows will not allow user programs to access the hardware using Rin0 system calls like _inp(). This can be used only if the libraries which allow such Ring0 access are loaded while the system boots.

This program gives out a run-time error as and when it starts. I have tried the same in WinXP where levels of access are still more complex.
Regards


Sreekanth Muralidharan,
Corporate Systems Consultant [Embedded Systems],
INDIA


Last Updated 10 Jun 2002 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010