0

I have some code which is reading via COM ports. I wanted to make sure that the reader is restarts when a cable is connects/disconnects but unfortunately when I use SerialPort.GetPortNames() in a timer I always get back the same values it does not matter if I disconnect one or not.

Are there any other options? Do you know why it does not updates?

Thank you in advanced

Edit From the commented link I created that class:

[DllImport("user32.dll", SetLastError = true)]
class CheckSerialPort
{
     public static extern IntPtr RegisterDeviceNotification(
                            IntPtr hRecipient,
                            IntPtr NotificationFilter,
                            uint Flags);

}

Do you have any idea how can I sue that to check if anything connected/disconnected? I am reading the forums, but I do not really get it. Might be just because of the long day:P

Daniel
  • 383
  • 1
  • 5
  • 20
  • http://stackoverflow.com/questions/2754857/serialport-getportnames-behavior – Matthew Watson Aug 15 '13 at 14:19
  • Is the cable you are referring to a RS232 cable or a USB dongle? – dbasnett Aug 15 '13 at 14:19
  • @MatthewWatson thank for the link, do you know how could I use the API? please see update – Daniel Aug 15 '13 at 14:44
  • Using P/Invoke for callbacks is a big subject. There's a sample project which might help here: http://www.codeproject.com/Articles/18062/Detecting-USB-Drive-Removal-in-a-C-Program Also try googling `RegisterDeviceNotification c#` – Matthew Watson Aug 15 '13 at 14:54
  • I understand that incorrect results from GetPortNames() is a rather large problem in itself, but most ways around it require some overhead. Could also just assume the port is there, try to open it, and catch the exception if SerialPort.Open() fails. Just a suggestion. – DanteTheEgregore Aug 15 '13 at 14:58
  • @MatthewWatson unfortunately the codeproject software are not working for usb/serial, it is working for pendrives:P I still searching reagarding ur other option – Daniel Aug 15 '13 at 15:03
  • @ZachSmith the only problem with that if it is exists than I am reading it and it is open. I am not sure if it will give back the right exception, plus with the time outs it could be a bit slow and might give back wrong information, or not? – Daniel Aug 15 '13 at 15:06
  • @Daniel If SerialPort.Open() fails, something is seriously wrong (settings are messed up, the COM port doesn't actually exist, access is locked by another program, etc.) and you should probably be checking the exception anyway. Otherwise, assuming you plan to use the COM port later in the program, It's safe to leave it open. It won't just close on its own. As for stale data, call `SerialPort.DiscardInBuffer();` to flush it. – DanteTheEgregore Aug 15 '13 at 15:25
  • @ZachSmith I close the port after I catch the exception of reading. I inserted in your suggestion as well SerialPort.DiscardInBuffer(); unfortunately it throws an error: the port is closed without I closing it, so it does close on its own if the connection get lost, but it is still found by SerialPort.GetPortNames(). – Daniel Aug 16 '13 at 06:54
  • Ok what I did now is I forcing to restart itself all the time when it gets a port "closed" (port crashed, used by something else) exception. It is not the best way as If i have more serial ports and one is unplugged the rest wont be read nether because of the continuous restarting, it is better than nothing but far from the best solution – Daniel Aug 16 '13 at 07:18
  • Sorted:P @ZachSmith I kind of got out something about ur first comment. "What if I try to always reopen the port when I fail to read?" I will get exceptions thrown when the port is disconnected but as soon as it is reconnected and I reopen it everything will work like magic:P:D thanks guys for your help, it is kind of going around the problem but as long as it works it is fine:P – Daniel Aug 16 '13 at 07:26
  • @Daniel Just an afterthought, if you're using `SerialPort`, don't call `SerialPort.Close()` unless you no longer plan to use that port in the program or don't plan to use it soon. `SerialPort.Close()` won't always close the port immediately. Source: http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.close.aspx – DanteTheEgregore Aug 16 '13 at 12:37
  • @ZachSmith yeah, the only time when I close the port is when I close/restart the entire service, otherwise I leave it open. Thank you for the advice – Daniel Aug 16 '13 at 15:01

0 Answers0