Post Go back to editing

Implementing AN1074 to burn programs on ADuC847

I'm tryng to implement the protocol to burn my program on the ADuC847 on Java, in order to update the board software more easily via an HMI. The problem i'm facing is that some packages receive the ACK and others don't. Below are the code i developed, it would be very nice if someone could help me to find out what's wrong. 

import com.fazecast.jSerialComm.SerialPort;
import com.fazecast.jSerialComm.SerialPortDataListener;
import com.fazecast.jSerialComm.SerialPortEvent;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;

public class Main {

static public void main(String[] args) throws IOException, InterruptedException {

SerialPort comPort = SerialPort.getCommPort("COM4");
comPort.setComPortParameters(9600, 8, SerialPort.ONE_STOP_BIT, SerialPort.NO_PARITY);
comPort.openPort();

comPort.addDataListener(new SerialPortDataListener() {
@Override
public int getListeningEvents() { return SerialPort.LISTENING_EVENT_DATA_AVAILABLE; }
@Override
public void serialEvent(SerialPortEvent event)
{
if (event.getEventType() != SerialPort.LISTENING_EVENT_DATA_AVAILABLE)
return;
byte[] newData = new byte[comPort.bytesAvailable()];
comPort.readBytes(newData, newData.length);
System.out.println("RESPONSE:" + Arrays.toString(newData));
}
});


String filepath = "hex/path";
FileReader SensorsFile = null;
BufferedReader buff = null;
String line;
int size;
int addr;
int type;


SensorsFile = new FileReader(filepath);
buff = new BufferedReader(SensorsFile);

byte[] WakeUp = {0x21, 0x5A, 0x00, (byte) 0xA6};
comPort.writeBytes(WakeUp, 6);
System.out.println(Arrays.toString(WakeUp));
Thread.sleep(100);

byte[] Clearcmd = {0x07, 0x0E, 0x01, 0x43, (byte) 0xBC};
comPort.writeBytes(Clearcmd, Clearcmd.length);
System.out.println(Arrays.toString(Clearcmd));
Thread.sleep(5000);
while((line = buff.readLine()) != null){
size = Integer.parseInt(line.substring(1, 3), 16);
addr = Integer.parseInt(line.substring(3, 7), 16);
type = Integer.parseInt(line.substring(7, 9), 16);
if(type == 1) {
break;
}
byte[] toSend = new byte[size + 8];
toSend[0] = 0x07;
toSend[1] = 0x0E;
toSend[2] = (byte) (size + 4);
toSend[3] = 0x45;
toSend[4] = (byte) ((addr >> 16) & 0xFF);
toSend[5] = (byte) ((addr >> 8) & 0xFF);
toSend[6] = (byte) (addr & 0xFF);
for(int i = 0; i < size; i++){
toSend[i + 7] = (byte) Integer.parseInt(line.substring(i*2 + 9, i*2 + 11), 16);
}
int checksum = 0;
for(int i = 2; i < toSend.length - 1; i++){
checksum += toSend[i];
}
checksum = (byte) (checksum & 0xFF);
checksum = 0x100 - checksum;
toSend[toSend.length - 1] = (byte) checksum;
comPort.writeBytes(toSend, toSend.length);
System.out.println(Arrays.toString(toSend));
Thread.sleep(100);
}
byte[] SecureMode = {0x07, 0x0E, 0x02, 0x53, 0x05, (byte) 0xA6};
comPort.writeBytes(SecureMode, SecureMode.length);
System.out.println(Arrays.toString(SecureMode));
byte[] Run = {0x07, 0x0E, 0x04, 0x55, 0x00, 0x00, 0x00, (byte) 0xA7};
comPort.writeBytes(Run, Run.length);
System.out.println(Arrays.toString(Run));
}
}