1

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

Here are the serials listed in Octo4a: enter image description here

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

enter image description here

John
  • 5,942
  • 3
  • 42
  • 79
  • Hello John. What USB serial port and mobile phone are you using? Can you post the output of `lsusb` and `dmesg | grep usb`? – Marcos G. Dec 08 '22 at 15:37
  • @MarcosG. `lsusb` says `lsusb: /sys/bus/usb/devices: Permission denied`. The second command says `dmesg: klogctl: Permission denied` Using a coolpad 3632a running andoird 7.1.1. Connected with a OTG cable thats connected to a USB splitter. Both my arduino and the 3d printer show up on the usb list of Octo4a. – John Dec 08 '22 at 16:04
  • I see, your device is not rooted... Have you tried without that USB splitter? With both devices connected, they might not be getting enough power from your phone, you might want to try a USB hub with an external power supply. In any case, if the devices are detected by Octo4a, you might be facing a different issue. Octo4a uses a custom driver for the serial port, you might need to tweak something in pyserial to make it work. – Marcos G. Dec 09 '22 at 10:21

1 Answers1

2

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.

Marcos G.
  • 3,371
  • 2
  • 8
  • 16
  • I managed to communicate with my arduino with `/dev/ttyOcto4a`, but it seems that its shared with the printer on the same serial/usb. So if one is reading, the other isn't. Any ideas on how to separate them? – John Dec 10 '22 at 05:52
  • 1
    Sorry John, I don't know what you are trying to do. If you look [here](https://github.com/feelfreelinux/octo4a/tree/master/app/app/src/main/java/com/octo4a/serial) you'll find the code for the Octo serial driver, maybe that helps. I don't think the VID/PID of your device is listed in the [PrinterProber](https://github.com/feelfreelinux/octo4a/blob/master/app/app/src/main/java/com/octo4a/serial/PrinterProber.kt) file. I'm not at all familiar with Octo, maybe you can explain in detail what you want to do or post an issue on their GitHub. – Marcos G. Dec 10 '22 at 09:29
  • I'm trying to connect to my arduino through a octoprint plugin. I managed to make it work on windows because the printer shows up as `COM6` and my arduino as `COM5`. However, when connected through android. It only shows the one serial `/dev/ttyOcto4a/` and whichever device (3d printer or arduino) that is physically connected first on the usb hub is the one that is able to read the stream. I need some way to talk to each device separately. – John Dec 11 '22 at 11:36
  • 1
    Hello John. I think first you should try to see both serial ports on Android. Are you able to install [this app](https://play.google.com/store/apps/details?id=de.kai_morich.serial_usb_terminal&hl=en&gl=US&pli=1). Connecting two serial ports through a USB hub (and OTG cable) I was able to reproduce the screenshot shown on the store, both ports are detected with no problem, even when they have the same PID/VID. I did this on a non-rooted phone with Android 12. – Marcos G. Dec 11 '22 at 15:23
  • In the screenshot I have in my question. You can see both the usbs on the list on the Octo4a apk interface called `USB Serial` and `USB2.0-Ser!`. But in the octoprint web interface, it only shows `/dev/ttyOcto4a/` (second pic).On windows, it has two options called `COM5` and `COM6` for the two usbs. When I connect using `serial.Serial(port='/dev/ttyOcto4a')`. I am able to communicate with only one of the devices. Whichever was plugged into the usb splitter/hub first. How can I use `serial.Serial` on just one specific port? Is there something like `/dev/ttyOcto4a/COM5` or `/dev/COM5`? – John Dec 12 '22 at 04:11
  • 1
    Hello John, my point was that sometimes it's not so easy to connect two devices to an Android phone/tablet. [This](https://stackoverflow.com/questions/51551081/how-to-find-usb-devices-and-communicate-with-them-using-pyserial-in-android) is the best reference I can find. You might need to list the ports like that if your device is not rooted. At some point, I guess it would be easier to root. Some libraries don't even support [multiple ports](https://stackoverflow.com/questions/11183792/android-usb-host-and-hidden-devices). Right now I don't have any Android devices to play with. – Marcos G. Dec 12 '22 at 17:49
  • 1
    As far as I can see (I'm no Android expert, sorry) there is no way you can access your port easily as a `/dev/tty*` file on an unrooted device. You can try [the libusb](https://wiki.termux.com/wiki/Termux-usb) approach instead. It's a bit tricky but I was able to make it work. Since I have no idea what you want to do with your serial port I'm not sure it will be useful for you. As I said in my comment above, some apps will give you easy access to your serial port as a `/dev/bus/usb/*/*` usb descriptor. – Marcos G. Dec 12 '22 at 20:00
  • 1
    [This issue](https://github.com/termux/termux-packages/issues/1145) will give you more background on the problem and libusb solution. And [this](https://github.com/Querela/termux-usb-python) might be a more convenient solution (if it works, I did not have time to test it). It came from [this issue](https://github.com/pyusb/pyusb/issues/285). – Marcos G. Dec 12 '22 at 20:07
  • I am using my android phone to run Octo4a to control my 3d printer and the second usb connection I have on the phone is to my arduino which toggles the power switch using a tiny stepper motor. I wrote a plugin that flips the powers switch when a print order comes in and turns it off when it idles. I tried using a wifi outlet instead, but the brand I bought had no html API and the unofficial API had libraries that couldn't install on the phone. I'll take a look at the links you mentioned. Thanks – John Dec 13 '22 at 06:35
  • And I guess that plugin uses pyserial from within the octoprint app (or maybe not I'm of of my depth here)? Following on the links above you might want to take a look at [this issue](https://github.com/pyserial/pyserial/issues/428). It seems to be a solution to read/write to a USB serial port in Python on Android. I'm curious, please let me know if you manage to make it work, maybe I'll attempt some test myself if I have the time. Thanks. – Marcos G. Dec 13 '22 at 15:41
  • I tried the Serial USB Terminal app and it seems to work. I can select which USB device to connect to and communicate with it using their specific baudrate. However, they are listed as having the exact same name in the usb list. So I don't know how to open a port to each one individually using pyserial. I checked the source of that app and it can iterate the devices, but I can't seem to in python or the linux terminal – John Dec 20 '22 at 18:54
  • yes, that was my point: you can't access a serial port directly from pyserial or a terminal, you need to use some kind of trick like the libusb shown in my comments above or access it through Java like the apps. You always need something in between to be able to talk to the USB descriptor, something like [this](https://github.com/jacklinquan/usbserial4a). – Marcos G. Dec 24 '22 at 07:46