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 FileOutputStrea m 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"
FileOutputStrea m outputWriter = null;
FileInputStream inputReader = null;
static byte[] check = {(byte)16, (byte)4, (byte)1};
static byte[] testprint
={(byte)29,(byt e)40,(byte)65,( byte)50,(byte)4 8,
(byte)48,(byte) 50};
try{
outputWriter = new FileOutputStrea m(port);
if(device.equal s("drawer")){
outputWriter.wr ite(check);
inputReader = new FileInputStream (port);
int input = 0;
input = inputReader.rea d();
inputReader.clo se();
inputReader = null;
if (input==18)
System.out.prin tln("Drawer is opened");
else //22
System.out.prin tln("Drawer is closed");
}
else if(device.equal s("printer"))
outputWriter.wr ite(testprint);
outputWriter.cl ose();
outputWriter = null;
} catch (IOException e) {
e.printStackTra ce();
JOptionPane.sho wMessageDialog( null,"Communica tion Error");
}
}
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 FileOutputStrea m 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"
FileOutputStrea m outputWriter = null;
FileInputStream inputReader = null;
static byte[] check = {(byte)16, (byte)4, (byte)1};
static byte[] testprint
={(byte)29,(byt e)40,(byte)65,( byte)50,(byte)4 8,
(byte)48,(byte) 50};
try{
outputWriter = new FileOutputStrea m(port);
if(device.equal s("drawer")){
outputWriter.wr ite(check);
inputReader = new FileInputStream (port);
int input = 0;
input = inputReader.rea d();
inputReader.clo se();
inputReader = null;
if (input==18)
System.out.prin tln("Drawer is opened");
else //22
System.out.prin tln("Drawer is closed");
}
else if(device.equal s("printer"))
outputWriter.wr ite(testprint);
outputWriter.cl ose();
outputWriter = null;
} catch (IOException e) {
e.printStackTra ce();
JOptionPane.sho wMessageDialog( null,"Communica tion Error");
}
}
Comment