Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hardware Interfacing thru C..

Status
Not open for further replies.

faraznasim

Electrical
Joined
Mar 27, 2004
Messages
1
Location
US

Hi,
I m relatively a beginner in C, but after covering some major topics, i want to learn something abt interfacing hardware like printer parallel port or some LED etc thru C.
can anyone guide me, or refer to any online material..

Thanks,
FaRaZ.

 
Under DOS it is simple -- under WINDOWS -- well I don't do windows.

<nbucska@pcperipherals.com>
 
Windows is just as simple, just need to know the tricks ;) ... Actually you can't direct interface to the hardware via an application (ring 3 code). However you can treat any port like a stream device.
 
claganga:
Reading Chinese is simple, too, after you learn a few
thousand symbols...



<nbucska@pcperipherals.com>
 
nbucska:
I guess I can't argue with that.

faraznasim:
Humm what exactly would like to write to a serial port, I'll give you a windows example if you want.
 
Opening a serial port is done in a very illogical way: you have to make a call to CreateFile(), ReadFile(), and WriteFile() functions with the COM port specified as a string value, i.e. "COM1" or "COM2", etc. You can find good code examples on how to do this on I think it should have been functions with names such as OpenDevice(), SendDeviceData(), and ReadDeviceData(), but what do I know.

Once you have opened the port, it's as simple as transmitting (WriteFile()) and receiving (ReadFile()) bytes.

Also take a look at the SetCommTimeouts() and SetCommState() functions, with the relevant COMMTIMEOUTS and DCB data structures as input. The MSDN library gives you all the help you'll need on these topics, and its available online as well.

Hope this helped.
 
Hi-

The C programming enviornment for a 'nix machine is
much easier than going through much rigamarole in the
windows enviornment.

If you feel comfortable going into say the Linux land,
you will find all sorts of tools that will assist you
in playing with I/O.

I might recommend the following link:



as a rather painless introduction to the subject.


Cheers,

Rich S.
 
Since you mention "an LED," and a parallel port, I assume you're tinkering with physical hardware. As of NT4 and 98SE, Windows added a Virtual Device Manager layer in the executive between programs and hardware. It's much more difficult to bit-bang to physical hardware than before. I/O instructions like "in" and "out" no longer write directly to the port. It WAS a lot easier in the DOS days.

Some things are fairly straightforward, like getting to RTS/CTS and DSR/DTR bits on a serial port via the MSCOMM.DLL package. Just don't expect reliable real-time performance when you do. You can blink and read Morse Code, for example, but don't think you'll be able to bit-blast a 100kHz burst pattern for X10 emulation accurately at zero-crossing through most Windows tools.

If you're just trying to print something to a parallel port, just open the printer as a stream i/o device and "printf" to it. If you want to light a couple of LED's, I'd suggest putting them on the serial port (or just watch the lights on an external modem.) For much beyond that, get an I/O board with a Mfgr's driver interface to C, or get ready to write your own replacement port driver, a task NOT for newbies. ;-)

Howrd
 
A very useful page for this kind of thing is

The direct page for parallel ports is

See the section on windows drivers that can be used to allow direct IO on windows, some are free, some are commercial, but it may help your problem.

Cheers

B
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top