My application is connected via USB to a PC. Within my application I'm using a 18F2550 PIC. A status change of a I/O will be send via USB to PC.

The code which I'm using for outgoing:

USBwriteStatus:
USBService ' Must service USB regularly
USBOUT 3, USBBUFFER ,CNT_UIT, USBWRITESTATUS
RETURN

The problem is when I disconnect the USB cable it seems that the the PIC program commes in an unendless loop of "USBwriteStatus. I have mode some modifications, see below, however this will still not solve the problem.

USBwriteStatus:
USBService ' Must service USB regularly
USBOUT 3, USBBUFFER ,CNT_UIT, exitusbwritestatus
exitusbwritestatus:
RETURN

When I'm taking this SUB out of my application everything works also without USB connection to PC.

Is there somebody who knows what I'm doing wrong?
Is it possible to see the status of the USB connection (connect/disconnect) before calling the SUB USBwriteStatus.