Results 1 to 2 of 2
  1. #1
    Jim Ryan is offline Member
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Default javax.comm.CommPort serial port reopen PortInUseException

    I have two serial ports, for the sake of redundancy in my app. One is designated arbitrarily the active port. If it goes down, I switch to the other as active port. (I can tell it is down because the acknowledgements from the other app at the other end of the connection go unreceived.) Any port that is down undergoes reconnect thread every thirty seconds or so, until someone plugs the cable back in, restoring the healthy dual-port state. The reconnect thread closes and reopens the down port. (On some platforms, my Windows laptop, for example, this is not necessary because plugging back in is sufficient for the port object to carry on read/writing. On others, including my Solaris/XEON machine, you need to reopen.) These ports are java comm's CommPort objects. Everything works fine, except for one thing.

    I have an issue with concurrency and reopening a down port.

    The reopen code causes PortInUseException when and only when opening (after successfully closing) the port sometimes but not always. It causes the exception when the port that is not down is in use for sending its messages and receiving its acknowledgements. This makes no sense to me unless the use of one of the two ports on the machine causes the other to be counted in as owned by the app at that moment (could this be??), causing the PortInUseException, or unless I'm overlooking something in my code which causes the exception to arise at the moment of the healthy port's read/writes as a matter of coincidence. For example, synchronizing in the read/write block on an object which has the down port as a member and can cause the down port to be synchronized on and therefore be considered part of the app even though it is closed. But I think I have eliminated such unhappy synchronization. (When I did, I saw the frequency of the exception drop off leaving only the case in which the other port is writing/reading) So, I am at a loss.

    If anyone has any advice on this, I'd be most appreciative.

  2. #2
    Jim Ryan is offline Member
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Default Re: javax.comm.CommPort serial port reopen PortInUseException

    Let me refine my query. The exception does not occur only when I'm writing to the port that isn't unconnected. I've tried writing to it only when the unconnected port is not being reopened. The exception still occurs. It occurs when and only when the execution point of the following loop isn't at the sleep() line. Here is the pseudocode of the main loop:

    while (running) {

    if not currently attempting to reopen either port...//This conditional is a primitive boolean.
    ...write heartbeat to port 1 if it isn't down.
    ...write heartbeat to port 2 if it isn't down.

    Wait for acknowledgement of heartbeats.
    If failed to receive acknowledgement from a port, then start a reopen thread for it

    sleep(five seconds) //WE"RE SAFE HERE. The reopen thread never throws the PortInUseException when this thread asleep here.
    }

    The reopen thread simply calls close() and javax.comm.CommPortIdentifier.getPortIdentifier(po rtName).open(). It does this without issue when the thread with the above loop is at the sleep() line. It throws PortInUseException if the open() attempt is made when the other thread is not at the sleep() line.

    Both the reopen thread and the main thread (whose loop is above) are in the same class, the reopen thread being an inner class (called ReconnectThread). The class has a handle to the two ports, of course, and there is another class that has a thread which listens to the ports for the acknowledgement signals from the server.

    I've tried synchronization/blocking and doing away with synchronization (in the thought that maybe the synch causes the app to mark the port as in use somehow). No difference. The loop above is the non-synched version.

    It appears that sleep() marks the port as not in use by the app and when the sleep is finished, the port is marked as in use by the app. I don't understand this.

    Any help much appreciated.

Similar Threads

  1. Management of Xbee serial javax.comm
    By luirossi in forum New To Java
    Replies: 0
    Last Post: 10-25-2011, 12:14 PM
  2. Replies: 2
    Last Post: 03-04-2011, 10:14 AM
  3. GIOVYNET Java Serial Comm Port Question
    By the_justin in forum New To Java
    Replies: 1
    Last Post: 11-30-2010, 01:13 PM
  4. Replies: 1
    Last Post: 07-30-2010, 09:29 AM
  5. Replies: 7
    Last Post: 07-12-2010, 05:24 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •