You can use the following code.
Code:
extern u8 control_signal_bitmap; // in libusb.a
extern u8 USBDeviceState;
#define cdc_dtr_clear() (control_signal_bitmap &= 0xfe)
#define cdc_dtr_status() ((control_signal_bitmap & 0x01) == 0x01)
#define cdc_usb_connected() ((U1OTGSTATbits.VBUSVD != 0) && (U1OTGSTATbits.SESVD != 0))
#define cdc_usb_configured() (USBDeviceState == 0x20)
The cdc_usb_connected() shows if USB is connected. Enhanced version is implemented as CDCUSBIsConnected() in cdc library.
cdc_usb_configured() shows if the Pinguino and host PC established CDC connection. That means CDC COM port is available for use on host PC. It is implemented as USBCDCisConfigured() in cdc library.
cdc_dtr_status() shows the DTR status set by the host PC application. Most terminal emulation programs on the host PC will set DTR high after opening the CDC com port. Enhanced version is implemented in cdc library as DCClearDTR() and CDCDTRIsReady().
Once Pinguino IDE detects any reference to CDC function in the code, it will attach the USB port and initialize it regardless if you actually execute it. If you want to save about 4mA current used by the USB regulator, you can use USBDeviceDetach() to disable the USB module. You can also enable it by USBDeviceAttach() and USBDeviceInit().
DJ