This commit is contained in:
rusefillc 2024-01-12 22:30:23 -05:00
parent 632187c3ac
commit 6697d24878
4 changed files with 26 additions and 22 deletions

View File

@ -1,34 +1,24 @@
package com.rusefi.can;
import com.rusefi.io.can.PCanHelper;
import peak.can.basic.PCANBasic;
import peak.can.basic.TPCANStatus;
import com.rusefi.io.can.CanSender;
import java.util.Date;
import java.util.List;
public class CanPacketSender {
public static void sendMessagesOut(List<CANPacket> logFileContent, PCANBasic pcan) throws InterruptedException {
public static void sendMessagesOut(List<CANPacket> packets, CanSender sender) {
int okCounter = 0;
for (CANPacket packet : logFileContent) {
TPCANStatus status = PCanHelper.send(pcan, packet.getId(), packet.getData());
if (status == TPCANStatus.PCAN_ERROR_XMTFULL || status == TPCANStatus.PCAN_ERROR_QXMTFULL) {
Thread.sleep(10);
// System.out.println(String.format("Let's retry ID=%x", packet.getId()) + " OK=" + okCounter);
status = PCanHelper.send(pcan, packet.getId(), packet.getData());
}
for (CANPacket packet : packets) {
boolean wasSendOk = sender.send(packet.getId(), packet.getData());
if (status == TPCANStatus.PCAN_ERROR_OK) {
if (wasSendOk) {
okCounter++;
} else {
System.out.println("Error sending " + status);
}
if (okCounter % 1000 == 0) {
System.out.println(new Date() + ": Total " + okCounter + " OK messages");
}
}
}
}

View File

@ -1,5 +1,5 @@
package com.rusefi.io.can;
public interface CanSender {
void send(int id, byte[] payload);
boolean send(int id, byte[] payload);
}

View File

@ -30,7 +30,7 @@ public class PCanHelper {
PCANBasic pcan = createPCAN();
TPCANStatus initStatus = init(pcan);
if (initStatus != TPCANStatus.PCAN_ERROR_OK) {
System.out.println("TPCANStatus " + initStatus);
System.out.println("createAndInit: *** ERROR *** TPCANStatus " + initStatus);
System.exit(-1);
}
return pcan;
@ -38,11 +38,24 @@ public class PCanHelper {
public static CanSender create() {
PCANBasic pcan = createAndInit();
return new CanSender() {
@Override
public void send(int id, byte[] payload) {
PCanHelper.send(pcan, id, payload);
return (id, payload) -> {
TPCANStatus status = send(pcan, id, payload);
if (status == TPCANStatus.PCAN_ERROR_XMTFULL || status == TPCANStatus.PCAN_ERROR_QXMTFULL) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
// System.out.println(String.format("Let's retry ID=%x", packet.getId()) + " OK=" + okCounter);
status = send(pcan, id, payload);
}
boolean isHappy = status == TPCANStatus.PCAN_ERROR_OK;
if (!isHappy) {
System.out.println("Error sending " + status);
}
return isHappy;
};
}
}

View File

@ -41,8 +41,9 @@ public class SocketCANHelper {
RawCanChannel canChannel = createSocket();
return new CanSender() {
@Override
public void send(int id, byte[] payload) {
public boolean send(int id, byte[] payload) {
SocketCANHelper.send(id, payload, canChannel);
return true;
}
};
}