LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Hardware
User Name
Password
Linux - Hardware This forum is for Hardware issues.
Having trouble installing a piece of hardware? Want to know if that peripheral is compatible with Linux?

Notices


Reply
  Search this Thread
Old 09-29-2010, 01:20 PM   #1
Jude Terror
LQ Newbie
 
Registered: Dec 2008
Posts: 27

Rep: Reputation: 0
Reading a Simple Hardware Switch in Linux


Hey guys,

I'm a moderately skilled linux user who can program a bit in php or shell scripts, and I've been tasked with something far out of my league, or at least out of my league to understand how to go about attempting.

I've been asked to design a switch, a simple circuit that can be opened or closed when two pieces of equipment are touching each other. In other words, when a box is closed, the circuit will be complete. When it's open, it will be broken.

I have to read the state of this switch in linux, so that I can retrieve the state, open or closed, within other programs, and use the state to decide whether certain actions in the program are available.

It seems like a simple matter, but I have no idea where to start. I was pointed in the direction of this device: http://www.mccdaq.com/usb-data-acqui...SB-1024LS.aspx , and I ordered one, but I have no idea how to use it. I did compile and install the latest libhid package, and the device is recognized when I run lsusb, but I don't know how to "read" the device. Can this be done from command line? Is this device too complicated for what I want to do?

Thanks in advance for any help.
 
Old 09-29-2010, 02:06 PM   #2
MS3FGX
LQ Guru
 
Registered: Jan 2004
Location: NJ, USA
Distribution: Slackware, Debian
Posts: 5,852

Rep: Reputation: 361Reputation: 361Reputation: 361Reputation: 361
That device is indeed ridiculously overcomplicated for what you need to do.

The first thing we need to get out of the way, is there an interface requirement here? Doing this over serial (or even parallel) would be very easy, but USB is a little more involved.

For USB there are a number of routes you could take, but the easiest and cheapest would be to use a USB-to-FTDI adapter, which basically gives you digital I/O lines which you can control using libftdi. These adapters range from $15 to $30, and once you get your basic code up and running (which won't be much more than a few lines) you will find it is actually pretty easy to add in other features (such as controlling LEDs via software for status indicators).

A good guide on getting started with FTDI devices was posted on Hack a Day awhile back.

Last edited by MS3FGX; 09-29-2010 at 02:08 PM.
 
Old 09-29-2010, 02:25 PM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,730

Rep: Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920
I agree it is overkill for just one switch. The README file in the linux usb FTP section has a sentence that infers they wrote an API for this device but little else. There is no reference on the posted page for this device that it is supported so I would call them on the phone and ask.

Another option would be a legacy parallel port if this computer is so equipped. Lots of info parallel port programming and interfacing can be found by googling.
 
Old 09-29-2010, 02:54 PM   #4
Jude Terror
LQ Newbie
 
Registered: Dec 2008
Posts: 27

Original Poster
Rep: Reputation: 0
USB isn't necessarily a requirement. However, I don't know how to program in C, so I'm hoping there is some really simple preexisting code that can give me the data from the device in a form I can use. Basically I'm hoping to be able to get a simple value, 0 for "open", 1 for "closed." Is that unrealistic?

If I were to get one of those USB to FTDI cables, I would then need some kind of external board to run the circuit from?

I hate to sound so dumb, it seemed so simple in theory.
 
Old 09-29-2010, 03:06 PM   #5
MS3FGX
LQ Guru
 
Registered: Jan 2004
Location: NJ, USA
Distribution: Slackware, Debian
Posts: 5,852

Rep: Reputation: 361Reputation: 361Reputation: 361Reputation: 361
As the page I linked to shows, you could simply connect the switch directly between two of the pins on the cable and check the condition of the switch using using a digital read.
 
Old 09-29-2010, 04:05 PM   #6
Jude Terror
LQ Newbie
 
Registered: Dec 2008
Posts: 27

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by MS3FGX View Post
As the page I linked to shows, you could simply connect the switch directly between two of the pins on the cable and check the condition of the switch using using a digital read.
Ok, so this looks very promising, but I'm just a little dense, as I've never really done anything like this. Basically, I:

1. Get one of these usb to ftdi cables, plug it in, and compile or install the libftdi driver if it is not already a part of my system.

2. stick two wires into two of the four pins on the cable.

3. At the other end of the wires, I can touch them together to complete my circuit (to be put into actual application on a foot pedal operated device).

4. (Here is the part I don't fully understand) I use a command to read the condition of the switch based on the status of the two pins. Is this done from the shell, or do I need to write code in c to output this data to the shell (problematic because I've never written anything in c, though I can kind of decipher the examples posted on that site).

Again, sorry for being so dense about this.
 
Old 09-30-2010, 11:22 AM   #7
Jude Terror
LQ Newbie
 
Registered: Dec 2008
Posts: 27

Original Poster
Rep: Reputation: 0
Ok, after a lot of reading, I'm thinking that maybe I could follow the example at hack-a-day almost exactly, substituting a circuit that could be opened or closed for the LED. Then I would modify the script as follows:

Code:
#include <stdio.h>
#include <ftdi.h>

#define CIRCUIT 0x08  /* CTS (brown wire on FTDI cable) */

int main()
{
    unsigned char c = 0;
    struct ftdi_context ftdic;

    /* Initialize context for subsequent function calls */
    ftdi_init(&ftdic);

    /* Open FTDI device based on FT232R vendor & product IDs */
    if(ftdi_usb_open(&ftdic, 0x0403, 0x6001) < 0) {
        puts("Can't open device");
        return 1;
    }

    /* Enable bitbang mode with a single output line */
    ftdi_enable_bitbang(&ftdic, CIRCUIT);

    /* Read state of pins and output once */
    ftdi_read_pins(&ftdic, buf, 1);
    printf(buf);
    }
}
Would that be the proper way to read the state of the circuit and output it so that if I ran the compiled version of this program from command line, it would respond with output that would be different depending on whether the circuit was open or closed?
 
  


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
Hardware Switch; Linux and Windows; Linux Wins!! AvePtah LinuxQuestions.org Member Success Stories 5 11-25-2006 05:10 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Hardware

All times are GMT -5. The time now is 01:44 AM.

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