I have a serial device ttyN0 which is configured for RS422 protocol. I want receive the data on this line using interrupt mechanism.
Can any of you pl guide me how to go abt in configuring this device.
while installing ttyN0 IRQ-5 has been chosen.
regards,
Auti123.
you can...
hmm, I guess you want to use the "select" or "poll" syscall... there's a nice example about select in the man page.
interrupts
No, you cannot use the interrupt (at least not directly). The interrupt, if it is used at all, signals the device driver that new data has arrived. In turn, it is possible for device drivers to allow you to register an 'I/O completion routine' (personally I have never had a need to use such a mechanism on Linux and don't even know if there is provision for it). It is also possible that some drivers allow you to send a specific signal to your process, but this is of very limited use. Consult the device driver source for your ttyXX.
You do not say anything about what you want to accomplish, but in my case when I need to use the serial port, unless I'm creating a Real-Time application, I just create a thread to read the port - it will be blocked most of the time. When data is picked up, you can use the various IPC mechanisms to communicate the information to your main program loop(s).
The other option of course had already been mentioned - just use 'select' (more portable than 'poll') to identify when data is ready.
If data is coming in on a low-speed serial port, latency is extremely rarely ever an issue - seriously, you're trying to open a banana with a steamroller if you want to be tied into the hardware interrupts.
opening bananas with steamrollers
OK, here is an outline of how you would use the signal mechanism to respond to all I/O on a serial port - note that for a file opened for R/W you get interrupts for both read and write (when write buffer was filled and is now not full, and when any data is available to read). This is for Linux and isn't quite portable (but glibc has an equivalent POSIX interface to get the job done):
1. Register an appropriate signal handler; use the form which has a SIGINFO param
2. Open the serial port; make sure you specify the O_ASYNC flag
3. Use 'fcntl' to set the 'F_SETOWN' attribute of the port
4. Each time you get a SIGIO, check SIGINFO to see if the device descriptor is the one you got when you opened the serial port. If it is, then you must now determine whether it is ready to read - use 'select' for this job. Of course if you open the serial port as Read Only then you know it's incoming data.
In principle it is possible to open the serial port for read only and open it again for write only - I have never done this so you would have to test that theory - it would be convenient in this case since a SIGIO immediately tells you whether you are looking at the read side or write side.
For single-character reception via the serial port, using signals is an extremely inefficient method for processing the information. You must also be careful or you may receive an interrupt while processing one - always a danger when you're playing with interrupts - if your code is sloppy you will have a deadlock.
The only time I ever used hardware interrupts was when writing my device driver for the only real-time software I had ever written. Otherwise I have only ever used the system's software interrupts, and never to process an open file/device.