Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C++ for accessing hardware

Posted on 1999-07-06
8
Medium Priority
?
202 Views
Last Modified: 2010-04-16
What support does c++ provide for accessing hardware like the hardware attached to parallel/serial ports or the pci/isa slots. what particular functions/commands are provided?
0
Comment
Question by:creative12
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 

Expert Comment

by:Elmar
ID: 1199392
Use CreateFile(...) with several parameters. e.g. to access Serial Port dwCreationDisposition parameter must be OPEN_EXISTING, and the hTemplate parameter must be NULL. Read, write, or read-write access can be specified, and the handle can be opened for overlapped I/O.
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 1199393
That is not C++.
There are no standard C++ functions for accessing hardware. Each platform/compiler usually provides some methods though. What OS / compiler are you using.
0
 

Author Comment

by:creative12
ID: 1199394
I am using windows 98. I would be obliged if some one gives me a small example program explaining the procedure.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Expert Comment

by:BSoeters
ID: 1199395
Under Windows 95 (and so probably under 98 too), the following works (at least, most certainly so for Visual C/C++ version 5.0, since I've used it):

#ifndef BYTE
#define BYTE unsigned char
#endif

#ifndef WORD
#define WORD unsigned short int
#endif

void io_outb (WORD address, BYTE value)
{
__asm mov dx,word ptr address
__asm mov al,byte ptr value
__asm out dx,al
}

BYTE io_inpb (WORD address)
{
BYTE result;

__asm mov dx,word ptr address
__asm in  al,dx
__asm mov byte ptr result,al

return (result);
}

You could make a class for doing these operations:

class HardwareIO
{
public:

void io_outb (WORD address, BYTE value);
BYTE io_inpb (WORD address);
};

void HardwareIO::io_outb (WORD address, BYTE value)
{
__asm mov dx,word ptr address
__asm mov al,byte ptr value
__asm out dx,al
}

BYTE HardwareIO::io_inpb (WORD address)
{
BYTE result;

__asm mov dx,word ptr address
__asm in  al,dx
__asm mov byte ptr result,al

return (result);
}

Keep in mind that this will NOT work under NT !
You'll get a privileged instruction error upon trying, because NT does not allow direct access to the hardware.

You could also look into the functions _inp, _inpw, _inpd, _out, _outpw and _outpd for doing hardware I/O.

Hope this helps, I found Elmar's answer rather small and inaccurate....
0
 

Author Comment

by:creative12
ID: 1199396
Thanks Bsoeters
Would you please explain your program a little bit.
I actually need such a program for my wireless lan project, so that i could transfer data from one pc to the other on a wireless link. I therefore wanted to be aware of the capabilities of the software so that i could make changes in the hardware accordingly or vice versa.
0
 
LVL 1

Expert Comment

by:BSoeters
ID: 1199397
Ofcourse, but it's a bit unclear what exactly you want me to explain. I'll asume you want me to explain the given functions though.

For example:

// This function will return a BYTE read from the I/O port
// identified by the address argument
// i.e. io_inpb (0x376) will return one byte read from port 0x376

BYTE io_inpb (WORD address)
{
BYTE result;  // for storing the read byte from port "address"

__asm mov dx,word ptr address   // set-up to read from port "address"
__asm in  al,dx   // read one byte from port "address"
__asm mov byte ptr result,al    // store the read byte into "result"

return (result);    // return the read byte to the caller
}

If this is not what you want, just let me know...

By the way, have you thought of an asynchronous protocol to transer data yet ?
0
 
LVL 86

Accepted Solution

by:
jkr earned 200 total points
ID: 1199398
>>There are no standard C++ functions for accessing hardware

I absolutely agree with KangaRoo - C++ does not provide _any_ means that are directly related to hardware.

However, as you're obviously targetting a MS platform, the most comprehensive explanation on this issue can be found at 'http://msdn.microsoft.com/library/techart/msdn_serial.htm' - the name of the article is 'Serial Communications in Win32'. It describes in detail how to access the COM ports using C/C++ (the same for LPT).

If you prefer a class for doing this, take a look at 'http://www.codeguru.com/misc/serialport.shtml', which provides a complete communication class for all this.

BTW: Using direct port access via assembler works on Win9x, but is an absolute no-no on NT (and Win2k)
0
 
LVL 1

Expert Comment

by:BSoeters
ID: 1199399
jkr: Your comment about direct port access under NT is right. I thought I had said that clearly enough though.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

664 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question