parent
0c91e77749
commit
3382bc375e
|
@ -64,7 +64,7 @@ public class PortDetector {
|
|||
if (mode == DetectorMode.DETECT_ELM327) {
|
||||
BaudRateHolder.INSTANCE.baudRate = ELM327_DEFAULT_BAUDRATE;
|
||||
}
|
||||
new SerialAutoChecker(mode, serialPort, portFound).openAndCheckResponse(result, callback);
|
||||
new SerialAutoChecker(mode, serialPort, portFound).openAndCheckResponse(mode, result, callback);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -7,6 +7,7 @@ import com.rusefi.io.IoStream;
|
|||
import com.rusefi.io.can.Elm327Connector;
|
||||
import com.rusefi.io.commands.HelloCommand;
|
||||
import com.rusefi.io.serial.BufferedSerialIoStream;
|
||||
import com.rusefi.io.serial.SerialIoStream;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.IOException;
|
||||
|
@ -55,11 +56,15 @@ public class SerialAutoChecker {
|
|||
}
|
||||
}
|
||||
|
||||
public void openAndCheckResponse(AtomicReference<AutoDetectResult> result, Function<CallbackContext, Void> callback) {
|
||||
public void openAndCheckResponse(PortDetector.DetectorMode mode, AtomicReference<AutoDetectResult> result, Function<CallbackContext, Void> callback) {
|
||||
String signature;
|
||||
try (IoStream stream = BufferedSerialIoStream.openPort(serialPort)) {
|
||||
signature = checkResponse(stream, callback);
|
||||
IoStream stream;
|
||||
if (mode == PortDetector.DetectorMode.DETECT_ELM327) {
|
||||
stream = SerialIoStream.openPort(serialPort);
|
||||
} else {
|
||||
stream = BufferedSerialIoStream.openPort(serialPort);
|
||||
}
|
||||
signature = checkResponse(stream, callback);
|
||||
if (signature != null) {
|
||||
/**
|
||||
* propagating result after closing the port so that it could be used right away
|
||||
|
|
|
@ -3,7 +3,7 @@ package com.rusefi.io.can;
|
|||
import com.devexperts.logging.Logging;
|
||||
import com.opensr5.io.DataListener;
|
||||
import com.rusefi.io.IoStream;
|
||||
import com.rusefi.io.serial.BufferedSerialIoStream;
|
||||
import com.rusefi.io.serial.SerialIoStream;
|
||||
import com.rusefi.io.tcp.BinaryProtocolProxy;
|
||||
import com.rusefi.io.tcp.TcpConnector;
|
||||
|
||||
|
@ -95,7 +95,7 @@ public class Elm327Connector implements Closeable {
|
|||
public void start(String serialPort) {
|
||||
log.info("* Elm327.start()");
|
||||
|
||||
if (initConnection(serialPort, BufferedSerialIoStream.openPort(serialPort))) {
|
||||
if (initConnection(serialPort, SerialIoStream.openPort(serialPort))) {
|
||||
// reset to defaults
|
||||
sendCommand("ATD", "OK");
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@ import java.util.concurrent.atomic.AtomicInteger;
|
|||
public abstract class AbstractIoStream implements IoStream {
|
||||
private boolean isClosed;
|
||||
|
||||
protected StreamStats streamStats = new StreamStats();
|
||||
protected final StreamStats streamStats = new StreamStats();
|
||||
private final AtomicInteger bytesOut = new AtomicInteger();
|
||||
private long latestActivity;
|
||||
|
||||
|
|
|
@ -2,9 +2,6 @@ package com.rusefi.io.serial;
|
|||
|
||||
import com.devexperts.logging.Logging;
|
||||
import com.fazecast.jSerialComm.SerialPort;
|
||||
import com.fazecast.jSerialComm.SerialPortDataListener;
|
||||
import com.fazecast.jSerialComm.SerialPortEvent;
|
||||
import com.opensr5.io.DataListener;
|
||||
import com.rusefi.binaryprotocol.IncomingDataBuffer;
|
||||
import com.rusefi.io.IoStream;
|
||||
|
||||
|
@ -38,10 +35,8 @@ public class BufferedSerialIoStream extends SerialIoStream {
|
|||
* @see PortHolder#connectAndReadConfiguration()
|
||||
*/
|
||||
public static IoStream openPort(String port) {
|
||||
log.info("[SerialIoStreamJSerialComm] openPort " + port);
|
||||
SerialPort serialPort = SerialPort.getCommPort(port);
|
||||
serialPort.setBaudRate(BaudRateHolder.INSTANCE.baudRate);
|
||||
serialPort.openPort(0);
|
||||
log.info("[BufferedSerialIoStream] openPort " + port);
|
||||
SerialPort serialPort = openSerial(port);
|
||||
// FileLog.LOGGER.info("[SerialIoStreamJSerialComm] opened " + port);
|
||||
return new BufferedSerialIoStream(serialPort, port);
|
||||
}
|
||||
|
|
|
@ -5,16 +5,34 @@ import com.fazecast.jSerialComm.SerialPortDataListener;
|
|||
import com.fazecast.jSerialComm.SerialPortEvent;
|
||||
import com.opensr5.io.DataListener;
|
||||
import com.rusefi.binaryprotocol.IncomingDataBuffer;
|
||||
import com.rusefi.io.IoStream;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class SerialIoStream extends AbstractIoStream {
|
||||
protected final SerialPort sp;
|
||||
protected final String port;
|
||||
private boolean withListener;
|
||||
|
||||
public SerialIoStream(SerialPort sp, String port) {
|
||||
this.sp = sp;
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public static IoStream openPort(String port) {
|
||||
log.info("[SerialIoStream] openPort " + port);
|
||||
SerialPort serialPort = openSerial(port);
|
||||
// FileLog.LOGGER.info("[SerialIoStreamJSerialComm] opened " + port);
|
||||
return new SerialIoStream(serialPort, port);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected static SerialPort openSerial(String port) {
|
||||
SerialPort serialPort = SerialPort.getCommPort(port);
|
||||
serialPort.setBaudRate(BaudRateHolder.INSTANCE.baudRate);
|
||||
serialPort.openPort(0);
|
||||
return serialPort;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IncomingDataBuffer getDataBuffer() {
|
||||
throw new UnsupportedOperationException();
|
||||
|
@ -40,8 +58,14 @@ public class SerialIoStream extends AbstractIoStream {
|
|||
|
||||
@Override
|
||||
public void setInputListener(DataListener listener) {
|
||||
// datalistener can be redefined
|
||||
sp.removeDataListener();
|
||||
if (withListener) {
|
||||
/**
|
||||
* it looks like some drivers do not handle change of listener properly
|
||||
* AndreyB had this problem at least on a random ELM327 clone with CH340 serial chip
|
||||
*/
|
||||
throw new IllegalStateException("Not possible to change listener");
|
||||
}
|
||||
withListener = true;
|
||||
sp.addDataListener(new SerialPortDataListener() {
|
||||
private boolean isFirstEvent = true;
|
||||
|
||||
|
|
Loading…
Reference in New Issue