From 9f32a3b7e9576d7214c383a862309f9f9a6be12d Mon Sep 17 00:00:00 2001 From: Felipe Herranz Date: Wed, 3 Jul 2019 23:36:44 +0200 Subject: [PATCH 1/2] back to previous ftdi syncRead method --- .../com/felhr/usbserial/FTDISerialDevice.java | 30 +++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/usbserial/src/main/java/com/felhr/usbserial/FTDISerialDevice.java b/usbserial/src/main/java/com/felhr/usbserial/FTDISerialDevice.java index 01b48db..f44c331 100644 --- a/usbserial/src/main/java/com/felhr/usbserial/FTDISerialDevice.java +++ b/usbserial/src/main/java/com/felhr/usbserial/FTDISerialDevice.java @@ -537,6 +537,30 @@ public class FTDISerialDevice extends UsbSerialDevice public class FTDIUtilities { + // Special treatment needed to FTDI devices + public byte[] adaptArray(byte[] ftdiData) + { + int length = ftdiData.length; + if(length > 64) + { + int n = 1; + int p = 64; + // Precalculate length without FTDI headers + while(p < length) + { + n++; + p = n*64; + } + int realLength = length - n*2; + byte[] data = new byte[realLength]; + copyData(ftdiData, data); + return data; + }else + { + return Arrays.copyOfRange(ftdiData, 2, length); + } + } + public void checkModemStatus(byte[] data) { if(data.length == 0) // Safeguard for zero length arrays @@ -624,10 +648,6 @@ public class FTDISerialDevice extends UsbSerialDevice return 0; } - if (mr1Version) { - return readSyncJelly(buffer, timeout, stopTime); - } - int n = buffer.length / 62; if(buffer.length % 62 != 0) { @@ -654,7 +674,7 @@ public class FTDISerialDevice extends UsbSerialDevice if(numberBytes > 2) // Data received { - byte[] newBuffer = adaptArray(tempBuffer); + byte[] newBuffer = this.ftdiUtilities.adaptArray(tempBuffer); System.arraycopy(newBuffer, 0, buffer, 0, buffer.length); int p = numberBytes / 64; From 2f4a4de99c15a84e4f27033fe9cfa7ab6f4d1bd7 Mon Sep 17 00:00:00 2001 From: Felipe Herranz Date: Fri, 5 Jul 2019 22:44:45 +0200 Subject: [PATCH 2/2] changed sync tests --- integration/integration_sync.py | 34 +++++++++++++++++++ .../felhr/integrationapp/MainActivity.java | 7 +++- .../felhr/integrationapp/UsbSyncService.java | 24 ++----------- 3 files changed, 43 insertions(+), 22 deletions(-) create mode 100644 integration/integration_sync.py diff --git a/integration/integration_sync.py b/integration/integration_sync.py new file mode 100644 index 0000000..7da8647 --- /dev/null +++ b/integration/integration_sync.py @@ -0,0 +1,34 @@ +# UsbSerial test: Integration test +# args: +# port (eg /dev/ttyUSB0) +# speed in bauds (eg 115200) + +import serial +import sys +import os + +class style(): + RED = lambda x: '\033[31m' + str(x) + GREEN = lambda x: '\033[32m' + str(x) + BLUE = lambda x: '\033[34m' + str(x) + RESET = lambda x: '\033[0m' + str(x) + +port = sys.argv[1] +speed = sys.argv[2] + +test_sizes = [1024, 2048, 16384] + +for i in range(0,3): + comm = serial.Serial(port, int(speed)) + print("Creating " + str(test_sizes[i]) + " bytes buffer") + data_tx = os.urandom(test_sizes[i]) + print("Sending buffer of " + str(test_sizes[i]) + " bytes") + comm.write(data_tx) + print("Receiving " + str(test_sizes[i]) + " bytes buffer") + data_rx = comm.read(test_sizes[i]) + + if data_tx == data_rx: + print(style.GREEN("Success: " + str(test_sizes[i]) + " bytes buffer was transmitted correctly")) + else: + print(style.RED("Error: " + str(test_sizes[i]) + " bytes buffer was not transmitted correctly")) + print(style.RESET("")) diff --git a/integrationapp/src/main/java/com/felhr/integrationapp/MainActivity.java b/integrationapp/src/main/java/com/felhr/integrationapp/MainActivity.java index 23076f2..8d25bfc 100644 --- a/integrationapp/src/main/java/com/felhr/integrationapp/MainActivity.java +++ b/integrationapp/src/main/java/com/felhr/integrationapp/MainActivity.java @@ -110,6 +110,7 @@ public class MainActivity extends AppCompatActivity { public void onResume() { super.onResume(); setFilters(); // Start listening notifications from UsbService + Toast.makeText(this, "Mode: " + String.valueOf(MODE), Toast.LENGTH_LONG).show(); if (MODE == 0) { startService(UsbService.class, usbConnection, null); // Start UsbService(if it was not started before) and Bind it }else if (MODE == 1) { @@ -121,7 +122,11 @@ public class MainActivity extends AppCompatActivity { public void onPause() { super.onPause(); unregisterReceiver(mUsbReceiver); - unbindService(usbConnection); + if(MODE == 0) { + unbindService(usbConnection); + }else{ + unbindService(usbSyncConnection); + } } private void startService(Class service, ServiceConnection serviceConnection, Bundle extras) { diff --git a/integrationapp/src/main/java/com/felhr/integrationapp/UsbSyncService.java b/integrationapp/src/main/java/com/felhr/integrationapp/UsbSyncService.java index 14e622f..50cf5cc 100644 --- a/integrationapp/src/main/java/com/felhr/integrationapp/UsbSyncService.java +++ b/integrationapp/src/main/java/com/felhr/integrationapp/UsbSyncService.java @@ -33,8 +33,6 @@ public class UsbSyncService extends Service { private static final int SIZE_TEST_1 = 1024; private static final int SIZE_TEST_2 = 2048; private static final int SIZE_TEST_3 = 16384; - private static final int SIZE_TEST_4 = 65535; - private static final int SIZE_TEST_5 = 131072; private static final String TEST_1 = "test1"; private static final String TEST_2 = "test2"; @@ -58,10 +56,6 @@ public class UsbSyncService extends Service { public static final int MESSAGE_TEST_1 = 0; public static final int MESSAGE_TEST_2 = 1; public static final int MESSAGE_TEST_3 = 2; - public static final int MESSAGE_TEST_4 = 3; - public static final int MESSAGE_TEST_5 = 4; - public static final int CTS_CHANGE = 1; - public static final int DSR_CHANGE = 2; private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION"; private static final int BAUD_RATE = 115200; // BaudRate. Change this value if you need public static boolean SERVICE_CONNECTED = false; @@ -282,11 +276,11 @@ public class UsbSyncService extends Service { @Override public void run() { while(true){ - byte[] tmpBuffer = new byte[BUFFER_SYNC]; + byte[] tmpBuffer = new byte[16384]; int n = serialPort.syncRead(tmpBuffer, 0); if(n > 0) { - buffer.write(tmpBuffer); + buffer.write(tmpBuffer, 0, n); } if(mode.equals(TEST_1)){ @@ -304,21 +298,9 @@ public class UsbSyncService extends Service { }else if(mode.equals(TEST_3)){ if(buffer.size() == SIZE_TEST_3){ - serialPort.syncWrite(buffer.readByteArray(), 0); - mode = TEST_4; - mHandler.obtainMessage(MESSAGE_TEST_3, "Test 16Kb completed correctly").sendToTarget(); - } - }else if(mode.equals(TEST_4)){ - if(buffer.size() == SIZE_TEST_4){ - serialPort.syncWrite(buffer.readByteArray(), 0); - mode = TEST_5; - mHandler.obtainMessage(MESSAGE_TEST_4, "Test 64Kb completed correctly").sendToTarget(); - } - }else if(mode.equals(TEST_5)){ - if(buffer.size() == SIZE_TEST_5){ serialPort.syncWrite(buffer.readByteArray(), 0); mode = TEST_1; - mHandler.obtainMessage(MESSAGE_TEST_5, "Test 128Kb completed correctly").sendToTarget(); + mHandler.obtainMessage(MESSAGE_TEST_3, "Test 16Kb completed correctly").sendToTarget(); } } }