Solved

interrupt driven serial communication...

Posted on 1998-08-25
4
573 Views
Last Modified: 2013-12-26
  I am trying to write an interrupt driven serial port program using C, and I was wondering if anyone have amy idea if this is possible. My Unix system supports POSIX termios.h and aio.h, will my answer lies within these two include files?
   Fundamentally, I would like to send a string of commands out of my serial port and proceed with other task. In the background I would like to have an interrupt handler that continously read the in-coming buffer for a full length string return data. I don't want to hang the system on the read process because of the my process is under real-time.
0
Comment
Question by:nickolas
  • 2
4 Comments
 

Author Comment

by:nickolas
ID: 1293017
Adjusted points to 200
0
 
LVL 2

Expert Comment

by:JYoungman
ID: 1293018
Use select(), or poll(), or pthreads, or asynchronous I/O.

0
 
LVL 2

Expert Comment

by:JYoungman
ID: 1293019
You could also set VMIN and VTIME with the <termios.h> API.

0
 
LVL 3

Accepted Solution

by:
dhm earned 200 total points
ID: 1293020
Under unix, a user-level program never deals directly with interrupts from devices -- the interrupts are handled by a piece of code called a device driver.  The driver can (but doesn't have to) send a signal to the user process using the device.  A user-process signal is similar to a hardware interrupt, but is not necessarily connected to any particular piece of hardware.  Signals can be a pain in the ass to get working properly, and you're probably better off not using them for anything that requires fairly accurate timing or that can't tolerate missed signals.

I'm not aware of any way to do "asynchronous I/O" under unix, assuming JYongman had in mind something like NT's asynch I/O, where you start a read operation and then go off and do something else, and when the read is finished, the system sets a flag for you to check.  (You could easily implement that sort of thing, though, using threads.)  Threads will probably give you the most flexibility in solving your problem, but if you're new to threaded programming, it can be difficult, and you may not need all that flexibility anyway.

You can use select() or poll() to see if there's any data ready to be read, and there's an ioctl() that will return the amount of data you can read immediately (the ioctl command is FIONREAD).  You can use a combination of these to keep from hanging on a read.  These are standard I/O calls that work on any file descriptor (FIONREAD may not work on all files, but it usually works on terminals and serial ports.)

If you want to use termios, then you can play with VMIN and VTIME to control how long a read will delay.  The termios man page is long and confusing, though, and I'm skeptical that things that are hard to describe will actually work properly all the time, so I'd advise trying select() and ioctl() first.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now