LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-24-2004, 04:52 AM   #1
mala fide
Member
 
Registered: Nov 2004
Location: Espoo, Finland
Distribution: Kubuntu 7.10
Posts: 48

Rep: Reputation: 15
accessing ports


I was thinking about using my computer to control other hardware
and I have all these pins on the motherboard so...

How can I set and reset the individual pins in serial port or printer port (parallel port?) using C or C++. Can I read them as well.

I've heard that at least in windows accessing serial port is possible but accessing printer port is (almost) impossible.

Sorry if this seems to be not very well thought out but my project is just beginning and I'm exploring possibilities.
 
Old 11-24-2004, 06:04 AM   #2
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
You can't control the individual pins on the serial port as far as I'm aware, but you can with the parallel (printer) port. I recommend that you spend a little money and buy a second parallel port on an expansion card, just in case you accidentally damage it (by connecting the wrong pins).

The easiest way is probably to write a user-space device driver for the parallel port (using the ppdev device, /dev/printer1); I haven't looked into doing this because it's a new feature in the 2.6 kernels. See http://people.redhat.com/twaugh/parport/html/ppdev.html

There's some older information on using /dev/port or writing a kernel driver at http://www.tldp.org/HOWTO/IO-Port-Programming.html
 
Old 11-24-2004, 08:08 AM   #3
mala fide
Member
 
Registered: Nov 2004
Location: Espoo, Finland
Distribution: Kubuntu 7.10
Posts: 48

Original Poster
Rep: Reputation: 15
So Parallel port is the way to go. Great! Even more pins *drool*
although i'm not sure if I can use these status and such pins.

I was hoping that there would be sort of built-in methods of manipulating the pins because
the word driver scares me even if it is just a certain kind of program.

Also I found out that if I'm going to do this I need to study a lot more and we'll see how badly I want this.
 
Old 11-27-2004, 05:40 PM   #4
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
What kind of device do you want to control?

For most purposes, you can rig up the data pins as outputs from the computer, and status pins as inputs from the controlled device (if you want any feedback from it).

If it's just a question of turning relays on and off, then you can just set the appropriate bits on the data lines. To control serial devices, the serial port is the better option; it can be set to emit just about any kind of data frame.

I've just skimmed through it; ppdev doesn't look that scary to me. You need to call ioctl() several times, passing it the file descriptor from the /dev/parport0 file. Call ioctl with PPCLAIM (to get the port) then PPEXCL (to get an exclusive lock).

You can set the control lines with
Code:
ctrl = PARPORT_CONTROL_STROBE | PARPORT_CONTROL_AUTOFD | PARPORT_CONTROL_INIT | PARPORT_CONTROL_SELECT;
ioctl(fd,PPWCONTROL,ctrl);
You can use write() to write a byte to the port, and ioctl(fd,PPRSTATUS,pChar); to get the status bit values in *pChar;

Then call ioctl with PPRELEASE when you're finished.
 
Old 11-27-2004, 07:40 PM   #5
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
There is also some commercial hardware products you can buy, most run from $20 and up, they connect to the serial port and have various types of digital and analog IO.
You can get these devices with any number of mechanical or solid state relays to control 120VAC devices, pretty much anything.
You just write a simple serial communication program that writes a few characters to the device to enable or disable the relays.
Just do a google on "RS-232 Serial IO Board", you'll find a bunch of them.
 
Old 11-28-2004, 01:05 PM   #6
mala fide
Member
 
Registered: Nov 2004
Location: Espoo, Finland
Distribution: Kubuntu 7.10
Posts: 48

Original Poster
Rep: Reputation: 15
I was thinking about controlliing relays at first and maybe use DACs if I feel like using analog control after I get the relays working. I haven't exactly figured out what I'm going to use it for but I'll probably start with lights.

Can I call the functions in ppdev straight or do I have to write a driver thingie to control the port? The ppdev-header file seemed quite informative but controlling ports and other hardware is pretty much uncharted territory for me.
 
Old 11-29-2004, 07:21 AM   #7
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
ppdev is designed so that you don't need to write a driver to use it; it is a driver in its own right.

You'll find some interesting information on the hardware side of relays in the Linux Coffee HOWTO (www.tldp.org).

Also, just a thought: you can use a cheap sound card as a DAC very easily if you don't need too much current or a well-defined zero-point (just throw numbers into /dev/dsp). This has the advantage that you can do all sorts of digital signal transforms using sox.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot Open Mail Server Ports 25, 110, and 220. Other Ports will open. Binxter Linux - Newbie 9 11-29-2007 02:03 AM
Accessing LAN from outside OliXNet Linux - Networking 11 02-08-2005 05:46 AM
Accessing my second HD aje Linux - Newbie 36 10-24-2004 12:37 AM
Accessing I'm a newbee Linux - Software 0 03-20-2004 10:01 PM
accessing serial ports w/ c++ iggy_mon Programming 3 08-31-2003 07:05 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration