Serial port problems

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mimisam

    Serial port problems

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

    }
  • Brad BARCLAY

    #2
    Re: Serial port problems

    mimisam wrote:
    [color=blue]
    > 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.[/color]

    Why on earth wuld you use FileInputStream and FileOutputStrea m 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:



    HTH!

    Brad BARCLAY

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

    Comment

    Working...