auto-sync
This commit is contained in:
parent
dd60195a7b
commit
2298924a41
|
@ -0,0 +1,15 @@
|
|||
package com.rusefi.binaryprotocol;
|
||||
|
||||
import java.util.zip.CRC32;
|
||||
|
||||
/**
|
||||
* (c) Andrey Belomutskiy
|
||||
* 3/6/2015
|
||||
*/
|
||||
public class CRC {
|
||||
public static int crc32(byte buf[], int size) {
|
||||
CRC32 c = new CRC32();
|
||||
c.update(buf, 0, size);
|
||||
return (int) c.getValue();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package com.rusefi.binaryprotocol.test;
|
||||
|
||||
import com.rusefi.binaryprotocol.CRC;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* (c) Andrey Belomutskiy
|
||||
* 3/6/2015
|
||||
*/
|
||||
public class CRCTest {
|
||||
@Test
|
||||
public void testCrc() {
|
||||
{
|
||||
byte[] a = {'A'};
|
||||
assertEquals(0xD3D99E8B, CRC.crc32(a, a.length));
|
||||
}
|
||||
{
|
||||
byte[] a = {'S'};
|
||||
assertEquals(0x2060EFC3, CRC.crc32(a, a.length));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -5,5 +5,5 @@ package com.rusefi.io;
|
|||
* 3/3/14
|
||||
*/
|
||||
public interface DataListener {
|
||||
void onStringArrived(String string);
|
||||
void onDataArrived(byte freshData[]);
|
||||
}
|
||||
|
|
|
@ -49,10 +49,7 @@ public class PortHolder {
|
|||
boolean opened = serialPort.openPort();//Open serial port
|
||||
if (!opened)
|
||||
FileLog.MAIN.logLine("not opened!");
|
||||
serialPort.setParams(BAUD_RATE, 8, 1, 0);//Set params.
|
||||
int mask = SerialPort.MASK_RXCHAR;
|
||||
//Set the prepared mask
|
||||
serialPort.setEventsMask(mask);
|
||||
setupPort(serialPort);
|
||||
serialPort.addEventListener(new SerialPortReader(serialPort, listener));
|
||||
} catch (SerialPortException e) {
|
||||
FileLog.rlog("ERROR " + e.getMessage());
|
||||
|
@ -84,6 +81,13 @@ public class PortHolder {
|
|||
return true;
|
||||
}
|
||||
|
||||
public static void setupPort(SerialPort serialPort) throws SerialPortException {
|
||||
serialPort.setParams(BAUD_RATE, 8, 1, 0);//Set params.
|
||||
int mask = SerialPort.MASK_RXCHAR;
|
||||
//Set the prepared mask
|
||||
serialPort.setEventsMask(mask);
|
||||
}
|
||||
|
||||
public void close() {
|
||||
synchronized (portLock) {
|
||||
if (serialPort != null) {
|
||||
|
|
|
@ -12,12 +12,12 @@ import com.rusefi.io.LinkManager;
|
|||
class SerialManager {
|
||||
public static String port;
|
||||
|
||||
private static boolean closed;
|
||||
// private static boolean closed;
|
||||
|
||||
static DataListener dataListener = new DataListener() {
|
||||
public void onStringArrived(String string) {
|
||||
public void onDataArrived(byte freshData[]) {
|
||||
// jTextAreaIn.append(string);
|
||||
LinkManager.engineState.processNewData(string);
|
||||
LinkManager.engineState.processNewData(new String(freshData));
|
||||
}
|
||||
};
|
||||
private static LinkManager.LinkStateListener listener;
|
||||
|
@ -39,10 +39,10 @@ class SerialManager {
|
|||
@Override
|
||||
public void run() {
|
||||
MessagesCentral.getInstance().postMessage(SerialManager.class, "Restarting serial IO");
|
||||
if (closed)
|
||||
return;
|
||||
// if (closed)
|
||||
// return;
|
||||
PortHolder.getInstance().close();
|
||||
boolean result = PortHolder.getInstance().openPort(port, dataListener, SerialManager.listener);
|
||||
PortHolder.getInstance().openPort(port, dataListener, SerialManager.listener);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
@ -35,8 +35,7 @@ class SerialPortReader implements SerialPortEventListener {
|
|||
private void handleRx(SerialPortEvent spe) throws SerialPortException {
|
||||
if (spe.getEventValue() > 0) {
|
||||
byte[] buffer = serialPort.readBytes(spe.getEventValue());
|
||||
String str = new String(buffer);
|
||||
listener.onStringArrived(str);
|
||||
listener.onDataArrived(buffer);
|
||||
// System.out.println("arrived [" + str + "]");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue