Commit bd8c3f69 by Tref

upload structured project

parents
package com.tDiagnostics;
//import com.tDiagnostics.device.Connection;
import com.tDiagnostics.device.ConnectDevice;
public class Main {
public static void main(String[] args) {
// write your code here
char c = 1;
ConnectDevice connection = new ConnectDevice(c);
if(connection.startConnection()){
System.out.println("Success");
return;
}
System.out.println("failed");
}
}
package com.tDiagnostics.communication;
import com.fazecast.jSerialComm.SerialPort;
import com.tDiagnostics.communication.handler.ConnectionHandler;
import static com.fazecast.jSerialComm.SerialPort.NO_PARITY;
public class InitiateConnection {
public static boolean startComms(SerialPort comPort, ConnectionHandler con){
System.out.println("START_startComms");
comPort.setBaudRate(9600);
comPort.setNumDataBits(8);
comPort.setParity(NO_PARITY);
comPort.setNumStopBits(1);
comPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_BLOCKING, 0, 0);
con.sendZeroWith5Bd(comPort);//start
//comPort.clearDTR();//data
//Thread.sleep(200);
int par = 1;
int buf = con.controlUnit;
Integer funcs[] = new Integer[7];
for(int i=6;i>=0;i--) {
if(buf >= Math.pow(2,i)) {
buf -= Math.pow(2,i);
funcs[i] = 1;
par = par ^ 1;
}else {
funcs[i] = 0;
}
}
for(int x : funcs) {
if(x == 0)
con.sendZeroWith5Bd(comPort);
else
con.sendOneWith5Bd(comPort);
}
if(par == 0)
con.sendZeroWith5Bd(comPort);
else
con.sendOneWith5Bd(comPort);
con.sendOneWith5Bd(comPort);
//comPort.setDTR();
return true;
}
}
package com.tDiagnostics.communication.handler;
import com.fazecast.jSerialComm.SerialPort;
import com.tDiagnostics.device.Device;
import com.tDiagnostics.diagnostics.blocks.DTCBlock;
import com.tDiagnostics.diagnostics.blocks.GroupReadingBlock;
import java.util.Arrays;
import java.util.Formatter;
import static com.tDiagnostics.communication.InitiateConnection.startComms;
import static com.tDiagnostics.diagnostics.blocks.GroupReadingResponse.readFirstPackage;
public class ConnectionHandler {
public boolean WRONG_ANSWER;
public int syncbyte;
public int LSB;
public int HSB;
public char controlUnit = 0x01;
public char groupReading = 0x0a;
public boolean echoOn = false;
public boolean echoPrint = true;
public boolean startZeros = true;
public boolean skipEchoCheck = false;
public int blockCounter = 0;
public int blockTitle;
Integer groupReadingData[][][] = new Integer[26][4][3];
public ConnectionHandler(char module){
controlUnit = module;
}
public boolean nextPackage(int x, Device device){
System.out.println("START_nextPackage");
if(x == 0){
return startComms(device.uartDevice, this);
}else if(x == 1){
return readFirstPackage(device.uartDevice, this) == 0;
}else if(x == 2){
GroupReadingBlock grpReading = new GroupReadingBlock(this);
grpReading.sendGroupReading(device.uartDevice, controlUnit);
Package pack = new Package(1);
pack.setTitle(blockTitle);
pack.setModule(controlUnit);
pack.startTask(device.uartDevice, this);
grpReading.groupReadingData = pack.getGroupReadingData();
return true;
}else if(x == 3){
DTCBlock.sendDTCBlock(device.uartDevice, this);
Package pack = new Package(2);
pack.setTitle(blockTitle);
pack.setModule(controlUnit);
pack.startTask(device.uartDevice, this);
return true;
}else if(x == 4){
DTCBlock.removeDTCs(device.uartDevice, this);
Package pack = new Package(3);
pack.setTitle(blockTitle);
pack.setModule(controlUnit);
pack.startTask(device.uartDevice, this);
return true;
}
else if(x == 5){
System.out.println("Starting ECU read");
Package pack = new Package(0);
/*pack.setTitle(blockTitle);
pack.setModule(controlUnit);*/
pack.startTask(device.uartDevice, this);
return true;
}
return false;
}
private void nextBlock(){
}
public void sendZeroWith5Bd(SerialPort comPort){
try {
comPort.setBreak();//0
//Thread.sleep(200);
//comPort.setRTS();//data
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void sendOneWith5Bd(SerialPort comPort){
comPort.clearBreak();//1
try {
//comPort.clearRTS();//data
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public int bytesToHex(byte[] arr){
int hex;
Formatter formatter = new Formatter();
for (byte b : arr) {
formatter.format("%02x", b);
}
String str = formatter.toString();
hex = Integer.parseInt(str, 16);
return hex;
}
public byte[] toByteArray(final int data) {
return new byte[] {
(byte)((data >> 24) & 0xff),
(byte)((data >> 16) & 0xff),
(byte)((data >> 8) & 0xff),
(byte)((data >> 0) & 0xff),
};
}
public void cancelEcho(SerialPort in){
System.out.println("START_cancelEcho");
int len = 1;
byte buf[] = new byte[len];
if(echoOn) {//remove echo
/*while(true){
if (!(in.readBytes(buf, buf.length) == 0)) break;
}*/
in.readBytes(buf, buf.length);
System.out.print("ECHO: ");
System.out.println(Arrays.toString(buf));
}
/*try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}*/
}
public void sendByte(SerialPort port, int bytes, boolean echo){
System.out.println("START_sendByte");
port.writeBytes(toByteArray(bytes), 1);
if(echo)
cancelEcho(port);
}
public int readByte(SerialPort port, int chk, boolean echo){
System.out.println("START_readByte");
int dat;
byte buf[] = new byte[1];
/*while(true){
if (!(port.readBytes(buf, buf.length) == 0)) break;
}*/
port.readBytes(buf, buf.length);
dat = bytesToHex(buf);
if(dat != 0xFF - chk && echo)
WRONG_ANSWER = true;
return dat;
}
}
package com.tDiagnostics.communication.handler;
import com.fazecast.jSerialComm.SerialPort;
import com.tDiagnostics.diagnostics.blocks.ACKBlock;
import com.tDiagnostics.diagnostics.blocks.DTCBlock;
import com.tDiagnostics.diagnostics.blocks.GroupReadingResponse;
public class Package {
int arrSize = 4;
int arrPointer = 0;
private int module;
private boolean taskStarted;
private boolean taskCompleted;
private int lastTitle;
private int currentTask;//1-readsensor,2-readDTC,3-removeDTCs
public Integer groupReadingData[][] = new Integer[4][3];///group,nr,data
public String groupReadingDataString[][] = new String[26][4];
public String DTCs[] = new String[arrSize];
public Package(int task){
taskCompleted = false;
taskStarted = false;
currentTask = task;
}
public void startTask(SerialPort port, ConnectionHandler con){
System.out.println("START_startTask");
System.out.println("...");
readBlockData(port, con);
}
public void sendNextPackage(SerialPort port, ConnectionHandler con){
System.out.println("START_sendNextPackage");
if(taskCompleted){
ACKBlock.sendAckBlock(port, con);
}else{
if(lastTitle == 0x06){
endConnection();
}else if(lastTitle == 0x09){
carryOnWithTask(port, con);
}else if(lastTitle == 0xE7){
ACKBlock.sendAckBlock(port, con);
carryOnWithTask(port, con);
}else if(lastTitle == 0xFC){
ACKBlock.sendAckBlock(port, con);
carryOnWithTask(port, con);
}
}
}
private void carryOnWithTask(SerialPort port, ConnectionHandler con){
readBlockData(port, con);
}
private void endConnection(){
}
public void setModule(int dat){
module = dat;
}
public void setTitle(int dat){
lastTitle = dat;
}
public int getModule(){
return module;
}
public int getTitle(){
return lastTitle;
}
public void setTaskCompleted(boolean status){
taskCompleted = status;
}
public boolean isTaskCompleted() {
return taskCompleted;
}
public int readBlockData(SerialPort port, ConnectionHandler con){
System.out.println("Read block length");
int blockLength = con.readByte(port,0, false);
System.out.print("Block Length: ");
con.sendByte(port, 0xFF-blockLength, true);
System.out.println(blockLength);
con.blockCounter = con.readByte(port,0, false);
con.sendByte(port, 0xFF-con.blockCounter, true);
System.out.print("Block Counter: ");
System.out.println(con.blockCounter);
int blockTitle = con.readByte(port,0, false);
System.out.print("Block Title: ");
System.out.println(blockTitle);
con.sendByte(port, 0xFF-blockTitle, true);
switch(blockTitle){
case 0xE7:
lastTitle = 0xE7;
groupReadingData = GroupReadingResponse.readSensorDataBlock(port, con);
break;
case 0xF6:
lastTitle = 0xF6;
GroupReadingResponse.readECUDataPackage(port, con);
break;
case 0xFC:
DTCs = DTCBlock.readDTCBlock(port, con);
break;
default:
ACKBlock.readACK(port, con);
if(taskStarted)
taskCompleted = true;
else
taskStarted = true;
break;
}
sendNextPackage(port, con);
return blockTitle;
}
public Integer[][] getGroupReadingData(){
return groupReadingData;
}
}
package com.tDiagnostics.device;
import com.tDiagnostics.communication.handler.ConnectionHandler;
public class ConnectDevice {
boolean connectionStarted = false;
boolean connectionDataAquired = false;
boolean deviceDataAquired = false;
boolean moduleDataAquired = false;
boolean DTCAquired = false;
boolean DTCsDeleted = false;
ConnectionHandler connection;
public ConnectDevice(char module){
connection = new ConnectionHandler(module);
}
public boolean startConnection(){
System.out.println("START_startConnection");
Device device = new Device();
device.ConnectDevice("COM2");
do {
connectionStarted = connection.nextPackage(0, device);
System.out.println("No Connection ... ");
}while(!connectionStarted);
System.out.println("Connected");
connectionDataAquired = connection.nextPackage(1,device);
if(!connectionDataAquired){
return false;
}
System.out.println("Connection data obtained");
deviceDataAquired = connection.nextPackage(5,device);
if(!deviceDataAquired){
return false;
}
System.out.println("ECU data obtained");
//stop
moduleDataAquired = connection.nextPackage(2,device);//loop
if(!moduleDataAquired){
return false;
}
//DTCAquired = connection.nextPackage(3,device);
//DTCsDeleted = connection.nextPackage(4, device);
return connectionStarted & deviceDataAquired & moduleDataAquired;
}
}
package com.tDiagnostics.device;
import com.fazecast.jSerialComm.SerialPort;
public class Device {
public String deviceName;
public SerialPort uartDevice;
public void ConnectDevice(String port){
uartDevice = getDevice(port);
System.out.println(uartDevice.getDescriptivePortName());
}
private SerialPort getDevice(String port){
System.out.println("START_getDevice");
SerialPort device = null;
SerialPort[] deviceList = SerialPort.getCommPorts();
for (SerialPort temp : deviceList) {
if(temp.getSystemPortName().equals(port)) {
try {
temp.openPort();
device = temp;
break;
} catch (Exception e) {
e.printStackTrace();
}
}
}
return device;
}
}
package com.tDiagnostics.diagnostics.blocks;
import com.fazecast.jSerialComm.SerialPort;
import com.tDiagnostics.communication.handler.ConnectionHandler;
public class ACKBlock {
public static void sendAckBlock(SerialPort device, ConnectionHandler con){
System.out.println("START_sendAckBlock");
con.sendByte(device, 0x03, true);
con.readByte(device, 0x03, true);
if(con.WRONG_ANSWER)
return ;
con.sendByte(device, con.blockCounter+1, true);
con.readByte(device, con.blockCounter+1, true);
if(con.WRONG_ANSWER)
return ;
con.sendByte(device, 0x09, true);
con.readByte(device,0x09, true);
if(con.WRONG_ANSWER)
return ;
con.sendByte(device, 0x03, true);
}
public static void readACK(SerialPort port, ConnectionHandler con){
System.out.println("START_readACK");
int ack = con.readByte(port,0, false);
con.sendByte(port, 0xFF-ack, true);
con.readByte(port,0, false);
}
}
package com.tDiagnostics.diagnostics.blocks;
public class Block {
}
package com.tDiagnostics.diagnostics.blocks;
import com.fazecast.jSerialComm.SerialPort;
import com.tDiagnostics.communication.handler.ConnectionHandler;
public class DTCBlock{
static int arrSize = 4;
static String strDTC[] = new String[arrSize];
public static void sendDTCBlock(SerialPort device, ConnectionHandler con){
con.sendByte(device, 0x03, true);
con.readByte(device, 0x03, true);
if(con.WRONG_ANSWER)
return ;
con.sendByte(device, con.blockCounter+1, true);
con.readByte(device, con.blockCounter+1, true);
if(con.WRONG_ANSWER)
return ;
con.sendByte(device, 0x09, true);
con.readByte(device,0x09, true);
if(con.WRONG_ANSWER)
return ;
con.sendByte(device, 0x03, true);
}
public static String[] readDTCBlock(SerialPort port, ConnectionHandler con){
int i = 0;
for(i=0;i<4;i++){
int hex = 0x00;
for(int j=0;j<3;j++){
int asciiData = con.readByte(port,0, false);
if(asciiData == 0x03) {
i = 5;
break;
}
con.sendByte(port, 0xFF-asciiData, true);
if(j==0)
hex = 0x1100 * asciiData;
else if(j==1)
hex += asciiData;
}
strDTC[i] = String.format("%d", hex);
}
if(i==5)
con.readByte(port,0, false);//end
return strDTC;
}
public static int removeDTCs(SerialPort port, ConnectionHandler con){
con.sendByte(port, 0x03, true);
con.readByte(port, 0x03, true);
if(con.WRONG_ANSWER)
return 1;
con.sendByte(port, con.blockCounter+1, true);
con.readByte(port, con.blockCounter+1, true);
if(con.WRONG_ANSWER)
return 1;
con.sendByte(port, 0x05, true);
con.readByte(port, 0x05, true);
if(con.WRONG_ANSWER)
return 1;
con.sendByte(port, 0x03, true);
return 0;
}
}
package com.tDiagnostics.diagnostics.blocks;
import com.fazecast.jSerialComm.SerialPort;
import com.tDiagnostics.communication.handler.ConnectionHandler;
public class GroupReadingBlock {
public Integer groupReadingData[][] = new Integer[4][3];///group,nr,data
//public String groupReadingDataString[][] = new String[26][4];
ConnectionHandler con;
public GroupReadingBlock(ConnectionHandler connection){
con = connection;
}
public int sendGroupReading(SerialPort port, int key){
System.out.println("START_sendGroupReading");
con.sendByte(port, 0x04, true);
con.readByte(port, 0x04, true);
if(con.WRONG_ANSWER)
return 1;
con.sendByte(port, con.blockCounter+1, true);
con.readByte(port, con.blockCounter+1, true);
if(con.WRONG_ANSWER)
return 1;
con.sendByte(port, 0x29, true);
con.readByte(port, 0x29, true);
if(con.WRONG_ANSWER)
return 1;
con.sendByte(port, key, true);
con.readByte(port, key, true);
if(con.WRONG_ANSWER)
return 1;
con.sendByte(port, 0x03, true);
return 0;
}
}
package com.tDiagnostics.diagnostics.blocks;
import com.fazecast.jSerialComm.SerialPort;
import com.tDiagnostics.communication.handler.ConnectionHandler;
public class GroupReadingResponse {
public static int readFirstPackage(SerialPort in, ConnectionHandler con){
System.out.println("START_readFirstPackage");
int len = 1;
byte buf[] = new byte[len];
if(con.startZeros) {
in.readBytes(buf, buf.length);
in.readBytes(buf, buf.length);///mingi kahtlased nullid
System.out.println("ignored two zeroes");
}
in.readBytes(buf, buf.length);
con.syncbyte = con.bytesToHex(buf);
System.out.println("syncbyte: " + String.format("0x%02x", con.syncbyte));
if(con.syncbyte == 0)
con.startZeros = true;
if(con.syncbyte != 0x55)
return 1;
in.readBytes(buf, buf.length);
con.LSB = con.bytesToHex(buf);
System.out.println("LSB: " + String.format("0x%02x", con.LSB));
if(con.LSB != 0x01)
return 1;
in.readBytes(buf, buf.length);
con.HSB = con.bytesToHex(buf);
System.out.println("HSB: " + String.format("0x%02x", con.HSB));
if(con.HSB != 0x8A)
return 1;
in.writeBytes(con.toByteArray((0xFF - con.HSB)), len);
if(!con.echoOn && !con.skipEchoCheck) {
in.readBytes(buf, buf.length);
if (con.bytesToHex(buf) == 0xFF - con.HSB) {
con.echoOn = true;
return 1;
}else{
con.skipEchoCheck = true;
}
}else{
con.cancelEcho(in);
}
return 0;
}
//
public static void readECUDataPackage(SerialPort port, ConnectionHandler con){
System.out.println("START_readECUDataPackage");
for(;;){
int dat = con.readByte(port,0, false);
System.out.println(dat);
if(dat == 0x03)
break;
con.sendByte(port, 0xFF-dat, true);
}
}
public static Integer[][] readSensorDataBlock(SerialPort port, ConnectionHandler con){
System.out.println("START_readSensorDataBlock");
Integer blockReadingValues[][] = new Integer[4][3];
int j = 0, k = 0;
for(;;){
int dat = con.readByte(port,0, false);
if(dat == 0x03)
break;
con.sendByte(port, 0xFF-dat, true);
blockReadingValues[j][k] = dat;
k++;
if(k == 3){
k = 0;
j++;
}
}
return blockReadingValues;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment