LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 08-27-2003, 07:54 AM   #1
BongFish
Member
 
Registered: Jun 2003
Location: England
Distribution: Slack 9
Posts: 141

Rep: Reputation: 15
Check for status of Serial ports in any language?


Is there any way to do something like the folowing in ANY language?

if 'pin 1 and pin 2' are connected:
do command
elif 'pin 1 and pin 3' are connected:
do other command

etc.


The reason is I have a box with 4 buttons connected to a serial port that I used to controll winamp when I was running XP before I made my remote. It would be usefull to have buttons that could flick through windows in ratpoison or something like that.
 
Old 08-27-2003, 10:50 AM   #2
sk8guitar
Member
 
Registered: Jul 2003
Location: DC
Distribution: mandrake 9.1
Posts: 415

Rep: Reputation: 30
http://www.tldp.org/HOWTO/Coffee-3.html

http://www.tldp.org/HOWTO/Coffee-2.html

those will probably give you some idea on this.
 
Old 08-28-2003, 08:30 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Reading/writing pins directly

Is there some electronic circuit on you 4-button-device that makes it
talk the RS232 protocol?

If not, then I suppose you are trying to read the status of 4 pins on
the serialport directly. As far as I know there are only 2 pins on
the serial port you can write to directly (RTS & DTR), and only 2
other pins you can read directly (DSR & CTS). For reading, there may
be 3; If the DCD-pin also works. Not sure about this. So reading 4 may
not be possible without RS232 circuitry on your 4-button-device.

I recently got into this because I wanted to do the opposite (writing
directly to some pins on the serial post): I wanted to switch 3 LED's
independently through software without an external power supply. USB
is obviously the best way to do this, but it involves too much
electronics for my purpose; I was looking for a solution as cheap as
possible. (If somebody has some info about how to do this with USB, I
would be very interested though, especially a electronic diagram...)

Anyways, I managed to switch on only 2 LED's through the serial port
using the experimental program below. It needs only little changes to
read the DSR and CTS lines.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>  
#include <termios.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

char *program_name;
int fd;

int openserial(char *device)
{
     if ((fd = open(device, O_WRONLY)) == -1) { 
	  perror(program_name);
	  return 0;
     }
     return 1;
}

int closeserial(void)
{
     if (close(fd) == -1) {
	  perror(program_name);
	  return 0;
     }
     return 1;
}

int RTS(unsigned short level)
{
     int status;

     if (ioctl(fd, TIOCMGET, &status) == -1) {
	  perror(program_name);
	  return 0;
     }

     if (level) {
	  status |= TIOCM_RTS;
     } else {
	  status &= ~TIOCM_RTS
     }
     
     if (ioctl(fd, TIOCMSET, &status) == -1) {
	  perror(program_name);
	  return 0;
     }

     return 1;
}


int DTR(unsigned short level)
{
     int status;

     if (ioctl(fd, TIOCMGET, &status) == -1) {
	  perror(program_name);
	  return 0;
     }

     if (level) {
	  status |= TIOCM_DTR;
     } else {
	  status &= ~TIOCM_DTR;
     }

     if (ioctl(fd, TIOCMSET, &status) == -1) {
	  perror(program_name);
	  return 0;
     }

     return 1;
}

int main(int argc, char **argv)
{
     char *dev = "/dev/ttyS0";
     program_name = argv[0];

     if (!openserial(dev)) {
	  fprintf(stderr, "%s: Could not open device file %s\n", program_name, dev);
	  return 1;
     }

     RTS(0);
     DTR(0);
     
     sleep(1);
     RTS(1);
     sleep(1);
     DTR(1);

     if (!closeserial()) {
	  fprintf(stderr, "%s: Error close device file %s", program_name, dev);
	  return 1;
     }

     return 0;
}
Some documents you may want to read:

man termios
IO-port programming mini-HOWTO
Serial programming HOWTO
man ioctl
man ioctl_list
http://www.easysw.com/~mike/serial/serial.html (recommended)

If you manage to read (or better for me: write) to/from more than 2
pins directly, I would appreciate it very much if you tell me by
posting here or by e-mail: heiky_at_freeshell.org

TIA and good luck.

Last edited by Hko; 08-28-2003 at 08:36 AM.
 
