Skip to content

Commit

Permalink
DSR and CTS callbacks are executed on the beginning to know the statu…
Browse files Browse the repository at this point in the history
…s of the line
  • Loading branch information
felHR85 committed Feb 13, 2016
1 parent 7a87944 commit 9d2562d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 17 deletions.
47 changes: 31 additions & 16 deletions usbserial/src/main/java/com/felhr/usbserial/CP2102SerialDevice.java
Original file line number Diff line number Diff line change
Expand Up @@ -391,40 +391,55 @@ private class FlowControlThread extends Thread
{
private long time = 40; // 40ms

private boolean firstTime;

private AtomicBoolean keep;

public FlowControlThread()
{
keep = new AtomicBoolean(true);
firstTime = true;
}

@Override
public void run()
{
while(keep.get())
{
byte[] modemState = pollLines();

// Check CTS status
if(rtsCtsEnabled)
if(!firstTime) // Only execute the callback when the status change
{
if(ctsState != ((modemState[0] & 0x10) == 0x10))
byte[] modemState = pollLines();

// Check CTS status
if(rtsCtsEnabled)
{
ctsState = !ctsState;
if(ctsCallback != null)
ctsCallback.onCTSChanged(ctsState);
if(ctsState != ((modemState[0] & 0x10) == 0x10))
{
ctsState = !ctsState;
if (ctsCallback != null)
ctsCallback.onCTSChanged(ctsState);
}
}
}

// Check DSR status
if(dtrDsrEnabled)
{
if(dsrState != ((modemState[0] & 0x20) == 0x20))
// Check DSR status
if(dtrDsrEnabled)
{
dsrState = !dsrState;
if(dsrCallback != null)
dsrCallback.onDSRChanged(dsrState);
if(dsrState != ((modemState[0] & 0x20) == 0x20))
{
dsrState = !dsrState;
if (dsrCallback != null)
dsrCallback.onDSRChanged(dsrState);
}
}
}else // Execute the callback always the first time
{
if(rtsCtsEnabled && ctsCallback != null)
ctsCallback.onCTSChanged(ctsState);

if(dtrDsrEnabled && dsrCallback != null)
dsrCallback.onDSRChanged(dsrState);

firstTime = false;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -471,10 +471,17 @@ public void checkModemStatus(byte[] data)
boolean cts = (data[0] & 0x10) == 0x10;
boolean dsr = (data[0] & 0x20) == 0x20;

if(firstTime) // First modem status received, set the flags and exit
if(firstTime) // First modem status received
{
ctsState = cts;
dsrState = dsr;

if(rtsCtsEnabled && ctsCallback != null)
ctsCallback.onCTSChanged(ctsState);

if(dtrDsrEnabled && dsrCallback != null)
dsrCallback.onDSRChanged(dsrState);

firstTime = false;
return;
}
Expand Down

0 comments on commit 9d2562d

Please sign in to comment.