0

I'm using termios.h to communicate with a USB Dongle. I would like to close the file descriptor when I disconnect the dongle. Is there a way to do this. I know that for f.e. a TCP/IP connection you get a EOF if the other party closes the connection. However, with a serial connection, my program basically runs wild (I'm using a loop to read from the descriptor...).

Basically I would like to have some indication that the connection was closed so that i can gracefully shut down the program. Maybe by changing the settings with ioctl?

user3142067
  • 1,222
  • 3
  • 13
  • 26
  • Just to clarify: I know about udev and such. I would like to know if there is a way similar to TCP/IP where you can read the file descriptor and if it is EOF you know the connection was closed. I haven't found a way to do this with serial ports. But maybe there is a special configuration which allows that .. (?) – user3142067 Jul 19 '15 at 16:17
  • Your program runs wild, because...? Maybe there you should search for the solution - reading from a "broken" descriptor should set `errno` to some other value than `EAGAIN`, used when there's simply no data. – nsilent22 Jul 19 '15 at 21:14
  • Thanks for that suggestion. I will try this out tomorrow ... – user3142067 Jul 19 '15 at 22:38

1 Answers1

0

I Googled a bit using the search phrase how to receive usb disconnect notification in C for windows and came across Detecting USB Insertion / Removal Events in Windows using C++ on stackoverflow.com. If you run on Linux, you can adapt the seach phrase and try those answers.

Community
  • 1
  • 1
Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
  • I know how to detect the events using the udev daemon or the libudev. And I managed to do it writing my own daemon. However, I would like to know if there is some way to use termios and sockets only ... Since i didn't manage to get udev rules to work when a USB Dongle is inserted, i wrote this daemon... but in order to shut down the correct program (I want to start an aritrary amount of instances of that program) I need to save the PID in order to kill it when the Dongle is removed. It would be nice for the program to shut down itself by noticing that the dongle was removed... – user3142067 Jul 19 '15 at 16:04