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; package com.rusefi.can;
import com.rusefi.io.can.PCanHelper; import com.rusefi.io.can.CanSender;
import peak.can.basic.PCANBasic;
import peak.can.basic.TPCANStatus;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
public class CanPacketSender { 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; int okCounter = 0;
for (CANPacket packet : logFileContent) { for (CANPacket packet : packets) {
TPCANStatus status = PCanHelper.send(pcan, packet.getId(), packet.getData()); boolean wasSendOk = sender.send(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());
}
if (status == TPCANStatus.PCAN_ERROR_OK) { if (wasSendOk) {
okCounter++; okCounter++;
} else {
System.out.println("Error sending " + status);
} }
if (okCounter % 1000 == 0) { if (okCounter % 1000 == 0) {
System.out.println(new Date() + ": Total " + okCounter + " OK messages"); System.out.println(new Date() + ": Total " + okCounter + " OK messages");
} }
} }
} }
} }

View File

@ -1,5 +1,5 @@
package com.rusefi.io.can; package com.rusefi.io.can;
public interface CanSender { 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(); PCANBasic pcan = createPCAN();
TPCANStatus initStatus = init(pcan); TPCANStatus initStatus = init(pcan);
if (initStatus != TPCANStatus.PCAN_ERROR_OK) { if (initStatus != TPCANStatus.PCAN_ERROR_OK) {
System.out.println("TPCANStatus " + initStatus); System.out.println("createAndInit: *** ERROR *** TPCANStatus " + initStatus);
System.exit(-1); System.exit(-1);
} }
return pcan; return pcan;
@ -38,11 +38,24 @@ public class PCanHelper {
public static CanSender create() { public static CanSender create() {
PCANBasic pcan = createAndInit(); PCANBasic pcan = createAndInit();
return new CanSender() { return (id, payload) -> {
@Override TPCANStatus status = send(pcan, id, payload);
public void send(int id, byte[] payload) {
PCanHelper.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(); RawCanChannel canChannel = createSocket();
return new CanSender() { return new CanSender() {
@Override @Override
public void send(int id, byte[] payload) { public boolean send(int id, byte[] payload) {
SocketCANHelper.send(id, payload, canChannel); SocketCANHelper.send(id, payload, canChannel);
return true;
} }
}; };
} }