Is there a simple way to use the registers as an interrupt trigger? I can make the application run in a new pid and poll for serial input using fork() I just need to read data, no write needed This is the "read" code I am using

fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
fcntl(fd, F_SETFL, 0);
char buf[256];
n = read(fd, (void*)buf, 255);

I am not even sure if I have framed the question correctly Could it look something like this?

int  com_data=0;

void main(void)
 if (com_data){
  --do something--

char getcom()
    retrieve byte from rxbuffer;

interrupt routine

No "interrupt" needed. The "if(com_data)" would be achieved by using poll(2) to test if data is available on fd.

  • I do want to use an interrupt, it would be more efficient – peterretief Nov 13 '12 at 7:32
  • 1
    @peterretief: poll(2) system call is using an interrupt for you. It will put your task to sleep until that interrupt wakes it which will make this function call return. So it is efficient and it is the way userspace programs are usually written. – Krzysztof Adamski Nov 13 '12 at 8:01
  • @KrzysztofAdamski - but that only works if main() doesn't need to do anything else?! – Andrew Nov 13 '12 at 8:56
  • @andrew I though of using fork() in main? – peterretief Nov 13 '12 at 11:58
  • @Andrew poll(2) with a timeout of zero will return immediately and indicate whether data is ready to be read. – Jim Paris Nov 13 '12 at 17:01

I was going to post this as a comment rather than an answer, but perhaps standalone is better.

It's a long time since I wrote interrupt driver drivers under Linux, so my memory is rusty... these days I'm more native.

There is an article in the Linux How To which talks about using interrupts for serial I/O. Sadly it is a bit RTFM (for setserial), so may not be overly helpful, but may give a pointer or two.

For example, ttyS0 normally uses IRQ number 4 known as IRQ4 (or IRQ 4). A list of them and more will be found in "man setserial" (search for "Configuring Serial Ports").

Further help on setting up interrupt handlers can be found on Linux Device Drivers, another site in my bookmarks.

However, you don't need to use interrupts, if your can manage polling adequately. You just need to configure the port to return (without delay) if no data is available. Replace fcntl(fd, F_SETFL, 0); with fcntl(fd, F_SETFL, FNDELAY);

The call to read() will return immediately with n=0 if no data is available.

  • 1
    This answer is misleading in many ways. One, the device in question here is a USB device, so you will not have a single IRQ like a traditional PC com port; at best you'd have to use the USB controller's interrupt and actually speak to the HCI to find out if your previously scheduled URB has completed. Two, the asker is running in userspace; only a kernel driver can have an interrupt handler, but in this case the driver of course already exists and is loaded and working. An additional handler would do nothing. Three, fnctl is unneeded, O_NDELAY was already specified to open. – Jim Paris Nov 13 '12 at 17:09
  • Also, using just read gives no way to find out if the fd has data available, without also reading that data. poll, on the other hand, would let you test whether data is available (equivalent to the asker's if (com_data) statement) without actually reading it. – Jim Paris Nov 13 '12 at 17:12
  • 1
    @JimParis I really do appreciate your help, to complicate matters even further the USB device is temporary, waiting for the GPIO level shifter. I will report back on what worked for me – peterretief Nov 14 '12 at 8:26

You can use this library: https://github.com/mwheels/libcssl

It's an easy to use, event driven serial port communication library for Linux.

You can check out some code examples in the README file

  • Welcome to the site. Please take a moment to read over the 'What kind of behavior is expected of users?' page of the site's help centre, particularly the section entitled 'Avoid overt self-promotion'. When you're linking to your own products it is absolutely necessary that you disclose that affiliation. – goobering Mar 16 '17 at 20:21
  • Copy/pasting answers against multiple questions is also highly frowned upon. I've downvoted this answer as, while it addresses the question, it lacks enough detail to be usable. Please demonstrate exactly how this approach would be applied here. – goobering Mar 16 '17 at 20:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.