jacklinquan / usbserial4a

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

Pydroid can run code from example when I click the run button but not when I run it from Pydroid terminal #11

Closed artisticfox8 closed 2 years ago

artisticfox8 commented 2 years ago

Hello, I know that you aren't the developer of Pydroid but you know your library usbserial4a best, so I would like to ask you a question about running Python files which use it using:

 /storage/emulated/0/ $ cd usb
 /storage/emulated/0/usb $ ls
file.py
 /storage/emulated/0/usb $ python file.py

The file works well if I open it and run it using the run button. However, I'd like to run a webserver, and for that I need to run the script from terminal. When I try the commands I posted I get this:

File "jnius/jnius_export_class.pxi", line 119, in jnius.jnius.MetaJavaClass.__new__

                                                                                                                                                                                    SystemError: NULL result without error in PyObject_Call

Thank you for making the usbserial4a library sir!

artisticfox8 commented 2 years ago

Screenshot_20211228-171552

artisticfox8 commented 2 years ago

Here is the code:

# When this script is run for the first time, it might prompt you for 
# permission. Accept the permission and run this script again, then it should 
# send the data as expected.

# Kivy is needed for pyjnius behind the scene.
import kivy
from usb4a import usb
from usbserial4a import serial4a
from pprint import pprint
import time

usb_device_list = usb.get_usb_device_list()
usb_device_name_list = [device.getDeviceName() for device in usb_device_list]
usb_device_dict = {
    device.getDeviceName():[            # Device name
        device.getVendorId(),           # Vendor ID
        device.getManufacturerName(),   # Manufacturer name
        device.getProductId(),          # Product ID
        device.getProductName()         # Product name
        ] for device in usb_device_list
    }
pprint(usb_device_dict)

if usb_device_list:
    serial_port = serial4a.get_serial_port(
        usb_device_list[0].getDeviceName(), 
        9600,   # Baudrate
        8,      # Number of data bits(5, 6, 7 or 8)
        'N',    # Parity('N', 'E', 'O', 'M' or 'S')
        1)      # Number of stop bits(1, 1.5 or 2)
    if serial_port and serial_port.is_open:
        b = 1
        while True:
            if b == 1:
                b=2
                text = "1,2,3" + "\n"
                t = text.encode("ascii")
                serial_port.write(t)
            elif b==2:
                b=1
                text = "28,44,44"+"\n"
                t = text.encode("ascii")
                serial_port.write(t)
            time.sleep(1)
jacklinquan commented 2 years ago

@artisticfox8 , Thank you for reporting this issue. usbserial4a depends on another package named usb4a which in turn depends on a package named pyjnius. pyjnius is part of kivy project. It wraps the underlayer java implementation and exposes a Python API. It looks like the problem happened at from jnius import autoclass So it is not something can be fixed in usb4a or usbserial4a. I'm not sure why running scripts in Pydroid terminal behaves differently from in its GUI. But for a webserver, there could be a workaround. You may try to run the webserver in another thread. Or run the webserver in main code and handle serial reading in another thread. I hope this can help you.