auto-sync

This commit is contained in:
rusEfi 2016-02-13 22:04:44 -05:00
parent 294f949bd3
commit dadf4f6172
5 changed files with 65 additions and 19 deletions

View File

@ -21,6 +21,7 @@ import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -126,7 +127,7 @@ public class BinaryProtocol {
} catch (ExecutionException e) { } catch (ExecutionException e) {
throw new IllegalStateException(e); throw new IllegalStateException(e);
} catch (TimeoutException e) { } catch (TimeoutException e) {
getLogger().error("timeout, giving up: " + e); getLogger().error("timeout sending [" + command + "] giving up: " + e);
return; return;
} }
/** /**
@ -204,7 +205,7 @@ public class BinaryProtocol {
synchronized (ioLock) { synchronized (ioLock) {
boolean isTimeout = incomingData.waitForBytes(2, start, "switch to binary"); boolean isTimeout = incomingData.waitForBytes(2, start, "switch to binary");
if (isTimeout) { if (isTimeout) {
logger.info("Timeout waiting for switch response"); logger.info(new Date() + ": Timeout waiting for switch response");
close(); close();
return; return;
} }

View File

@ -38,7 +38,9 @@ public class SerialIoStream implements IoStream {
@Override @Override
public void write(byte[] bytes) throws IOException { public void write(byte[] bytes) throws IOException {
try { try {
serialPort.writeBytes(bytes); synchronized (serialPort) {
serialPort.writeBytes(bytes);
}
} catch (SerialPortException e) { } catch (SerialPortException e) {
throw new IOException(e); throw new IOException(e);
} }
@ -47,7 +49,9 @@ public class SerialIoStream implements IoStream {
@Override @Override
public void purge() { public void purge() {
try { try {
serialPort.purgePort(SerialPort.PURGE_RXCLEAR | SerialPort.PURGE_TXCLEAR); synchronized (serialPort) {
serialPort.purgePort(SerialPort.PURGE_RXCLEAR | SerialPort.PURGE_TXCLEAR);
}
} catch (SerialPortException e) { } catch (SerialPortException e) {
logger.info("Error while purge: " + e); logger.info("Error while purge: " + e);
close(); close();

View File

@ -12,33 +12,57 @@ import jssc.SerialPortException;
* (c) Andrey Belomutskiy * (c) Andrey Belomutskiy
*/ */
public class SerialPortReader implements SerialPortEventListener { public class SerialPortReader implements SerialPortEventListener {
private SerialPort serialPort; private final SerialPort serialPort;
private DataListener listener; private DataListener listener;
public SerialPortReader(SerialPort serialPort, DataListener listener) { public SerialPortReader(final SerialPort serialPort, final DataListener listener) {
this.serialPort = serialPort; this.serialPort = serialPort;
this.listener = listener; this.listener = listener;
new Thread(new Runnable() {
@Override
public void run() {
try {
while (serialPort.isOpened()) {
byte[] data;
synchronized (serialPort) {
data = serialPort.readBytes();
}
if (data != null) {
listener.onDataArrived(data);
} else {
Thread.sleep(100);
}
}
} catch (SerialPortException e) {
e.printStackTrace();
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
}
}, "Reader_" + serialPort).start();
} }
public void serialEvent(SerialPortEvent spe) { public void serialEvent(SerialPortEvent spe) {
if (spe.isRXCHAR() || spe.isRXFLAG()) { if (spe.isRXCHAR() || spe.isRXFLAG()) {
try { // event-based serial read implementation does not work well on Windows 10 for some reason
handleRx(spe); // https://sourceforge.net/p/rusefi/tickets/264/
} catch (SerialPortException e) { // try {
e.printStackTrace(System.err); // handleRx(spe);
} // } catch (SerialPortException e) {
// e.printStackTrace(System.err);
// }
} else if (spe.getEventType() != SerialPortEvent.TXEMPTY) { } else if (spe.getEventType() != SerialPortEvent.TXEMPTY) {
FileLog.MAIN.logLine("less expected SerialPortReader serialEvent " + spe.getEventType()); FileLog.MAIN.logLine("less expected SerialPortReader serialEvent " + spe.getEventType());
} }
} }
private void handleRx(SerialPortEvent spe) throws SerialPortException { // private void handleRx(SerialPortEvent spe) throws SerialPortException {
if (spe.getEventValue() > 0) { // if (spe.getEventValue() > 0) {
byte[] buffer = serialPort.readBytes(spe.getEventValue()); // byte[] buffer = serialPort.readBytes(spe.getEventValue());
listener.onDataArrived(buffer); // listener.onDataArrived(buffer);
// System.out.println("arrived [" + str + "]"); // // System.out.println("arrived [" + str + "]");
} // }
} // }
public void readInitial() throws SerialPortException { public void readInitial() throws SerialPortException {
int input = serialPort.getInputBufferBytesCount(); int input = serialPort.getInputBufferBytesCount();

View File

@ -27,6 +27,7 @@ package jssc;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Date; import java.util.Date;
/** /**
@ -137,6 +138,7 @@ public class SerialPort {
* @return Method returns true if port is open, otherwise false * @return Method returns true if port is open, otherwise false
*/ */
public boolean isOpened() { public boolean isOpened() {
log("isOpened " + portOpened);
return portOpened; return portOpened;
} }
@ -305,6 +307,7 @@ public class SerialPort {
* @throws SerialPortException * @throws SerialPortException
*/ */
public int getEventsMask() throws SerialPortException { public int getEventsMask() throws SerialPortException {
log("getEventsMask");
checkPortOpened("getEventsMask()"); checkPortOpened("getEventsMask()");
if(SerialNativeInterface.getOsType() == SerialNativeInterface.OS_LINUX || if(SerialNativeInterface.getOsType() == SerialNativeInterface.OS_LINUX ||
SerialNativeInterface.getOsType() == SerialNativeInterface.OS_SOLARIS || SerialNativeInterface.getOsType() == SerialNativeInterface.OS_SOLARIS ||
@ -357,6 +360,7 @@ public class SerialPort {
* @throws SerialPortException * @throws SerialPortException
*/ */
public boolean writeBytes(byte[] buffer) throws SerialPortException { public boolean writeBytes(byte[] buffer) throws SerialPortException {
log("writeBytes " + Arrays.toString(buffer));
checkPortOpened("writeBytes()"); checkPortOpened("writeBytes()");
return serialInterface.writeBytes(portHandle, buffer); return serialInterface.writeBytes(portHandle, buffer);
} }
@ -371,6 +375,7 @@ public class SerialPort {
* @since 0.8 * @since 0.8
*/ */
public boolean writeByte(byte singleByte) throws SerialPortException { public boolean writeByte(byte singleByte) throws SerialPortException {
log("writeByte " + singleByte);
checkPortOpened("writeByte()"); checkPortOpened("writeByte()");
return writeBytes(new byte[]{singleByte}); return writeBytes(new byte[]{singleByte});
} }
@ -385,6 +390,7 @@ public class SerialPort {
* @since 0.8 * @since 0.8
*/ */
public boolean writeString(String string) throws SerialPortException { public boolean writeString(String string) throws SerialPortException {
log("writeString");
checkPortOpened("writeString()"); checkPortOpened("writeString()");
return writeBytes(string.getBytes()); return writeBytes(string.getBytes());
} }
@ -399,6 +405,7 @@ public class SerialPort {
* @since 2.8.0 * @since 2.8.0
*/ */
public boolean writeString(String string, String charsetName) throws SerialPortException, UnsupportedEncodingException { public boolean writeString(String string, String charsetName) throws SerialPortException, UnsupportedEncodingException {
log("writeString");
checkPortOpened("writeString()"); checkPortOpened("writeString()");
return writeBytes(string.getBytes(charsetName)); return writeBytes(string.getBytes(charsetName));
} }
@ -413,6 +420,7 @@ public class SerialPort {
* @since 0.8 * @since 0.8
*/ */
public boolean writeInt(int singleInt) throws SerialPortException { public boolean writeInt(int singleInt) throws SerialPortException {
log("writeInt " + singleInt);
checkPortOpened("writeInt()"); checkPortOpened("writeInt()");
return writeBytes(new byte[]{(byte)singleInt}); return writeBytes(new byte[]{(byte)singleInt});
} }
@ -427,6 +435,7 @@ public class SerialPort {
* @since 0.8 * @since 0.8
*/ */
public boolean writeIntArray(int[] buffer) throws SerialPortException { public boolean writeIntArray(int[] buffer) throws SerialPortException {
log("writeIntArray");
checkPortOpened("writeIntArray()"); checkPortOpened("writeIntArray()");
byte[] byteArray = new byte[buffer.length]; byte[] byteArray = new byte[buffer.length];
for(int i = 0; i < buffer.length; i++){ for(int i = 0; i < buffer.length; i++){
@ -462,6 +471,7 @@ public class SerialPort {
* @since 0.8 * @since 0.8
*/ */
public String readString(int byteCount) throws SerialPortException { public String readString(int byteCount) throws SerialPortException {
log("readString " + byteCount);
checkPortOpened("readString()"); checkPortOpened("readString()");
return new String(readBytes(byteCount)); return new String(readBytes(byteCount));
} }
@ -478,6 +488,7 @@ public class SerialPort {
* @since 0.8 * @since 0.8
*/ */
public String readHexString(int byteCount) throws SerialPortException { public String readHexString(int byteCount) throws SerialPortException {
log("readHexString " + byteCount);
checkPortOpened("readHexString()"); checkPortOpened("readHexString()");
return readHexString(byteCount, " "); return readHexString(byteCount, " ");
} }
@ -494,6 +505,7 @@ public class SerialPort {
* @since 0.8 * @since 0.8
*/ */
public String readHexString(int byteCount, String separator) throws SerialPortException { public String readHexString(int byteCount, String separator) throws SerialPortException {
log("readHexString");
checkPortOpened("readHexString()"); checkPortOpened("readHexString()");
String[] strBuffer = readHexStringArray(byteCount); String[] strBuffer = readHexStringArray(byteCount);
String returnString = ""; String returnString = "";
@ -709,6 +721,7 @@ public class SerialPort {
* @since 0.8 * @since 0.8
*/ */
public byte[] readBytes() throws SerialPortException { public byte[] readBytes() throws SerialPortException {
log("readBytes all");
checkPortOpened("readBytes()"); checkPortOpened("readBytes()");
int byteCount = getInputBufferBytesCount(); int byteCount = getInputBufferBytesCount();
if(byteCount <= 0){ if(byteCount <= 0){
@ -865,6 +878,7 @@ public class SerialPort {
* @since 0.8 * @since 0.8
*/ */
public int getFlowControlMode() throws SerialPortException { public int getFlowControlMode() throws SerialPortException {
log("getFlowControlMode");
checkPortOpened("getFlowControlMode()"); checkPortOpened("getFlowControlMode()");
return serialInterface.getFlowControlMode(portHandle); return serialInterface.getFlowControlMode(portHandle);
} }
@ -915,6 +929,7 @@ public class SerialPort {
* @throws SerialPortException * @throws SerialPortException
*/ */
public int[] getLinesStatus() throws SerialPortException { public int[] getLinesStatus() throws SerialPortException {
log("getLinesStatus");
checkPortOpened("getLinesStatus()"); checkPortOpened("getLinesStatus()");
return serialInterface.getLinesStatus(portHandle); return serialInterface.getLinesStatus(portHandle);
} }
@ -1086,6 +1101,7 @@ public class SerialPort {
* @throws SerialPortException * @throws SerialPortException
*/ */
public boolean removeEventListener() throws SerialPortException { public boolean removeEventListener() throws SerialPortException {
log("removeEventListener");
checkPortOpened("removeEventListener()"); checkPortOpened("removeEventListener()");
if(!eventListenerAdded){ if(!eventListenerAdded){
throw new SerialPortException(portName, "removeEventListener()", SerialPortException.TYPE_CANT_REMOVE_LISTENER); throw new SerialPortException(portName, "removeEventListener()", SerialPortException.TYPE_CANT_REMOVE_LISTENER);
@ -1115,6 +1131,7 @@ public class SerialPort {
* @throws SerialPortException * @throws SerialPortException
*/ */
public boolean closePort() throws SerialPortException { public boolean closePort() throws SerialPortException {
log("closePort");
checkPortOpened("closePort()"); checkPortOpened("closePort()");
if(eventListenerAdded){ if(eventListenerAdded){
removeEventListener(); removeEventListener();

View File

@ -34,7 +34,7 @@ import static com.rusefi.ui.storage.PersistentConfiguration.getConfig;
* @see EngineSnifferPanel * @see EngineSnifferPanel
*/ */
public class Launcher { public class Launcher {
public static final int CONSOLE_VERSION = 20160212; public static final int CONSOLE_VERSION = 20160213;
public static final boolean SHOW_STIMULATOR = false; public static final boolean SHOW_STIMULATOR = false;
private static final String TAB_INDEX = "main_tab"; private static final String TAB_INDEX = "main_tab";
protected static final String PORT_KEY = "port"; protected static final String PORT_KEY = "port";