CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 2002
    Location
    Holland
    Posts
    279

    vector or array (language understanding problem)

    Hi,
    simple question for java guru, less simple question for vb guru (me)

    I have got a program that javax.comm for serial communication.
    javax.comm uses byte arrays for communicating.
    I have got several kind messages to send, so I created an interface that hold the internal data and performs some calculation for the crc.

    some functions :
    BuildMessage
    getMessageCount
    getResponce
    CalcCRC()


    my question is, what is the best way in java hold the byte array.
    for example 1 message can be an array of 8 bytes and another can be 5 arrays of 80/80/80/80 and 45 in byte size.
    I know that there is an byte object. so what is the best way of keeping the information?

    in vb I hold the array in a collection but what is the best way in Java?

    thx,

    Jewe
    (java wanna be)
    A VB programmer trying to stay alive in a Real C World

    If the hardware is so great.. why use software to correct it..?? It will only slow it down..
    Al is de hardware nog zo snel de software achterhaalt het wel

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: vector or array (language understanding problem)

    Not entirely sure what you're asking here...

    In Java, if you want an array of bytes, you declare and use an array of bytes, e.g.
    Code:
    byte[] byteArray = new byte[ARRAY_SIZE];
    byte[][] byte2DArray = new byte[OUTER_ARRAY_SIZE][INNER_ARRAY_SIZE];
    There are other ways of declaring arrays, see any good language guide.
    You can hold such an array in a collection class if you want (although I'm not sure why you'd want to do this).

    If you could be a bit more specific with the question, the answers can be more specific.

    A man's reach should exceed his grasp, or what's heaven for?
    R. Browning
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    Mar 2002
    Location
    Holland
    Posts
    279

    Re: vector or array (language understanding problem)

    ok dlorde, I'll try again.

    For my program I created an interface called IMessage.
    this interface has the following items :
    1. buildmessage
    2. getMessage(index) as byte[]
    3. get messagecount.
    4. CalcCrc

    it is possible that an message object can create 1 or more messages.
    for example messageobject one :
    "Hello world"

    en messages object 2:
    "Hello world"
    and
    "How are you?"

    and the for messages object 3:
    "Give your ID"
    "Who are you?"
    "Give me your age?"
    "Are you alive?"
    "Abort"

    So far so good.
    The javaX.comm (serial communication) interface wants to have a byte[] as an argument.
    if I use byte[x][y], then I am sending more data than I inserted. and the receiving side is not the best receiver there is. It can not handle any other information then according to the protocol. one additional char and it will block the message.

    if it was C then I would have created an array of pointers to a char array. But its java so I'm wondering what the best solution is in java.

    I assumed that I could create array of bytes and add them to a collection.
    but as far as I know java does not have a std collection class for this.

    so please advice what to do.

    thx a lot,

    Jewe
    Last edited by Jewe; January 10th, 2005 at 09:00 AM.
    A VB programmer trying to stay alive in a Real C World

    If the hardware is so great.. why use software to correct it..?? It will only slow it down..
    Al is de hardware nog zo snel de software achterhaalt het wel

  4. #4
    Join Date
    Apr 2003
    Location
    Los Angeles area
    Posts
    776

    Re: vector or array (language understanding problem)

    If you are dealing with Strings, then use strings. Bytes seem to just be a method of intermediary transmission that you don't really have to deal with in Java if you don't want to (read InputStreamReader documentation to get some ideas.)

    If you need a special message object to deal specifically with bytes (I see you have a CRC method), then create a MyMessage class for that so that you have one reference point to deal with when you use it in a collection or array.

    Java has collections much like STL. I would use an ArrayList.

    If your objects need to be marked as IMessage to denote they CONTAIN message information then perhaps this design idea is not entirely correct. Consider having IMessage only put forth message management methods:
    Code:
    public List<MyMessage> getMessages();
    public boolean addMessage(MyMessage msg);
    public boolean removeMessage(MyMessage msg);
    and then have the other functionality you need, like CRC, be in the actual MyMessage class.

    Possible implementation for IMessage:
    Code:
    public List<MyMessage> getMessages(){
        return Collections.unmodifiableList( thisObjectsArrayListofMyMessages );
    }
    public boolean addMessage(MyMessage msg){
        return thisObjectsArrayListofMyMessages.add( msg );
    }
    public boolean removeMessage(MyMessage msg){
        return thisObjectsArrayListofMyMessages.remove( msg );
    }
    Having a message(s) and being a message are two different concepts that are not mutually exclusive. The MyMessage object is for clarity and sanity. The IMessage interface is to alert (most likely a collection) that this object contains a MyMessage(s). For example, everyone has something to say, a teacher has messages to students, a politician has messages to constiuents, and a KeepOutSign has a message to intruders. All of these can implement the IMessage interface and then in your program implementation you would be able to do the following:
    Code:
        ArrayList<IMessage> thingsThatHaveSomethingToSay = new ArrayList<IMessage>();
        Teacher t = new Teacher();
        Politician p = new Politician();
        KeepOutSign kos = new KeepOutSign();
        t.addMessage(new MyMessage("Homework is due now!");
        p.addMessage(new MyMessage("Four More Wars!");
        kos.addMessage("Prosecutors will be violated");
        thingsThatHaveSomethingToSay.add( t );
        thingsThatHaveSomethingToSay.add( p );
        thingsThatHaveSomethingToSay.add( kos );
    If your program does not need soemthing like a thingsThatHaveSomethingToSay object then you may not need an IMessage interface at all.
    "The Chicken and Rice MRE is not a personal lubricant."

  5. #5
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: vector or array (language understanding problem)

    Quote Originally Posted by Jewe
    The javaX.comm (serial communication) interface wants to have a byte[] as an argument.
    Just incidentally, which methods in javax.comm require a byte[] parameter? I just had a browse through the API (javax.comm) and I can't see any mention of a byte[] anywhere...

    We should try to be sure we're talking about the same things...
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  6. #6
    Join Date
    Mar 2002
    Location
    Holland
    Posts
    279

    Re: vector or array (language understanding problem)

    Quote Originally Posted by dlorde
    Just incidentally, which methods in javax.comm require a byte[] parameter? I just had a browse through the API (javax.comm) and I can't see any mention of a byte[] anywhere...
    the outputstream has got three functions. they all use byte arrays. )] link to outputstream

    could be that there are other ways,

    Jewe
    A VB programmer trying to stay alive in a Real C World

    If the hardware is so great.. why use software to correct it..?? It will only slow it down..
    Al is de hardware nog zo snel de software achterhaalt het wel

  7. #7
    Join Date
    Mar 2002
    Location
    Holland
    Posts
    279

    Re: vector or array (language understanding problem)

    Quote Originally Posted by Joe Nellis
    If you are dealing with Strings, then use strings.
    No, I need the bytes because somebody figured out a really unnatural protocol.
    (putting the message size half way, stripping the ascii value 10 down, crc check, having double zero as problem)

    The general idea was this:
    I have several messages that contain different information.
    for example messages that only hold the function number but no additional arguments. I created one class for it. but I also need to overwrite some flash memory. and I need to calc when a new page started. So I introduced the IMessage interface so that the class that is sending the message does not have to know about the intelligence or lack of it. it only gets the array of bytes and send it. the IMessage class also has some parameters so that the responce can be stored in the object, with the header and trailer stripped.

    If somebody has a better suggestion, please let me know.

    Jewe
    (Java wannabe)
    A VB programmer trying to stay alive in a Real C World

    If the hardware is so great.. why use software to correct it..?? It will only slow it down..
    Al is de hardware nog zo snel de software achterhaalt het wel

  8. #8
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: vector or array (language understanding problem)

    as dlorde has suggested, you would use a class that encapsulates a byte[] array. the byte holds your message in correctly formatted mode.

    you can call this class anything.. but a good idea is to have it implement an interface. the interface specifies what methods the class will contain, then you can make many different kinds of messages, and so long as they implement the interface, they will be transmittable. here is an example:


    Code:
    public interface JeweTransmittableMessage{
     public void buildmessage(...);
     public byte[] getMessage(index)
     public int getMessageCount()
     public String CalcCrc(...)
    }

    now say you ahve one message that is for overwriting flash memory, and another that is for disconnecting from the server, and another that is for ....

    Code:
    class OverwriteFlashMemory implements JeweTransmittableMessage{
      //fill in all the methods in the interface with relevant code for flashing
    Code:
    class DisconnectFromServer implements JeweTransmittableMessage{
      //fill in all the methods in the interface with relevant code for disconnecting


    then you ahve one class that does the communication:

    Code:
    class Communicator{
    
      //serial port setup etc
    
      JeweTransmittableMessage jtm = //whatever message to send
    
      for(int i = 0; i < jtm.getMessageCount(); i++){
        serialPort.sendData( jtm.getMessage(i) );
      }
    }
    it matters not now, what the message is, so long as it implements the interface it is type compatible with the sender method..

    and if you wanted then you can have lots of these messages to send one after the other:


    ArrayList<JeweTransmittableMessage> al = new ArrayList<JeweTransmittableMessage>();
    al.put( //message for login);
    al.put( //message for erase memory );
    al.put( //message for overwrite memory );
    al.put( //message for restore user prefs );
    al.put( //message X, Y Z whatever)


    then you'll have an ArrayList (like array but it grows when it is to small to hold a new element.. like vector, but lower resource intensiveness.) that you can pull everything out of and send as and when...

    for pulling things out of an arraylist in the order they were put in, see the Iterator related methods in the ArrayList API
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  9. #9
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: vector or array (language understanding problem)

    Quote Originally Posted by Jewe
    the outputstream[/URL] has got three functions. they all use byte arrays.

    could be that there are other ways,
    OK, the java.io.OutputStream (Java is case sensitive, please respect this) class is not part of the javax.comm package, it is part of the java.io package. The CommPort class returns a raw OutputStream so you can write to the port...

    You are not restricted to the low-level methods of the OutputStream. The java.io package has a whole bunch of classes that can wrap an OutputStream and handle all the messy conversions so you can send data in convenient formats without worrying about bytes unless you really want to.

    If you want to send Strings containing character-based messages, you should use OutputStreamWriter. This wraps the OutputStream and provides 'write' methods that take String arguments and do any character conversions required for the character set in use (see Writer and OutputStreamWriter). Remember that Java character sets are sixteen-bit Unicode and this class enables the correct mapping from a particular character set to raw bytes.

    If you're not interested in different character sets, or you want to send your own object type (i.e. an instance of a class you wrote), you can use ObjectOutputStream to write out any serializable object.

    For example:
    Code:
    String[] strings = { "A message",
                         "Another message",
                         "Yet another message" };
    
    OutputStreamWriter osWriter = null;
    try {
        // wrap the OutputStream in a Writer
        osWriter = new OutputStreamWriter(serialPort.getOutputStream());
    
        // write out the strings from the array
        for (String message : strings) {
            osWriter.write(message);
        }
    }
    catch (IOException e) {
        e.printStackTrace();
    }
    finally {
        // close any open streams and ports
    }
    Alternatively, use your own Message class with ObjectOutputStream, something like this:
    Code:
    ObjectOutputStream oos = null;
    try {
        oos = new ObjectOutputStream(serialPort.getOutputStream());
        MessageClass myMessage = getMessages(); // whatever...
        oos.writeObject(myMessage);
    }
    catch (IOException e) {
        e.printStackTrace();
    }
    finally {
        // close any open streams and ports
    }
    You can use ObjectOuputStream.writeObject(...) to write any object, including Strings (although you don't get character set mapping).

    Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it in the same way twice...
    C. Alexander
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured