Home > database >  Unable to view Serial Ports (ComPorts) on android
Unable to view Serial Ports (ComPorts) on android

Time:12-09

I installed Octo4A on my android phone. It installed Alpine linux and python3. When I run a python script to view the serial ports. It says no ports are found, but it does find the ports on my windows computer using the same script:

import serial.tools.list_ports as ports

def getAvailablePorts():
    availablePorts = list(ports.comports())
    return availablePorts

availablePorts = getAvailablePorts()

for port in availablePorts:
    print("Available port: "   port.device)

The output on windows:

Available port: COM3

How can I get a list of the available ports and connect to it on android using python3?

CodePudding user response:

If you look at the code, the way pyserial looks for ports in Linux is by trying to find the following strings:

/dev/ttyS*     # built-in serial ports
/dev/ttyUSB*   # usb-serial with own driver
/dev/ttyXRUSB* # xr-usb-serial port exar (DELL Edge 3001)
/dev/ttyACM*   # usb-serial with CDC-ACM profile
/dev/ttyAMA*   # ARM internal port (raspi)
/dev/rfcomm*   # BT serial devices
/dev/ttyAP*    # Advantech multi-port serial controllers
/dev/ttyGS*    # https://www.kernel.org/doc/Documentation/usb/gadget_serial.txt

In Octo4A, serial ports are apparently called /dev/ttyOcto4a so they will not be found by list_ports().

Of course, that does not mean pyserial won't work, you can try to instantiate and open your serial port directly with:

import serial

ser = serial.Serial(port='/dev/ttyOcto4a')

ser.isOpen()

I have not tried this myself, so I can not guarantee it will work.

CodePudding user response:

It is not possible to directly access serial ports (ComPorts) on an Android device using Python. Android does not provide a built-in API for accessing serial ports, and the Linux kernel on Android does not expose the necessary interfaces to allow access to serial ports.

However, you can use a USB-to-serial adapter to connect a serial device to your Android device, and then use an Android API such as the Android USB Host API to access the serial device through the USB interface. This approach may require you to write custom Android code to communicate with the serial device, but it will allow you to access the serial device on your Android device.

Alternatively, you can use a Bluetooth serial adapter to connect a serial device to your Android device wirelessly, and then use the Android Bluetooth API to access the serial device over Bluetooth. This approach may also require custom code, but it will allow you to access the serial device without using a physical connection.

In either case, you will need to modify your Python script to use the appropriate Android API to access the serial device, rather than trying to access the serial port directly. This will likely involve significant changes to your script, and may require knowledge of Android programming in addition to Python.

Try again

  • Related