serial port reading

Discussion in 'Java' started by Willie, Sep 7, 2005.

  1. Willie

    Willie Guest

    Hi, my friends,

    I am working on a device connecting to the serial port. I need to read
    data from the device. I used the SimpleRead.java from SUN's website.
    http://java.sun.com/developer/releases/javacomm/SimpleRead.java

    The port I used is COM1. But it didn't give me what I want. The data
    was stored in the log area of the device. The previous version for the
    application was in C++ and worked well. They use
    ReadFile(hComm,s,maxtoread,&read,0) to retrieve the data from the
    device.

    Does the inputstream not fit the application or I did other things
    wrong? Any help is good. Thank you in advance.

    Willie
    Willie, Sep 7, 2005
    #1
    1. Advertising

  2. Willie

    Roedy Green Guest

    On 7 Sep 2005 15:06:00 -0700, "Willie" <> wrote or
    quoted :

    >But it didn't give me what I want.


    what happened?

    Did you get garbled data, nothing at all, valid data but not what you
    expected?

    There are so many things to configure before a serial port will work
    correctly:
    baud rate, character width, stop bits, parity, flow control.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
    Roedy Green, Sep 8, 2005
    #2
    1. Advertising

  3. Willie

    Roedy Green Guest

    On Thu, 08 Sep 2005 03:17:26 GMT, Roedy Green
    <> wrote or quoted :

    >There are so many things to configure before a serial port will work
    >correctly:
    >baud rate, character width, stop bits, parity, flow control.


    Then you have the connectors, and cables which usually are missing
    conductors.

    For background on serial ports see
    http://mindprod.com/jgloss/rs232c.html
    and chase the links.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
    Roedy Green, Sep 8, 2005
    #3
  4. Willie

    jan V Guest

    Willie,

    It seems you need to learn how to tackle debugging in a systematic way.

    You've connected your device to your Java app via a cable and a host for
    your Java app, OK?

    Is any data flowing over that cable? If not, forget the program, your
    problem is probably cabling.

    What's the log area of the device (we really can't read minds in this
    newsgroup, you need to be in alt.mystic for that...) ?

    Have you checked that your very first transmission to the device is
    bit-for-bit what it needs to be? Your device isn't going to answer if you
    send it junk...

    Break the problem down. Tackle each sub-problem one at a time.


    "Willie" <> wrote in message
    news:...
    > Hi, my friends,
    >
    > I am working on a device connecting to the serial port. I need to read
    > data from the device. I used the SimpleRead.java from SUN's website.
    > http://java.sun.com/developer/releases/javacomm/SimpleRead.java
    >
    > The port I used is COM1. But it didn't give me what I want. The data
    > was stored in the log area of the device. The previous version for the
    > application was in C++ and worked well. They use
    > ReadFile(hComm,s,maxtoread,&read,0) to retrieve the data from the
    > device.
    >
    > Does the inputstream not fit the application or I did other things
    > wrong? Any help is good. Thank you in advance.
    >
    > Willie
    >
    jan V, Sep 8, 2005
    #4
  5. Willie

    Willie Guest

    I used serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
    SerialPort.STOPBITS_1, SerialPort.PARITY_NONE) to set the parameters
    and
    ....
    case SerialPortEvent.DATA_AVAILABLE:
    byte[] readBuffer = new byte[20];
    int r=0;
    try {
    while
    ((r=inputStream.read(readBuffer))!=-1){//(inputStream.available() > 0)
    {
    int numBytes = inputStream.read(readBuffer);
    System.out.println(new String(readBuffer, 0, r));
    }
    ....

    to print the output. The output repeated like this :
    (Q )
    (Q )
    (Q )
    ....
    I don't know what's that.

    My expected output is a string of digital data. Thanks,

    Willie


    Roedy Green wrote:
    > On 7 Sep 2005 15:06:00 -0700, "Willie" <> wrote or
    > quoted :
    >
    > >But it didn't give me what I want.

    >
    > what happened?
    >
    > Did you get garbled data, nothing at all, valid data but not what you
    > expected?
    >
    > There are so many things to configure before a serial port will work
    > correctly:
    > baud rate, character width, stop bits, parity, flow control.
    > --
    > Canadian Mind Products, Roedy Green.
    > http://mindprod.comAgain taking new Java programming contracts.
    Willie, Sep 8, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lec
    Replies:
    1
    Views:
    389
    Grant Edwards
    Jul 17, 2003
  2. Pom
    Replies:
    2
    Views:
    1,630
    Bas-i
    Jan 31, 2007
  3. Vivek Menon
    Replies:
    8
    Views:
    3,154
    bogdan
    Dec 6, 2006
  4. Hal Vaughan

    Reading the Serial Port

    Hal Vaughan, Mar 11, 2008, in forum: C++
    Replies:
    20
    Views:
    1,060
  5. msalerno
    Replies:
    3
    Views:
    405
    Ilmari Karonen
    Jul 14, 2005
Loading...

Share This Page