Serial port problems

From: mimisam (michikotas_at_yahoo.com)
Date: 12/27/03


Date: 27 Dec 2003 08:37:15 -0800

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");
        }

}



Relevant Pages

  • Re: Design Problem
    ... COM Port, and that the 'instructions' contain a destination flag ... The tasks are instructions to the BMS. ... Do you have 'feedback' within the Task? ... Send command Wait until some feedback then send another command ...
    (microsoft.public.vb.general.discussion)
  • Re: Design Problem
    ... COM Port, and that the 'instructions' contain a destination flag ... Do you have 'feedback' within the Task? ... Send command Wait until some feedback then send another command ... how to set up such a scheduler. ...
    (microsoft.public.vb.general.discussion)
  • Re: Serial port problems
    ... When parallel port is used, ... My problem is that for serial port, I can't get any feedback ... serial/parallel hardware, allowing you to set the necessary ...
    (comp.lang.java)
  • Re: py3k feature proposal: field auto-assignment in constructors
    ... I appreciate everyone's feedback on the topic. ... creating more complexity is not the way to go. ... This is probably even more preposterous than @host, @port, but to me ...
    (comp.lang.python)
  • Re: roZetta Status
    ... roZetta firmware is written in ZBasic and uses the ZX-40 from Elba, ... There is one main highspeed RS232 level serial port which interfaces ... with the TW523 as the ZX-40's interrupts are sluggish about 3% of the time. ...
    (comp.home.automation)