Results 1 to 17 of 17
  1. #1
    kkGG is offline Member
    Join Date
    Nov 2010
    Posts
    28
    Rep Power
    0

    Default byte[] to double[][]

    In a program I'm currently writing, I read from a port. And, given the way I've done it, using RxTx and the InputStream of the port "port.read(byte[] byteArray, int start, int end);" will read from the port and store the information read into a byte array. However, for purposes of this program, I need to eventually move the data form the byte array into a double[][]. The port from which I'm reading should return lines of data in the format "s10:0:1.493875e-12" where (0,1.493875e-12) is the data point I'm interested in. Any idea on how I can go about doing this?

    Thanks.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,329
    Rep Power
    25

    Default Re: byte[] to double[][]

    What is the format of the data in the byte array? Is it character data from Strings or what?
    If its from a String you should be able to use a String class method to convert the bytes to a String.

    Are you able to extract what you want from a String? The String class's split method may be useful.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: byte[] to double[][]

    If it's character data you might be able to use a BufferedReader and readLine, depending on whether "should return lines of data" means actual lines delineated by a linebrak of some sort.

  4. #4
    kkGG is offline Member
    Join Date
    Nov 2010
    Posts
    28
    Rep Power
    0

    Default Re: byte[] to double[][]

    ok, so if I was to use BufferedReader to read in the forms of strings, how do I go about creating a BufferedReader that is linked with the port? To handle the port I am using gnu.io package

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,329
    Rep Power
    25

    Default Re: byte[] to double[][]

    In Java SE you would use a socket to connect at the port and then you can get an input stream from the socket. No ideas about the gnu package.

  6. #6
    kkGG is offline Member
    Join Date
    Nov 2010
    Posts
    28
    Rep Power
    0

    Default Re: byte[] to double[][]

    Well, I know you can get input stream in the form port.getInputStream() using the gnu.io package. Do you know how I could take the OutputStream and link it to the BufferedReader?

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,329
    Rep Power
    25

    Default Re: byte[] to double[][]

    Can you explain what you are trying to do.

    Do you want what is written to the output stream to be read by the reader? Is this all in one class or program?
    One way is to use a ByteArray...

  8. #8
    kkGG is offline Member
    Join Date
    Nov 2010
    Posts
    28
    Rep Power
    0

    Default Re: byte[] to double[][]

    Ok, so I'm working with a machine containing a central control unit. To communicate with the CCU I'm using a serial port and the gnu.io package. But the CCU seems to respond to commands that are line-based and are in the form of strings. The CCU also responds with Strings. The problem I've run into is that I can write commands to the CCU effectively using OutputStream because it's easy to take a String and convert it into its bytes (String.getBytes();). But, say I want to get a value from the CCU. I can effectively write a String command to the CCU in this manner, but reading the response becomes trickier, for the gnu package allows me to serialPort.getInputStream(), but if I want to inputStream.read() it becomes tricky to read byte data that is in the form of a line of String for example, "ok:LowMass:12". What I'm eventually looking to do is retrieve the number int value "12" but again, that's seems tricky given the situation. So I'm looking for a way to make it easy to read from the CCU given its constraints.
    Last edited by kkGG; 01-22-2012 at 05:45 PM.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,329
    Rep Power
    25

    Default Re: byte[] to double[][]

    tricky to read byte data that is in the form of a line of String
    Have you tried using the String class's methods to convert the bytes to a String?
    Can you read in a byte array and print out its contents and post it here?
    Use the Arrays class's toString() method to format the byte array for printing.

    I'm assuming that once you have a String you know how to parse it and convert a String to an int

  10. #10
    kkGG is offline Member
    Join Date
    Nov 2010
    Posts
    28
    Rep Power
    0

    Default Re: byte[] to double[][]

    One thing, the inputStream's read function I know will store the bytes it reads into a byte array and reads up to some length of bytes, programmer-specified. Does that mean I would have to specify the amount of bytes to read based on the string command I except to receive? Is it safe to do this using InputStream available bytes function? If so, this in conjunction with toString() may solve my problem.

    I don;t know how to parse the string.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,329
    Rep Power
    25

    Default Re: byte[] to double[][]

    I don;t know how to parse the string.
    Do you have a definition of all the Strings the device can write out?
    The one String you posted shows 3 fields separated by a ":". The String split method will parse that String into 3 Strings.

    inputStream's read function I know will store the bytes it reads into a byte array and reads up to some length of bytes, programmer-specified
    Again this goes back to the protocol of communications with the device. what and when does the device write? Can any of its possible output be written at any time?
    How does it signify the end of one String and the beginning of the next String?
    If you don't have a protocol and the output Strings are not delimited, that makes it a lot harder.

  12. #12
    kkGG is offline Member
    Join Date
    Nov 2010
    Posts
    28
    Rep Power
    0

    Default Re: byte[] to double[][]

    The CCU delimits commands by new lines and only responds given a "get:" command. Does the read() method of InputStream keep track of where it is in the stream?

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,329
    Rep Power
    25

    Default Re: byte[] to double[][]

    Does the read() method of InputStream keep track of where it is in the stream?
    It does in the java SE class. The InputStream has some methods you can use to change your location in the stream.
    What class is the InputStream you are talking about?
    You mentioned you were using classes from the gnu package. What does its doc say?

  14. #14
    kkGG is offline Member
    Join Date
    Nov 2010
    Posts
    28
    Rep Power
    0

    Default Re: byte[] to double[][]

    when you retrieve the inputstream from a port using the gnu package, you get a java.io.InputStream so yes. so a call to inputStream.read(byte[] bytes, 0, inputStream.available()) should read all bytes from the last place it was called I'm thinking

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,329
    Rep Power
    25

    Default Re: byte[] to double[][]

    If a new instance of InputStream is created, I have no idea where it would be reading from in the input stream.
    Once you get an InputStream, do you use that one instance for the rest of the application or do you get new instances every time you want to read something?

    Are you losing parts of your data?

  16. #16
    kkGG is offline Member
    Join Date
    Nov 2010
    Posts
    28
    Rep Power
    0

    Default Re: byte[] to double[][]

    I'll have to take it to the lab and test...in the mean time I want to thank you so much for all your help!

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,329
    Rep Power
    25

Similar Threads

  1. Replies: 1
    Last Post: 10-21-2011, 11:55 AM
  2. Read a bitmap byte by byte
    By cbr400aero in forum Java 2D
    Replies: 2
    Last Post: 10-18-2010, 11:42 PM
  3. Replies: 1
    Last Post: 04-21-2010, 02:28 AM
  4. double a * double b = weird output
    By GPB in forum New To Java
    Replies: 3
    Last Post: 03-26-2010, 10:40 AM
  5. Double.valueOf() vs Double.parseDouble()
    By greenbean in forum New To Java
    Replies: 10
    Last Post: 01-12-2009, 08:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •