-
January 10th, 2005, 06:53 AM
#1
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
-
January 10th, 2005, 07:12 AM
#2
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.
-
January 10th, 2005, 08:21 AM
#3
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
-
January 10th, 2005, 05:45 PM
#4
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."
-
January 10th, 2005, 06:33 PM
#5
Re: vector or array (language understanding problem)
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 [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.
-
January 11th, 2005, 02:45 AM
#6
Re: vector or array (language understanding problem)
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
-
January 11th, 2005, 03:09 AM
#7
Re: vector or array (language understanding problem)
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
-
January 11th, 2005, 04:47 AM
#8
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
-
January 11th, 2005, 06:47 AM
#9
Re: vector or array (language understanding problem)
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 [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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|