Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Tek-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
Mar 27, 2004
1
0
0
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.

 
Replies continue below

Recommended for you

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.
 
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
 
Status
Not open for further replies.
Back
Top