Old 08-28-2003, 08:56 AM   #4
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
hko: how about using the parallel port, in basic output mode it has has 5 input pins(10-13, 15) and 8 output pins(2-9) which can be read/wriiten by two 8 bit registers(forgotten io address but you can get it from cat /proc/ioports) i doubt the parallel port driver lets you set/get these registers directly but if it doesnt you can write a kernel module and call inportb() and outportb() as necessary.
 
Old 08-28-2003, 09:11 AM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
kev82: Thanks for thinking along with me. But I want to use the serial port because I want no external power supply and because people tend to have the serial port unused, while often having a printer attached to the parallel port.

It is impossible, or at least difficult and "dangerous", to drive LED's from the parallel port without an external power supply. The serial port can supply more current than the parallel port. Also, when you take too much current from the parallel port it easily breaks! This would leave me with a broken IO-card, or worse, a broken motherboard when the parallel port is integrated with the motherboard.
 
Old 08-28-2003, 10:35 AM   #6
BongFish
Member
 
Registered: Jun 2003
Location: England
Distribution: Slack 9
Posts: 141

Original Poster
Rep: Reputation: 15
No, there's no circutry, pressing the buttons simply shorts two of the pins. I'll post a circuit diagram in a moment, showing which buttons are connected to which pins.

I know it's possible because there's a winamp plugin called ComCtrl that works with the drvice I have. I may write to the author of it asking for the source.

_____________
S | | | |
E \ \ \ \
R __ | | | |
I _____ | | |
A ________ | |
L ___________ |

This is the circuit diagram, I don't have the names of the pins that they are connected to yet.


Anyway, thanks for a very comprehensive reply Hko!!

Last edited by BongFish; 08-28-2003 at 10:37 AM.
 
Old 08-28-2003, 12:07 PM   #7
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
I found the circuit diagram by googling for "ComCtrl" at:
http://diba.hotbox.ru/comctrl/tutorial1/index.html
and at:
http://www.mattsclearpcs.com/com/com.html

I looked at it, and it's indeed possible to read 4 pins like this.
I figured out the names of the pin-names thanks to de doc I recommended.

It does not help me to switch more than 2 LED's however :-(
I'm still convinced that's not possible without additional circuitry and/or external power-supply.

By using parts of my code and change it, it's not too difficult to read the 4 pins independently in Linux.

If you need more help, just say so, and I'll make a basic program for this. I think I'm going to make the thing myself as well. (don't know when).

Thanks too you as well.
 
Old 08-28-2003, 01:17 PM   #8
BongFish
Member
 
Registered: Jun 2003
Location: England
Distribution: Slack 9
Posts: 141

Original Poster
Rep: Reputation: 15
I'm going to try and create the program myself as an exercise, I'm just starting to learn how to program and there's nothing better to aid learning than a good project.

However I would like it if you could PM me the code when you're done for comparison. I have a tendancy to do things very inneficiently at the moment as I am a very poor programer.

Thanks
 
Old 08-28-2003, 02:15 PM   #9
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
OK.
("PM" means Private Message, I suppose?)
 
Old 08-28-2003, 02:41 PM   #10
BongFish
Member
 
Registered: Jun 2003
Location: England
Distribution: Slack 9
Posts: 141

Original Poster
Rep: Reputation: 15
Yep
 
Old 09-10-2003, 05:38 PM   #11
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Its' finished. I've made a program that handles the 4 buttons. It does not do anything useful (yet). It just prints what happens when the buttons are pressed and released.

You can get it here:

http://www.xs4all.nl/~heiky/
 
Old 09-11-2003, 01:46 AM   #12
BongFish
Member
 
Registered: Jun 2003
Location: England
Distribution: Slack 9
Posts: 141

Original Poster
Rep: Reputation: 15
Downloading source now, I'm off to college then I'll try it out later.

Cheers.
 
  


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
Serial ATA (SATA) Linux status report zero0w Linux - Hardware 4 04-03-2006 09:57 PM
Can only use one of my serial ports steaktc Linux - Hardware 1 11-23-2005 06:07 AM
How to check the status of NumLock & CapsLock in the keyboard prash.k Linux User Groups (LUG) 1 10-06-2005 03:40 PM
Check Non Continuous File Status (fragmentation)? carl0ski Mandriva 0 02-07-2005 08:34 AM
Check Printer Status Debby Linux - General 5 02-08-2002 08:52 PM

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

All times are GMT -5. The time now is 06:46 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