jacklinquan / usbserial4a

Python package for Android USB host serial port.
MIT License
60 stars 17 forks source link

Timeout not working (correctly) #2

Open DottoreG opened 4 years ago

DottoreG commented 4 years ago

Using a timeout of 1 seconds does not work: serial4a waits at least 15 seconds if no data is available. Raising the timeout value seems to result in a higher effective timeout. But I am not able to achieve a timeout with shorter time.

jacklinquan commented 4 years ago

Using a timeout of 1 seconds does not work: serial4a waits at least 15 seconds if no data is available. Raising the timeout value seems to result in a higher effective timeout. But I am not able to achieve a timeout with shorter time.

Hi GottoreG,

Can you tell which usb serial driver you are working on? Among CDC CH34x CP210x FTDI PL2303

DottoreG commented 4 years ago

Hi jacklinquan, I observed the behaviour using the driver Pl2303Serial (Vendor id: 1659, Manufaturer name: Prolific Technology Inc., Product id: 8963, Product name: USB-Serial Controller). The device is not connected to any other device. So no data can be received. In the end I want to use the CDC driver. But until now I did not test the timeout behaviour using this device. Thanks!

jacklinquan commented 4 years ago

Hi jacklinquan, I observed the behaviour using the driver Pl2303Serial (Vendor id: 1659, Manufaturer name: Prolific Technology Inc., Product id: 8963, Product name: USB-Serial Controller). The device is not connected to any other device. So no data can be received. In the end I want to use the CDC driver. But until now I did not test the timeout behaviour using this device. Thanks!

Hi DottoreG, Thank you for testing this package! At the moment I don't have a PL2303 device available at hand. I'll look into it as soon as I get one.

PapoKarlo commented 4 years ago

hi, jacklinquan.

im also have problems with timeouts on CH340. im need to set read timeout less than 0.001 sec. but in real its about 10-15 sec.

also self.serial_port.in_waiting very slow

jacklinquan commented 4 years ago

Hi PapoKarlo,

Thank you for the report. But a read timeout less than 0.001 sec is not achievable. 1 millisecond(0.001 sec) is the minimum value software can handle, and different hardware has different capability to handle hardware read timeout. Could you help try a potential solution like this?: Before running self.serial_port.in_waiting, run this first: self.serial_port.USB_READ_TIMEOUT_MILLIS = [a hardware read timeout in milliseconds] For example, to set hardware read timeout to 10 milliseconds, do this:

self.serial_port.USB_READ_TIMEOUT_MILLIS = 10
num_of_received_bytes = self.serial_port.in_waiting

Further explanation: USB_READ_TIMEOUT_MILLIS is different from the timeout parameter you pass to __init__(). It is the hardware timeout that works behind the scene. Its value must be less than timeout to make timeout work correctly. But the tricky part is that this value is hardware dependent. It might need some error and trial to be tuned well. Let's say we set this value to 10 milliseconds, but the hardware might not accept it and use some other value instead. Try to play with USB_READ_TIMEOUT_MILLIS to make it work for your application.

PapoKarlo commented 4 years ago

oh thank you!

im play with self.serial_port.USB_READ_TIMEOUT_MILLIS = self.serial_port.timeout =

best effect what i get is 0.047 sec timeout

jacklinquan commented 4 years ago

oh thank you!

im play with self.serial_port.USB_READ_TIMEOUT_MILLIS = self.serial_port.timeout =

best effect what i get is 0.047 sec timeout

Hi PapoKarlo, Thanks for sharing the test result! It looks like 0.047 sec hits CH340's hardware limit.

PapoKarlo commented 4 years ago

the same code on kivy on windows host with same CH340 get 0,0019 sec. 20 times faster than android.

PapoKarlo commented 4 years ago

another strange thing, when i run my script via Pydroid 3 (python 3.7.2 and kivy 1.11.0) it works 2 times faster rather apk created with buildozer with same python and kivy versions.

with apk timeout 0.043 , in pydroid 0.019

jacklinquan commented 4 years ago

another strange thing, when i run my script via Pydroid 3 (python 3.7.2 and kivy 1.11.0) it works 2 times faster rather apk created with buildozer with same python and kivy versions.

with apk timeout 0.043 , in pydroid 0.019

Hi PapoKarlo, Thank you for the information! That surprises me as well! Since they use the same Python and Kivy, how could this timeout behavior be so different? I will keep it in mind and hope someday I can figure it out.