Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. SerialPort throwing uncatchable error?

SerialPort throwing uncatchable error?

Scheduled Pinned Locked Moved Visual Basic
questionhelpcomtestingbeta-testing
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Chinners
    wrote on last edited by
    #1

    Hi, I am having a bit of difficulty trying to catch an error thrown by io.ports I have an app that displays a message on an external screen, using serial comms. The routine to do this needs to be as "bullet-proof" as possible. Testing the code, it is fine on a standard serial port, I can unplug the port and plug it back in again, and the code recovers, but as an ultimate "real world" test, I tried it on a USB->Serial adapter, and it works well - untill I unplug the USB cable and plug it back in again. Now, what happens is a bit strange. The application recovers (so the com port is coming back), and starts displaying messages again, but only for a few seconds. I then get the following exception:

    System.UnauthorizedAccessException was unhandled
    Message="Access to the port is denied."
    Source="System"
    StackTrace:
    at System.IO.Ports.InternalResources.WinIOError(Int32 errorCode, String str)
    at System.IO.Ports.SerialStream.Dispose(Boolean disposing)
    at System.IO.Ports.SerialStream.Finalize()

    I cannot trap this error, and the application just bombs out. I certainly am not disposing of the serialport object I have created, so this must be being done automatically. But, my question is, how do I catch, and trap this error? Any help, or pointers would be much appreciated. Thanks, Jason

    D R 2 Replies Last reply
    0
    • C Chinners

      Hi, I am having a bit of difficulty trying to catch an error thrown by io.ports I have an app that displays a message on an external screen, using serial comms. The routine to do this needs to be as "bullet-proof" as possible. Testing the code, it is fine on a standard serial port, I can unplug the port and plug it back in again, and the code recovers, but as an ultimate "real world" test, I tried it on a USB->Serial adapter, and it works well - untill I unplug the USB cable and plug it back in again. Now, what happens is a bit strange. The application recovers (so the com port is coming back), and starts displaying messages again, but only for a few seconds. I then get the following exception:

      System.UnauthorizedAccessException was unhandled
      Message="Access to the port is denied."
      Source="System"
      StackTrace:
      at System.IO.Ports.InternalResources.WinIOError(Int32 errorCode, String str)
      at System.IO.Ports.SerialStream.Dispose(Boolean disposing)
      at System.IO.Ports.SerialStream.Finalize()

      I cannot trap this error, and the application just bombs out. I certainly am not disposing of the serialport object I have created, so this must be being done automatically. But, my question is, how do I catch, and trap this error? Any help, or pointers would be much appreciated. Thanks, Jason

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      If you disconnect the cable, the serial port is still a device that shows up in DeviceManager. When you remove the USB device, the entire COM port goes away. The serial device no longer exists. So, when it comes back, your (internally managed) handle to the COM port is no longer valid. It may work briefly, but you've found out what happens.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007, 2008

      C 1 Reply Last reply
      0
      • C Chinners

        Hi, I am having a bit of difficulty trying to catch an error thrown by io.ports I have an app that displays a message on an external screen, using serial comms. The routine to do this needs to be as "bullet-proof" as possible. Testing the code, it is fine on a standard serial port, I can unplug the port and plug it back in again, and the code recovers, but as an ultimate "real world" test, I tried it on a USB->Serial adapter, and it works well - untill I unplug the USB cable and plug it back in again. Now, what happens is a bit strange. The application recovers (so the com port is coming back), and starts displaying messages again, but only for a few seconds. I then get the following exception:

        System.UnauthorizedAccessException was unhandled
        Message="Access to the port is denied."
        Source="System"
        StackTrace:
        at System.IO.Ports.InternalResources.WinIOError(Int32 errorCode, String str)
        at System.IO.Ports.SerialStream.Dispose(Boolean disposing)
        at System.IO.Ports.SerialStream.Finalize()

        I cannot trap this error, and the application just bombs out. I certainly am not disposing of the serialport object I have created, so this must be being done automatically. But, my question is, how do I catch, and trap this error? Any help, or pointers would be much appreciated. Thanks, Jason

        R Offline
        R Offline
        Rajesh Anuhya
        wrote on last edited by
        #3

        hi jason Sorry for my poor english, I understood your problem. But here i am not telling you to do this. In this situation there is no problem with on board PCI comports. This problem happens only for the USB2Serial cables which are Run through 3rd party drivers(USB2Serial Drivers). When U connected Your USB Device to PC then only u get a comport in your Device Manager. If Your Remove this it will gone pertinently until u connect again. While Running Your Application If U Remove the USB Device, comport will removed from your PC. So, In your application before reading/writing to comport, check for comport is present or not. instead of catch the exception. Thanks

        Rajesh B --> A Poor Workman Blames His Tools <--

        C 1 Reply Last reply
        0
        • D Dave Kreskowiak

          If you disconnect the cable, the serial port is still a device that shows up in DeviceManager. When you remove the USB device, the entire COM port goes away. The serial device no longer exists. So, when it comes back, your (internally managed) handle to the COM port is no longer valid. It may work briefly, but you've found out what happens.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007, 2008

          C Offline
          C Offline
          Chinners
          wrote on last edited by
          #4

          Thanks for the reply. Yes, I had thought this could be the problem, and now, when this type of error occurs, I destroy my serialport object and keep looking through the "My.Computer.Ports.SerialPortNames" collection to see if my comport reappears. If it does, I then create a new serialport object. However, I get the same problem - a crash after a few seconds. I suppose the real question I am asking is how to prevent the exception crashing my program completely? All code that accesses the comport is within a try/catch block, but the exception is still unhandled.

          1 Reply Last reply
          0
          • R Rajesh Anuhya

            hi jason Sorry for my poor english, I understood your problem. But here i am not telling you to do this. In this situation there is no problem with on board PCI comports. This problem happens only for the USB2Serial cables which are Run through 3rd party drivers(USB2Serial Drivers). When U connected Your USB Device to PC then only u get a comport in your Device Manager. If Your Remove this it will gone pertinently until u connect again. While Running Your Application If U Remove the USB Device, comport will removed from your PC. So, In your application before reading/writing to comport, check for comport is present or not. instead of catch the exception. Thanks

            Rajesh B --> A Poor Workman Blames His Tools <--

            C Offline
            C Offline
            Chinners
            wrote on last edited by
            #5

            Thanks for the reply. I have started checking that the comport is availiable however, if the cable is unplugged between checking for the port and the "open / write / close" cycle, then I cannot catch the error. The cable could be pulled out at any time. If my display stops working, that is not a problem, the fact my application stops working is VERY bad.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups