Skip to content

Commit

Permalink
Low Level controller now checks every byte, add flush() and re-enable…
Browse files Browse the repository at this point in the history
… serial block
  • Loading branch information
Christian S committed Jan 12, 2020
1 parent e3fc944 commit 8b40bb8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 24 deletions.
47 changes: 26 additions & 21 deletions src/main/java/de/csdev/ebus/core/EBusLowLevelController.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ public class EBusLowLevelController extends EBusControllerBase {

private static final Logger logger = LoggerFactory.getLogger(EBusLowLevelController.class);

private static final boolean DIRECT_MODE = true; // Boolean.getBoolean((System.getProperty("ebus.direct.mode",
// "false")));

protected IEBusConnection connection;

/** counts the re-connection tries */
Expand Down Expand Up @@ -303,13 +300,18 @@ private void send(boolean secondTry) throws IOException {

readByte = (byte) (connection.readByte(true) & 0xFF);

// clacule send receive roundtrip time in ns
// calculate send receive roundtrip time in ns
sendRoundTrip = System.nanoTime() - startTime;

// update the state machine
sendMachine.update(readByte);

if (b != readByte) {
if (readByte == -1) {
logger.warn("End of stream reached for first byte. Stop sending attempt ...");
queue.setBlockNextSend(true);
return;

} else if (b != readByte) {

// written and read byte not identical, that's
// a collision
Expand Down Expand Up @@ -343,14 +345,29 @@ else if ((byte) (readByte & 0x0F) == (byte) (b & 0x0F)) {
for (int i = 1; i < dataOutputBuffers.length; i++) {
byte b0 = dataOutputBuffers[i];

logger.trace("Send {}", EBusUtils.toHexDumpString(b0));
connection.writeByte(b0);

// read every byte immediately
if (DIRECT_MODE) {
byte b1 = (byte) (connection.readByte(true) & 0xFF);
sendMachine.update(b1);
byte b1 = (byte) (connection.readByte(true) & 0xFF);

if (logger.isTraceEnabled()) {
logger.trace("Send 0x{} -> Received 0x{}", EBusUtils.toHexDumpString(b0),
EBusUtils.toHexDumpString(b1));
}

if (b1 == -1) {
logger.warn("End of stream reached. Stop sending attempt ...");
queue.setBlockNextSend(true);
return;

} else if (b0 != b1) {
logger.warn("Received byte 0x{} is not equal to send byte 0x{}! Stop send attempt ...",
EBusUtils.toHexDumpString(b1), EBusUtils.toHexDumpString(b0));
queue.setBlockNextSend(true);
return;
}

sendMachine.update(b1);
}

// start of transfer successful
Expand All @@ -359,14 +376,6 @@ else if ((byte) (readByte & 0x0F) == (byte) (b & 0x0F)) {
queue.setLastSendCollisionDetected(false);
queue.setBlockNextSend(false);

// read all send bytes from input buffer, if DIRECT_MODE is disabled
if (!DIRECT_MODE) {
for (int i = 0; i < dataOutputBuffers.length - 1; i++) {
byte b0 = (byte) (connection.readByte(true) & 0xFF);
sendMachine.update(b0);
}
}

// read slave data if this is a master/slave telegram
if (sendMachine.isWaitingForSlaveAnswer()) {
logger.trace("Waiting for slave answer ...");
Expand Down Expand Up @@ -409,9 +418,6 @@ else if ((byte) (readByte & 0x0F) == (byte) (b & 0x0F)) {
queue.resetSendQueue();

} catch (EBusDataException e) {

// logger.error(e.getLocalizedMessage());

this.fireOnEBusDataException(e, sendEntry.id);

if (e.getErrorCode().equals(EBusDataException.EBusError.SLAVE_ACK_FAIL)) {
Expand Down Expand Up @@ -458,5 +464,4 @@ protected void fireWatchDogTimer() {
logger.error("error!", e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public abstract class AbstractEBusConnection implements IEBusConnection {
/** input stream for eBus communication */
protected InputStream inputStream;

@Override
public boolean close() throws IOException {

if (outputStream != null) {
Expand All @@ -41,34 +42,41 @@ public boolean close() throws IOException {

inputStream = null;
outputStream = null;

return true;
}

@Override
public boolean isOpen() throws IOException {
return inputStream != null;
}

@Override
public int readBytes(byte[] buffer) throws IOException {
return inputStream.read(buffer);
}

@Override
public int readByte(boolean lowLatency) throws IOException {
return inputStream.read();
}

@Override
public boolean isReceiveBufferEmpty() throws IOException {
return inputStream.available() == 0;
}

@Override
public void writeByte(int b) throws IOException {
outputStream.write(b);
outputStream.flush();
}

@Override
public void reset() throws IOException {
int available = inputStream.available();
if (available > 0) {
logger.debug("InputBuffer is not empty before sending: {} bytes waiting !", available);
logger.debug("InputBuffer is not empty before sending: {} byte(s) waiting !", available);
}
inputStream.skip(available);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public boolean open() throws IOException {
serialPort.setSerialPortParams(2400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);

serialPort.disableReceiveFraming();
serialPort.disableReceiveTimeout();
serialPort.enableReceiveThreshold(1);
serialPort.enableReceiveTimeout(10000);

// serialPort.disableReceiveThreshold();
// serialPort.enableReceiveTimeout(10000);

Expand All @@ -66,7 +71,7 @@ public boolean open() throws IOException {

// set buffers to 1 for low latency
serialPort.setOutputBufferSize(1);
serialPort.setInputBufferSize(1);
serialPort.setInputBufferSize(50);

// use event to let readByte wait until data is available, optimize cpu usage
serialPort.addEventListener(new SerialPortEventListener() {
Expand Down

0 comments on commit 8b40bb8

Please sign in to comment.