[Ilugc] [LANGUAGETIP - C ] serial port access using C

  • From: girishvenkatachalam@xxxxxxxxx (Girish Venkatachalam)
  • Date: Sun Nov 8 17:23:49 2009

I did this today. So I thought I would talk about this.

The easiest way to get going if you wish to learn serial port access
on UNIX is by installing the Perl CPAN module
Device::Modem.

Then all you have to do is this:

 use Device::Modem;

         my $modem = new Device::Modem( port => '/dev/tty00' );

        /*
         if( $modem->connect( baudrate => 9600 ) ) {
             print "connected!\n";
         } else {
             print "sorry, no connection with serial port!\n";
         }
        */

        my %sig = $modem->status();
        for ('CTS','DSR','RLSD')
        {
                if($_ =~ /RLSD/) {
                        print "Signal DCD is: ", ($sig{$_} > 0 ? 'on'
: 'off'), "\n";
                } else {
                        print "Signal $_  is: ", ($sig{$_} > 0 ? 'on'
: 'off'), "\n";
                }
        }

Now this should give an idea about where I am coming from.

I am trying to get a footpedal device used in medical transcription
for pausing/playing, rewind and fast forward to work
 with a Windoze box.

I thought that interfacing this on OpenBSD or linux would be a good
starting point. So I went ahead and came up with
this code. I took the perl module code from above as a sample for this activity.

The footpedal is a dumb device with no microcontroller or power. I
figured out that the left pedal is connected to the
CTS[clear to send] pin(8), the middle pedal is connected to the
DSR[data set ready] pin(6) and that the last pedal is
wired to the DCD pin(1) which is also known as RLSD .

My job was to read the serial port. And this code does the job admirably well.

 http://gayatri-hitech.com/languageclasses/footpedal.c.html

I also have the code to do this on Windows
 here:

http://gayatri-hitech.com/languageclasses

The explanation of the code is very simple. Please remember to run
this as root user!

You are opening the serial port in read only mode and you are setting
the basic terminal attributes for setting raw mode
 and no parity, no hardware handshaking etc. Opening with a O_NDELAY
flag and setting fcntl(fd, F_SETFL, 0) are necessary to
ensure that the port is opened and is immediately available for
reading. As you can see, we are not using a MODEM here,
no point in waiting.

Once you do that you are reading the port pins in an ioctl call

ioctl(fd, TIOCMGET, &status);

I am having to open and close the port everytime(while loop) on
OpenBSD. Otherwise it does not work. I have not debugged it further.

I have much more important things to do. So I left it like that. On
Windows it is a lot more simper.

You check whether a flag is set using the binary '&' operator. it
masks out the particular bit of interest and we can detect
 the pressing of the pedal that way.

-Girish
-- 
Gayatri Hitech
web: http://gayatri-hitech.com

SpamCheetah Spam filter:
http://spam-cheetah.com

Other related posts: