Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed issue #79, baudrate detection not always reliable. Added baudra…

…te override to fix this.
  • Loading branch information...
commit 84627a8c66a549d4aae293cdc006c6ca4dd81c9d 1 parent 04e7a74
@jawi authored
View
24 tool.uart/src/main/java/nl/lxtreme/ols/tool/uart/BaudRateAnalyzer.java
@@ -29,11 +29,12 @@
* allows to evaluate each detected bit length. The bit length with the highest
* occurrence is used for baudrate calculation.
*/
+@SuppressWarnings( "boxing" )
final class BaudRateAnalyzer
{
// CONSTANTS
- private static final int[] COMMON_BAUDRATES = { 150, 300, 600, 1200, 2400, 4800, 9600, 19200, 28800, 38400, 57600,
+ public static final int[] COMMON_BAUDRATES = { 150, 300, 600, 1200, 2400, 4800, 9600, 19200, 28800, 38400, 57600,
115200, 230400, 460800, 921600 };
// VARIABLES
@@ -48,6 +49,25 @@
*
* @param aSampleRate
* the sample rate at which the incoming data was sampled;
+ * @param aFixedBaudRate
+ * the fixed baud rate that this analyzer should return.
+ */
+ public BaudRateAnalyzer( final int aSampleRate, final int aFixedBaudRate )
+ {
+ this.sampleRate = aSampleRate;
+ this.statData = new Frequency<Integer>();
+
+ // We already know our baudrate, so lets put a single value for the
+ // corresponding bitlength in our frequency mapping to let it be used...
+ final int bitLength = ( int )( aSampleRate / ( double )aFixedBaudRate );
+ this.statData.addValue( bitLength );
+ }
+
+ /**
+ * Creates a new BaudRateAnalyzer instance.
+ *
+ * @param aSampleRate
+ * the sample rate at which the incoming data was sampled;
* @param aValues
* the values to determine the baudrate for;
* @param aTimestamps
@@ -70,7 +90,7 @@ public BaudRateAnalyzer( final int aSampleRate, final int[] aValues, final long[
if ( lastBitValue != bitValue )
{
final int bitLength = ( int )( aTimestamps[i] - lastTransition );
- this.statData.addValue( Integer.valueOf( bitLength ) );
+ this.statData.addValue( bitLength );
lastTransition = aTimestamps[i];
}
View
42 tool.uart/src/main/java/nl/lxtreme/ols/tool/uart/UARTAnalyserTask.java
@@ -58,6 +58,7 @@
private UARTStopBits stopBits;
private UARTParity parity;
private int bitCount;
+ private int baudRate;
// CONSTRUCTORS
@@ -80,6 +81,7 @@ public UARTAnalyserTask( final ToolContext aContext, final ToolProgressListener
this.riIndex = -1;
this.dsrIndex = -1;
this.dtrIndex = -1;
+ this.baudRate = -1;
}
// METHODS
@@ -183,6 +185,17 @@ public boolean isInverted()
}
/**
+ * Sets baudRate to the given value.
+ *
+ * @param aBaudRate
+ * the baudRate to set.
+ */
+ public void setBaudRate( final int aBaudRate )
+ {
+ this.baudRate = aBaudRate;
+ }
+
+ /**
* @param aBitCount
*/
public void setBitCount( final int aBitCount )
@@ -287,6 +300,27 @@ public void setTxdIndex( final int aTxdIndex )
}
/**
+ * Factory method for creating the baud rate analyzer for the given
+ * acquisition results & bit mask.
+ *
+ * @param aData
+ * the acquisition results to use;
+ * @param aMask
+ * the bit mask of the data to use.
+ * @return a {@link BaudRateAnalyzer} instance, never <code>null</code>.
+ */
+ private BaudRateAnalyzer createBaudRateAnalyzer( final AcquisitionResult aData, final int aMask )
+ {
+ if ( this.baudRate <= 0 )
+ {
+ // Auto detect the baud rate...
+ return new BaudRateAnalyzer( aData.getSampleRate(), aData.getValues(), aData.getTimestamps(), aMask );
+ }
+ // Use a fixed baud rate...
+ return new BaudRateAnalyzer( aData.getSampleRate(), this.baudRate );
+ }
+
+ /**
* Decodes a control line.
*
* @param aDataSet
@@ -347,12 +381,12 @@ private void decodeData( final UARTDataSet aDataSet, final int aChannelIndex, fi
final AcquisitionResult data = this.context.getData();
final int mask = ( 1 << aChannelIndex );
- final BaudRateAnalyzer baudrateAnalyzer = new BaudRateAnalyzer( data.getSampleRate(), data.getValues(),
- data.getTimestamps(), mask );
-
- LOG.log( Level.FINE, "Baudrate = {0}bps", Integer.valueOf( baudrateAnalyzer.getBestBitLength() ) );
+ final BaudRateAnalyzer baudrateAnalyzer = createBaudRateAnalyzer( data, mask );
final int bitLength = baudrateAnalyzer.getBestBitLength();
+
+ LOG.log( Level.FINE, "Baudrate = {0}bps", Integer.valueOf( bitLength ) );
+
if ( bitLength <= 0 )
{
LOG.log( Level.INFO, "No (usable) {0}-data found for determining bitlength/baudrate ...",
View
44 tool.uart/src/main/java/nl/lxtreme/ols/tool/uart/UARTProtocolAnalysisDialog.java
@@ -61,6 +61,33 @@
// INNER TYPES
/**
+ * Cell item renderer for baud rates.
+ */
+ static final class BaudrateItemRenderer extends DefaultListCellRenderer
+ {
+ // CONSTANTS
+
+ private static final long serialVersionUID = 1L;
+
+ // METHODS
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Component getListCellRendererComponent( final JList aList, final Object aValue, final int aIndex,
+ final boolean aIsSelected, final boolean aCellHasFocus )
+ {
+ String text = ( aValue == null ) ? "" : aValue.toString();
+ if ( "-1".equals( text ) )
+ {
+ text = "Auto detect";
+ }
+ return super.getListCellRendererComponent( aList, text, aIndex, aIsSelected, aCellHasFocus );
+ }
+ }
+
+ /**
* Provides a combobox renderer for {@link UARTParity} values.
*/
static final class UARTParityItemRenderer extends EnumItemRenderer<UARTParity>
@@ -148,6 +175,7 @@ else if ( UARTStopBits.STOP_2.equals( aValue ) )
private JComboBox bits;
private JComboBox stop;
private JCheckBox inv;
+ private JComboBox baudrate;
private JEditorPane outText;
private RestorableAction runAnalysisAction;
@@ -310,6 +338,7 @@ protected void prepareToolTask( final ToolTask<UARTDataSet> aToolTask )
toolTask.setRiIndex( this.ri.getSelectedIndex() - 1 );
toolTask.setDsrIndex( this.dsr.getSelectedIndex() - 1 );
toolTask.setDtrIndex( this.dtr.getSelectedIndex() - 1 );
+ toolTask.setBaudRate( ( ( Integer )this.baudrate.getSelectedItem() ).intValue() );
// Other properties...
toolTask.setInverted( this.inv.isSelected() );
@@ -473,6 +502,19 @@ private JPanel createSettingsPane()
this.ri = SwingComponentUtils.createOptionalChannelSelector( channelCount );
settings.add( this.ri );
+ final Integer[] baudrates = new Integer[BaudRateAnalyzer.COMMON_BAUDRATES.length + 1];
+ baudrates[0] = Integer.valueOf( -1 );
+ int i = 1;
+ for ( int baudrate : BaudRateAnalyzer.COMMON_BAUDRATES )
+ {
+ baudrates[i++] = Integer.valueOf( baudrate );
+ }
+ this.baudrate = new JComboBox( baudrates );
+ this.baudrate.setSelectedIndex( 0 );
+ this.baudrate.setRenderer( new BaudrateItemRenderer() );
+ settings.add( createRightAlignedLabel( "Baudrate" ) );
+ settings.add( this.baudrate );
+
settings.add( createRightAlignedLabel( "Parity" ) );
this.parity = new JComboBox( UARTParity.values() );
this.parity.setSelectedIndex( 0 );
@@ -482,7 +524,7 @@ private JPanel createSettingsPane()
settings.add( createRightAlignedLabel( "Bits" ) );
final String[] bitarray = new String[10];
// allow word lengths between 5 and 14 bits...
- for ( int i = 0; i < bitarray.length; i++ )
+ for ( i = 0; i < bitarray.length; i++ )
{
bitarray[i] = String.format( "%d", Integer.valueOf( i + 5 ) );
}
Please sign in to comment.
Something went wrong with that request. Please try again.