sending data to an array
darnit
Posts: 25
I cant seem to find the proper sentax that will allow me to send the information i have in variables in my main program,·to the CRC routine (a separate class file). the crc routine initially sets up a character array and i dont know how to populate that array from the main program.
This is the data from the main program. (cmd,dlen,brightness):
while (menumode == 0){
········· while (brightness <=100){···· //increment brightness <= 100
··········· ++brightness;
··········· cmd = 14;
··········· dlen = 1;
This is where i need to send it:
public class CRC16 {
· public static char[noparse]/noparse data = new char[noparse][[/noparse]3];
·static void main() {
··· int crc;
··· data[noparse][[/noparse]0] = 14;·· //hard coded data just to get it to compile
··· data[noparse][[/noparse]1] = 1;··· //
··· data[noparse][[/noparse]2] = 12;· //
··· crc = compute(data);
··· Format.printf("%04x\n",crc);
please help!
·
This is the data from the main program. (cmd,dlen,brightness):
while (menumode == 0){
········· while (brightness <=100){···· //increment brightness <= 100
··········· ++brightness;
··········· cmd = 14;
··········· dlen = 1;
This is where i need to send it:
public class CRC16 {
· public static char[noparse]/noparse data = new char[noparse][[/noparse]3];
·static void main() {
··· int crc;
··· data[noparse][[/noparse]0] = 14;·· //hard coded data just to get it to compile
··· data[noparse][[/noparse]1] = 1;··· //
··· data[noparse][[/noparse]2] = 12;· //
··· crc = compute(data);
··· Format.printf("%04x\n",crc);
please help!
·
Comments
Library classes like your CRC16 must not have a main() method.
public class CRC16 {
··char[noparse]/noparse array = new char[noparse][[/noparse]3];
· //constructor
· public CRC16(int cmd, int dlen, int brightness) {
··· initArray(cmd,dlen,brightness); //set array to initial values
· }
· //method to setup array
· public void initArray(int cmd, int dlen, int brightness) {
··· array[noparse][[/noparse]0] = (char)cmd;
··· array[noparse][[/noparse]1] = (char)dlen;
··· array[noparse][[/noparse]2] = (char)brightness;
· }
· //more methods here
}
//Your main program
public class MainProgram {
· static CRC16 crc = new CRC16(14,1,12); //initialize array to·cmd=14, dlen=1, brightness=12
· static void main() {
··· //access CRC16 methods like crc.initArray(cmd,dlen,brightness)
· }
}
Note you can not call methods in your main program class from library classes,
unless you define abtract methods in your library class. The main program class
then must extend the library class. I advice against this road. Better to include
methods that access the CRC variables inside the CRC class (like initArray).
regards peter
I tried to incorporate your suggestions into the code and i cant seem to get it to work.· there is probably something simple i am missing. I am attaching the two files so you can look at them.....i cant seem to get my head around this problem.·
I am trying to send values developed in getpackets.java......send them to CRC16.java........and get the crc value back into getpackets.java so I can eventually send everything out to the lcd (my next problem).
That should do it. (Both files are changed)
regards peter
Are you sure?
It's a common idiom in Java to put the test code for a class in the main() method of that class. And I could swear I'd done exactly that on the Javelin.
I was under the impression that execution begain in the main() of the class I marked for download to the Javelin, and that any main() functions in other linked classes were ignored.
The class you select for download must have a main()
and this main is the entry point.
Any main() in other classes are just like other methods.
I usually test a class by writing a special test class containing the main().
So·my library classes will never have a main().
This also ensures me, that any class that I have, that has a main(),
is a useful program. It also keeps the libraries smaller, which is nice
because classes are entirely included in a compilation.
regards peter
········ mycrc = crc.compute(s.toCharArray());
That way you pass s to method compute().
Is that what you want? Array2 is in this way not involved.
regards peter
eventually i would like to use this for all messages so that i can get away from hard coding some of the commands. but.....i have to take small steps so i can learn..and with your help and others in this forum i have been learning.
thanks
Please provide a written example of input variables (eg. strings, integers)
and then apply some crc math that leads to a crc value.
For example input = "MYTEXT"
Write out how to calculate its crc value.
I think you are complicating the issue more than necessary.
A writtenout example may provide insight in what you want
and how to get it.
If necessary write it out binary.
regards peter
Ryan
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Ryan Clarke
Parallax Tech Support
RClarke@Parallax.com
for different kinds of data (brightness and data array).
I made the crc class a static class with no private arrays to be filled.
You assemble the array for which to calculate the checksum in your main class.
This establishes·a much clearer·project buildup.
I defined two assemble methods for two kinds of data (brightness and array).
The test class simply calculates and displays the checksum for these data.
Simply add more assemble methods for other kind of data you need.
Both files go into your projectA folder.
regards peter
·
Post Edited (Peter Verkaik) : 5/6/2006 2:29:52 PM GMT