hi,
I am using Win XP and writing an application to check POS devices
(printer and cash drawer).In the application I need to choose the port
which the device is connected to. If the port chosen is wrong or the
device is off power, a communication error message should be
displayed.
I am using FileOutputStream and FileInputStream to send and receive
data from the ports. When parallel port is used, I got the feedback
when the printer is off(exception caught) or the drawer status
changes. My problem is that for serial port, I can't get any feedback
at all. No exception is caught and it hangs when I try to read in data
from the COM port.
Please help me on how to get feedback from serial ports.How do i know
when the device is off?
Thanks in advance.
Below is part of my code:
private void Check(String device, String port) {
//port="COM1" or "LPT1"
FileOutputStream outputWriter = null;
FileInputStream inputReader = null;
static byte[] check = {(byte)16, (byte)4, (byte)1};
static byte[] testprint
={(byte)29,(byte)40,(byte)65,(byte)50,(byte)48,
(byte)48,(byte)50};
try{
outputWriter = new FileOutputStream(port);
if(device.equals("drawer")){
outputWriter.write(check);
inputReader = new FileInputStream(port);
int input = 0;
input = inputReader.read();
inputReader.close();
inputReader = null;
if (input==18)
System.out.println("Drawer is opened");
else //22
System.out.println("Drawer is closed");
}
else if(device.equals("printer"))
outputWriter.write(testprint);
outputWriter.close();
outputWriter = null;
} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null,"Communication Error");
}
}