refactoring
This commit is contained in:
parent
337865ffb5
commit
2182f051cb
|
@ -7,8 +7,6 @@ import com.rusefi.io.CommunicationLoggingHolder;
|
||||||
import com.rusefi.io.ConnectionStateListener;
|
import com.rusefi.io.ConnectionStateListener;
|
||||||
import com.opensr5.io.DataListener;
|
import com.opensr5.io.DataListener;
|
||||||
import com.rusefi.io.IoStream;
|
import com.rusefi.io.IoStream;
|
||||||
import jssc.SerialPort;
|
|
||||||
import jssc.SerialPortException;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -49,26 +47,10 @@ public class PortHolder {
|
||||||
* @return true if everything fine
|
* @return true if everything fine
|
||||||
*/
|
*/
|
||||||
private boolean open(String port, final DataListener listener) {
|
private boolean open(String port, final DataListener listener) {
|
||||||
SerialPort serialPort = new SerialPort(port);
|
SerialIoStreamJSSC stream = SerialIoStreamJSSC.open(port, BAUD_RATE, FileLog.LOGGER);
|
||||||
try {
|
if (stream == null)
|
||||||
FileLog.MAIN.logLine("Opening " + port + " @ " + BAUD_RATE);
|
|
||||||
boolean opened = serialPort.openPort();//Open serial port
|
|
||||||
if (!opened)
|
|
||||||
FileLog.MAIN.logLine(port + ": not opened!");
|
|
||||||
setupPort(serialPort, BAUD_RATE);
|
|
||||||
} catch (SerialPortException e) {
|
|
||||||
FileLog.MAIN.logLine("ERROR " + e.getMessage());
|
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
FileLog.MAIN.logLine("PortHolder: Sleeping a bit");
|
|
||||||
try {
|
|
||||||
// todo: why is this delay here? add a comment
|
|
||||||
Thread.sleep(200);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
throw new IllegalStateException(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
SerialIoStreamJSSC stream = new SerialIoStreamJSSC(serialPort, FileLog.LOGGER);
|
|
||||||
synchronized (portLock) {
|
synchronized (portLock) {
|
||||||
this.serialPort = stream;
|
this.serialPort = stream;
|
||||||
portLock.notifyAll();
|
portLock.notifyAll();
|
||||||
|
@ -79,16 +61,6 @@ public class PortHolder {
|
||||||
return bp.connectAndReadConfiguration(listener);
|
return bp.connectAndReadConfiguration(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setupPort(SerialPort serialPort, int baudRate) throws SerialPortException {
|
|
||||||
serialPort.setRTS(false);
|
|
||||||
serialPort.setDTR(false);
|
|
||||||
serialPort.setParams(baudRate, 8, 1, 0);//Set params.
|
|
||||||
int mask = SerialPort.MASK_RXCHAR;
|
|
||||||
//Set the prepared mask
|
|
||||||
serialPort.setEventsMask(mask);
|
|
||||||
serialPort.setFlowControlMode(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void close() {
|
public void close() {
|
||||||
synchronized (portLock) {
|
synchronized (portLock) {
|
||||||
if (serialPort != null) {
|
if (serialPort != null) {
|
||||||
|
|
|
@ -9,6 +9,8 @@ import jssc.SerialPortException;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import static com.rusefi.io.serial.PortHolder.BAUD_RATE;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* (c) Andrey Belomutskiy
|
* (c) Andrey Belomutskiy
|
||||||
* 5/11/2015.
|
* 5/11/2015.
|
||||||
|
@ -23,6 +25,39 @@ public class SerialIoStreamJSSC implements IoStream {
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void setupPort(SerialPort serialPort, int baudRate) throws SerialPortException {
|
||||||
|
serialPort.setRTS(false);
|
||||||
|
serialPort.setDTR(false);
|
||||||
|
serialPort.setParams(baudRate, 8, 1, 0);//Set params.
|
||||||
|
int mask = SerialPort.MASK_RXCHAR;
|
||||||
|
//Set the prepared mask
|
||||||
|
serialPort.setEventsMask(mask);
|
||||||
|
serialPort.setFlowControlMode(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static SerialIoStreamJSSC open(String port, int baudRate, Logger logger) {
|
||||||
|
SerialPort serialPort = new SerialPort(port);
|
||||||
|
try {
|
||||||
|
FileLog.MAIN.logLine("Opening " + port + " @ " + baudRate);
|
||||||
|
boolean opened = serialPort.openPort();//Open serial port
|
||||||
|
if (!opened)
|
||||||
|
FileLog.MAIN.logLine(port + ": not opened!");
|
||||||
|
SerialIoStreamJSSC.setupPort(serialPort, baudRate);
|
||||||
|
} catch (SerialPortException e) {
|
||||||
|
FileLog.MAIN.logLine("ERROR " + e.getMessage());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
FileLog.MAIN.logLine("PortHolder: Sleeping a bit");
|
||||||
|
try {
|
||||||
|
// todo: why is this delay here? add a comment
|
||||||
|
Thread.sleep(200);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
throw new IllegalStateException(e);
|
||||||
|
}
|
||||||
|
SerialIoStreamJSSC stream = new SerialIoStreamJSSC(serialPort, logger);
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void close() {
|
public void close() {
|
||||||
isClosed = true;
|
isClosed = true;
|
||||||
|
|
|
@ -6,7 +6,6 @@ import com.rusefi.binaryprotocol.BinaryProtocol;
|
||||||
import com.opensr5.io.ConfigurationImageFile;
|
import com.opensr5.io.ConfigurationImageFile;
|
||||||
import com.rusefi.binaryprotocol.BinaryProtocolHolder;
|
import com.rusefi.binaryprotocol.BinaryProtocolHolder;
|
||||||
import com.rusefi.io.LinkManager;
|
import com.rusefi.io.LinkManager;
|
||||||
import com.rusefi.io.serial.PortHolder;
|
|
||||||
import com.rusefi.io.serial.SerialIoStreamJSSC;
|
import com.rusefi.io.serial.SerialIoStreamJSSC;
|
||||||
import com.rusefi.ui.RecentCommands;
|
import com.rusefi.ui.RecentCommands;
|
||||||
import com.rusefi.ui.SettingsTab;
|
import com.rusefi.ui.SettingsTab;
|
||||||
|
@ -67,7 +66,7 @@ public class UploadChanges {
|
||||||
if (!opened) {
|
if (!opened) {
|
||||||
logger.error("failed to open " + port);
|
logger.error("failed to open " + port);
|
||||||
}
|
}
|
||||||
PortHolder.setupPort(serialPort, 38400);
|
SerialIoStreamJSSC.setupPort(serialPort, 38400);
|
||||||
logger.info("Configuration looks good!");
|
logger.info("Configuration looks good!");
|
||||||
|
|
||||||
final ConfigurationImage ci1 = ConfigurationImageFile.readFromFile("rus_saved.bin");
|
final ConfigurationImage ci1 = ConfigurationImageFile.readFromFile("rus_saved.bin");
|
||||||
|
|
|
@ -7,7 +7,6 @@ import com.romraider.editor.ecu.ECUEditor;
|
||||||
import com.rusefi.RomRaiderWrapper;
|
import com.rusefi.RomRaiderWrapper;
|
||||||
import com.rusefi.UploadChanges;
|
import com.rusefi.UploadChanges;
|
||||||
import com.rusefi.config.Fields;
|
import com.rusefi.config.Fields;
|
||||||
import com.rusefi.io.serial.PortHolder;
|
|
||||||
import com.rusefi.io.serial.SerialIoStreamJSSC;
|
import com.rusefi.io.serial.SerialIoStreamJSSC;
|
||||||
import jssc.SerialPort;
|
import jssc.SerialPort;
|
||||||
|
|
||||||
|
@ -35,7 +34,7 @@ public class BinaryProtocolCmdSandbox {
|
||||||
}
|
}
|
||||||
BinaryProtocol bp = BinaryProtocolHolder.create(logger, new SerialIoStreamJSSC(serialPort, logger));
|
BinaryProtocol bp = BinaryProtocolHolder.create(logger, new SerialIoStreamJSSC(serialPort, logger));
|
||||||
|
|
||||||
PortHolder.setupPort(serialPort, 38400);
|
SerialIoStreamJSSC.setupPort(serialPort, 38400);
|
||||||
logger.info("Binary looks good!");
|
logger.info("Binary looks good!");
|
||||||
bp.switchToBinaryProtocol();
|
bp.switchToBinaryProtocol();
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue