Merge pull request #96 from marcosdiez/dtr_and_rts

User can now set DTR and RTS on the fly
pull/144/merge
mike w 2016-03-23 19:31:56 -04:00
commit ccc8e8d3f0
3 zmienionych plików z 67 dodań i 7 usunięć

Wyświetl plik

@ -28,6 +28,8 @@ import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ScrollView;
import android.widget.TextView;
@ -64,6 +66,8 @@ public class SerialConsoleActivity extends Activity {
private TextView mTitleTextView;
private TextView mDumpTextView;
private ScrollView mScrollView;
private CheckBox chkDTR;
private CheckBox chkRTS;
private final ExecutorService mExecutor = Executors.newSingleThreadExecutor();
@ -95,8 +99,30 @@ public class SerialConsoleActivity extends Activity {
mTitleTextView = (TextView) findViewById(R.id.demoTitle);
mDumpTextView = (TextView) findViewById(R.id.consoleText);
mScrollView = (ScrollView) findViewById(R.id.demoScroller);
chkDTR = (CheckBox) findViewById(R.id.checkBoxDTR);
chkRTS = (CheckBox) findViewById(R.id.checkBoxRTS);
chkDTR.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
try {
sPort.setDTR(isChecked);
}catch (IOException x){}
}
});
chkRTS.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
try {
sPort.setRTS(isChecked);
}catch (IOException x){}
}
});
}
@Override
protected void onPause() {
super.onPause();
@ -112,6 +138,11 @@ public class SerialConsoleActivity extends Activity {
finish();
}
void showStatus(TextView theTextView, String theLabel, boolean theValue){
String msg = theLabel + ": " + (theValue ? "enabled" : "disabled") + "\n";
theTextView.append(msg);
}
@Override
protected void onResume() {
super.onResume();
@ -130,6 +161,15 @@ public class SerialConsoleActivity extends Activity {
try {
sPort.open(connection);
sPort.setParameters(115200, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE);
showStatus(mDumpTextView, "CD - Carrier Detect", sPort.getCD());
showStatus(mDumpTextView, "CTS - Clear To Send", sPort.getCTS());
showStatus(mDumpTextView, "DSR - Data Set Ready", sPort.getDSR());
showStatus(mDumpTextView, "DTR - Data Terminal Ready", sPort.getDTR());
showStatus(mDumpTextView, "DSR - Data Set Ready", sPort.getDSR());
showStatus(mDumpTextView, "RI - Ring Indicator", sPort.getRI());
showStatus(mDumpTextView, "RTS - Request To Send", sPort.getRTS());
} catch (IOException e) {
Log.e(TAG, "Error setting up device: " + e.getMessage(), e);
mTitleTextView.setText("Error opening device: " + e.getMessage());

Wyświetl plik

@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/demoTitle"
android:layout_width="match_parent"
@ -15,15 +15,34 @@
<View
android:id="@+id/separator"
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="#eeeeee" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/textBtnDTR"
android:id="@+id/checkBoxDTR" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/textBtnRTS"
android:id="@+id/checkBoxRTS" />
<View
android:id="@+id/separator2"
android:layout_width="match_parent"
android:layout_below="@+id/demoTitle"
android:layout_height="1dip"
android:background="#eeeeee" />
<ScrollView
android:id="@+id/demoScroller"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/separator" >
>
<TextView
android:id="@+id/consoleText"
@ -32,5 +51,4 @@
android:layout_height="wrap_content"
android:typeface="monospace" />
</ScrollView>
</RelativeLayout>
</LinearLayout>

Wyświetl plik

@ -4,5 +4,7 @@
<string name="app_title">USB Serial Example</string>
<string name="app_name">Serial Example</string>
<string name="refreshing">Refreshing...</string>
<string name="textBtnRTS">RTS - Request To Send</string>
<string name="textBtnDTR">DTR - Data Terminal Ready</string>
</resources>