SocketCAN sender

This commit is contained in:
rusefillc 2024-01-06 00:15:03 -05:00
parent 1e9efdfa6d
commit e05d02475f
4 changed files with 46 additions and 2 deletions

View File

@ -7,8 +7,10 @@ apply from: 'dependencies.gradle'
group 'org.rusefi'
version '0.11-SNAPSHOT'
repositories {
mavenCentral()
allprojects {
repositories {
mavenCentral()
}
}
dependencies {

1
gradle.properties Normal file
View File

@ -0,0 +1 @@
javaCanVersion=3.2.4

View File

@ -6,4 +6,6 @@ plugins {
dependencies {
api rootProject
api project(':peak-can-basic')
api group: 'tel.schich', name: 'javacan-core', version: "$javaCanVersion"
api group: 'tel.schich', name: 'javacan-core', version: "$javaCanVersion", classifier: 'x86_64'
}

View File

@ -0,0 +1,39 @@
package com.rusefi.io.can;
import com.sun.istack.internal.NotNull;
import tel.schich.javacan.CanChannels;
import tel.schich.javacan.CanFrame;
import tel.schich.javacan.NetworkDevice;
import tel.schich.javacan.RawCanChannel;
import java.io.IOException;
import static tel.schich.javacan.CanFrame.FD_NO_FLAGS;
import static tel.schich.javacan.CanSocketOptions.RECV_OWN_MSGS;
public class SocketCANHelper {
@NotNull
public static RawCanChannel createSocket() {
final RawCanChannel socket;
try {
NetworkDevice canInterface = NetworkDevice.lookup(System.getProperty("CAN_DEVICE_NAME", "can0"));
socket = CanChannels.newRawChannel();
socket.bind(canInterface);
socket.configureBlocking(true); // we want reader thread to wait for messages
socket.setOption(RECV_OWN_MSGS, false);
} catch (IOException e) {
throw new IllegalStateException("Error looking up", e);
}
return socket;
}
public static void send(int id, byte[] payload, RawCanChannel channel) {
CanFrame packet = CanFrame.create(id, FD_NO_FLAGS, payload);
try {
channel.write(packet);
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
}