changed synchronous read in synchronous example

pull/75/head
Felipe Herranz 2016-09-18 18:38:47 +02:00
rodzic 5321490a3d
commit ee68de6f3a
4 zmienionych plików z 44 dodań i 45 usunięć

Wyświetl plik

@ -1,6 +1,9 @@
<manifest package="com.felhr.serialportexamplesync"
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-feature android:name="android.hardware.usb.host"
android:required="true"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
@ -8,6 +11,20 @@
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<service
android:name="com.felhr.serialportexamplesync.UsbService"
android:enabled="true">
</service>
</application>
</manifest>

Wyświetl plik

@ -19,13 +19,13 @@ import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.felhr.usbserial.UsbSerialDevice;
import java.lang.ref.WeakReference;
import java.util.Set;
public class MainActivity extends AppCompatActivity {
private static final int SYNC_READ = 3;
/*
* Notifications from UsbService will be received here.
*/
@ -91,20 +91,6 @@ public class MainActivity extends AppCompatActivity {
}
});
Button readButton = (Button) findViewById(R.id.buttonRead);
readButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
byte[] buffer = usbService.read(10);
mHandler.obtainMessage(SYNC_READ, buffer).sendToTarget();
}
}).start();
}
});
box9600 = (CheckBox) findViewById(R.id.checkBox);
box9600.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
@ -202,7 +188,7 @@ public class MainActivity extends AppCompatActivity {
case UsbService.DSR_CHANGE:
Toast.makeText(mActivity.get(), "DSR_CHANGE",Toast.LENGTH_LONG).show();
break;
case MainActivity.SYNC_READ:
case UsbService.SYNC_READ:
byte[] buffer = (byte[]) msg.obj;
mActivity.get().display.append(new String(buffer));
break;

Wyświetl plik

@ -12,6 +12,8 @@ import android.hardware.usb.UsbManager;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.SystemClock;
import android.util.Log;
import com.felhr.usbserial.CDCSerialDevice;
import com.felhr.usbserial.UsbSerialDevice;
@ -36,6 +38,7 @@ public class UsbService extends Service {
public static final int MESSAGE_FROM_SERIAL_PORT = 0;
public static final int CTS_CHANGE = 1;
public static final int DSR_CHANGE = 2;
public static final int SYNC_READ = 3;
private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
private static final int BAUD_RATE = 9600; // BaudRate. Change this value if you need
public static boolean SERVICE_CONNECTED = false;
@ -166,19 +169,6 @@ public class UsbService extends Service {
serialPort.syncWrite(data, 0);
}
/*
* This function will be called from MainActivity to read data through serial port
*/
public byte[] read(int nbytes){
if(serialPort != null){
byte[] buffer = new byte[nbytes];
serialPort.syncRead(buffer, 0);
return buffer;
}
return new byte[0];
}
/*
* This function will be called from MainActivity to change baud rate
*/
@ -274,6 +264,8 @@ public class UsbService extends Service {
serialPort.getCTS(ctsCallback);
serialPort.getDSR(dsrCallback);
new ReadThread().start();
//
// Some Arduinos would need some sleep because firmware wait some time to know whether a new sketch is going
// to be uploaded or not
@ -300,5 +292,20 @@ public class UsbService extends Service {
}
}
}
private class ReadThread extends Thread {
@Override
public void run() {
while(true){
byte[] buffer = new byte[100];
int n = serialPort.syncRead(buffer, 0);
if(n > 0) {
byte[] received = new byte[n];
System.arraycopy(buffer, 0, received, 0, n);
mHandler.obtainMessage(SYNC_READ, buffer).sendToTarget();
}
}
}
}
}

Wyświetl plik

@ -50,35 +50,24 @@
android:layout_marginTop="42dp"
android:text="Send"/>
<Button
android:id="@+id/buttonRead"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Read 10 bytes"
android:layout_below="@+id/buttonSend"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9600"
android:id="@+id/checkBox"
android:checked="true"
android:layout_below="@+id/buttonRead"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
android:layout_alignParentStart="true"
android:layout_below="@+id/buttonSend"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="38400"
android:id="@+id/checkBox2"
android:layout_below="@+id/buttonRead"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
android:layout_alignParentEnd="true"
android:layout_below="@+id/buttonSend"/>
<Button
android:layout_width="wrap_content"