Hello guys,

I'm developing system,that often prints reports, and always is checking the status of printer (Low on paper, Out of paper etc...). I've implemented class, that queries printer file (/dev/usb/lp0) for status as written in printers (Swecoin TTP2030) manual, here is a code:

Java Code:
     public class PrinterStatusEnquier {  
        private static Logger logger = Logger.getLogger(PrinterStatusEnquier.class);  
      
        private static byte[] PAPER_NEAR_END_ENQUIRY = {0x1B, 0x05, 0x02};   
        private static byte[] STATUS_ENQUIRY = {0x1B, 0x05, 0x01};   
      
        private static String PRINTER_DEVICE =   
                   PropertiesManager.getInstance().getApplicationProperty("printer.device.file");  
      
        public static PaperStatus enquiryPaperStatus() throws IOException {  
            logger.debug("In method enquiryPaperStatus()...");  
      
            RandomAccessFile device = null;  
            try  
            {  
                device = new RandomAccessFile(PRINTER_DEVICE, "rw");  
      
                device.write(PAPER_NEAR_END_ENQUIRY);  
                int response = device.readByte();  
      
                return PaperStatus.getStatus(response);  
            } catch (IOException e) {  
                logger.error("Error while opening file: " + e.getMessage());  
                e.printStackTrace();  
                throw e;  
            } finally {  
                if (device != null) {  
                    logger.debug("Closing file...");  
                    device.close();  
                }  
            }  
        }  
      
        public static PrinterStatus enquiryPrinterStatus() throws IOException {  
            logger.debug("In method enquiryPrinterStatus()...");  
      
            RandomAccessFile device = null;  
            try {  
                device = new RandomAccessFile(PRINTER_DEVICE, "rw");  
      
                device.write(STATUS_ENQUIRY);  
      
                byte[] response = new byte[2];  
                device.read(response);  
      
                return PrinterStatus.getStatus(response);  
            } catch (IOException e) {  
                logger.error("Error while opening file: " + e.getMessage());  
                e.printStackTrace();  
                throw e;  
            } finally {  
                if (device != null) {  
                    logger.debug("Closing file...");  
                    device.close();  
                }  
            }         
        }

And out of my system this code works great. But when I integrate it in system, it starts to raise a lot of IOExceptions. I noticed that it happens when printer prints something, and in this moment I try to get status. Some times I get exception (File not found), and in this case I can check printer file like that - file.canWrite() But sometimes I can get exception (Device or resource busy) or (Input/Output error), and in this case file.canWrite() won't help. The worst thing is, that printer status enquirer not only throws exceptions, it can lock printer's file for some time. It still prints, but unavailable for enquirer.

Does exist a way to enquiry printer file while it's printing? Or maybe exists a way how to check if printer file is available. Please help!

P.S.: System is Ubuntu 12.10