Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am using usbSerialForAndroid library for serial communication in android.
In my code i have successfully granted read and write permission on USB port.

My problem is "I receive the data on third time when i try to read from serial port, first two times i read the data received is zero bytes".

Is there any specific setting that needs to be done for reading and writing to serial ports in android.

My sample code is here

Java
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
                        List<usbserialdriver> availableDrivers = UsbSerialProber.getDefaultProber().findAllDrivers(manager);
                        UsbSerialDriver driver = availableDrivers.get(0);
                        UsbDeviceConnection connection = manager.openDevice(driver.getDevice());
                        port = driver.getPorts().get(0);

                        try {

                            port.open(connection);
                            port.setParameters(9600, UsbSerialPort.DATABITS_8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE);
                            int usbResultOut, usbResultIn1, usbResultIn2, usbResultIn3;
                            String tOut = "S\r"  //This is the data i am sending to serial device.
                            byte[] bytesOut = tOut.getBytes(); //convert String to byte[]
                            byte[] bytesIn1 = new byte[25];
                            byte[] bytesIn2 = new byte[25];
                            byte[] bytesIn3 = new byte[25];

                            usbResultOut = port.write(bytesOut, 1000); //write the data to serial device.

                            usbResultIn1 = port.read(bytesIn1, 1000);  //read the data but in my case 0 bytes received.
                            usbResultIn2 = port.read(bytesIn2, 1000);  //read the data but in my case 0 bytes received.
                            usbResultIn3 = port.read(bytesIn3, 1000);  //read the data, this time the data is received.
}
Posted
Updated 3-Jun-18 1:07am
v2

It can happen, if there is no data available at the time of your call (for instance the attached device is slow in answering). You might act (if appropriate) on the timeoutMillis parameter of your the read method.
 
Share this answer
 
v2
When a serial device responds to received data, there is always a delay before it will be seen on the host. Imagine what typically happens:

  • The host sends some data (a string here). The sending may be performed in the background so that the write function returns while the data are still on the line.
  • The client recives the data and stores it usually inside a buffer.
  • Once the client detects the end of the command (probably the carriage return in your example), it decodes the command and sends an answer back.

With 8N1, each byte requires 10 / baud rate seconds to be transferred. Multiply this with the number of bytes send and received and add the command processing time of the client to know when the complete answer might be available.

There are two methods to check for available data: Polling and signal functions.

With polling, you will call the read function inside a loop until the return value indicates that data are available.

With signal functions, you will be get informed when new data are available. These will be typically used with threads. See the github example for your library at https://github.com/mik3y/usb-serial-for-android/blob/master/usbSerialExamples/src/main/java/src/com/hoho/android/usbserial/examples/SerialConsoleActivity.java[^].
 
Share this answer
 
Comments
Member 12354342 3-Jun-18 7:20am    
i want to send sms through my system using comm.port via android device in vb6. connection is bluetooth. in some sets of nokia it is simple. but could not find how to send (through android) with same steps like nokia. anyone knows about this?
David Crow 6-Jun-18 10:38am    
"...via android device in vb6."

You have an Android device that is running a VB6 app? Do tell!
lordqasim 24-Mar-20 22:25pm    
Hey there...i know its really late, but can you share your solution with PC code? I am all new to this serial stuff.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900