Need help in creating test application in Java and passing parameters into a new designed Java API.
Posted
by Christophe
on Stack Overflow
See other posts from Stack Overflow
or by Christophe
Published on 2010-05-28T14:13:45Z
Indexed on
2010/05/28
17:42 UTC
Read the original article
Hit count: 184
Need help, Please!!!
By following the protocol, the Request should be built in 5 byte length, including 1 byte for changing Braud rate (Speed), and send request to a RS-232 port.
Protocol:
Request for the command processing, with optional extra byte for changing Baud Rate:
LGT : length message ( LGT = 5 )
TYPE : 0x06
TO(time out): 0x0000
CMD : (1 byte) 0x02 application update
Baud Rate : (1 byte) 0xNN (optional parameter to change baud rate of the Mnt App)
where NN can be:
0x00 = No Baud Rate Change (similar to 4-byte command above)
0x09 = Change to 9600 Baud for Application Update speed
0x0A = Change to 19200 Baud for Application Update speed
0x0E = Change to 115200 Baud for Application Update speed
All other bytes are not accepted and will result in a status of 0x01.
I'm trying to test if my code works or not by creating another class (TestApplication.java) and pass the "3 differenr Baut rate" to this CPXAppliaction.
the 3 Baud Rate is supposed to input by reading a file.txt.
Question:
- How do you think these code (first half)? please don't warry about the details about the "sending part". I mean, do I need setter/getter for the "speed" parameter pass?
- I created the demo test class DemoApp.java (input speed by reading a txt file, and pass into CPXAppliaction). how do you think about that code?
Many thanks to you guys!!
public class CPXApplication extends CPXCommand {
private int speed; .
public CPXApplication() {
speed = 9600;
}
public CPXApplication(int speedinit) {
speed = speedinit; // TODO: where to get the speed?
}
protected void buildRequest() throws ElitePortException {
String trans = "";
// build the full-qualified message following the protocol
trans = addToRequest(trans, (char) 0);
trans = addToRequest(trans, (char) 5);
trans = addToRequest(trans, (char) 6);
trans = addToRequest(trans, (char) 0);
trans = addToRequest(trans, (char) 0);
trans = addToRequest(trans, (char) 2);
switch (speed) {
case 9600:
trans = addToRequest(trans, (char) 0x09);
break;
case 19200:
trans = addToRequest(trans, (char) 0x0A);
break;
case 115200:
trans = addToRequest(trans, (char) 0x0E);
break;
default:
// TODO: unexpected baud rate. throw();
break;
}
trans = EncryptBinary(trans);
trans = "F0." + trans;
wrapRequest(trans);
}
protected String addToRequest(String req, char c) {
return req + c;
}
protected String addToRequest(String req, String s) {
return req + s;
}
protected String addToRequest(String req) {
return req;
}
public void analyzeResponse() {
//..............
}
}
Here is the demo test code:
class Run {
public static void run()
{
CPXAppliaction input = new CpXApplication();
int lineno = 0;
try {
FileReader fr = new FileReader("baudRateSpeed.txt");
BufferedReader reader = new BufferedReader(fr);
String line = reader.readLine();
Scanner scan = null;
while (line != null) {
scan = new Scanner(line);
String speed;
speed = scan.next();
if (lineno == 0)
{
input.speed = speed;
lineno++;
}
else
{
input = cpxapplication(speed, input);
}
line = reader.readLine();
}
reader.close();
}
catch (FileNotFoundException e) {
System.out.println("Could not find the file");
}
catch (IOException e) {
System.out.println("Had a problem reading from file");
}
}
}
public class DemoApp{
public void main(String args[])
{
run();
}
© Stack Overflow or respective owner