By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,737 Members | 1,971 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,737 IT Pros & Developers. It's quick & easy.

Serial port problems

P: n/a
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");
}

}
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
mimisam wrote:
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.


Why on earth wuld you use FileInputStream and FileOutputStream for
talking to serial and parallel ports? Are you trying to use them
against PRN/LPTx and COMx?

This is not how you should be communicating with serial and parallel
devices. If you want to do that, look at the Java Communications API,
which provides its own classes far talking directly to the
serial/parallel hardware, allowing you to set the necessary
communication parameters (speed, bits per byte, stop bits, handshaking,
etc.).

To download and for more details, see:

http://java.sun.com/products/javacomm/

HTH!

Brad BARCLAY

--
=-=-=-=-=-=-=-=-=
From the OS/2 WARP v4.5 Desktop of Brad BARCLAY.
The jSyncManager Project: http://www.jsyncmanager.org
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.