Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android USB Serial Support in pyserial with Chaquopy Integration #409

Open
Qlexio opened this issue Jan 27, 2025 · 0 comments
Open

Android USB Serial Support in pyserial with Chaquopy Integration #409

Qlexio opened this issue Jan 27, 2025 · 0 comments

Comments

@Qlexio
Copy link
Owner

Qlexio commented Jan 27, 2025

Description

This PR enhances pyserial to support serial connections on Android via Chaquopy and usb-serial-for-android. It enables pyserial to work in Android environments, specifically for firmware flashing tools like esptool.py.

Motivation

Android firmware developers need reliable serial connections to flash devices like ESP32/ESP8266 from Android apps. esptool.py, which uses pyserial, faces compatibility issues on Android. This PR integrates usb-serial-for-android to address this.

Context

Addresses the following issues: #192, #428, #603, #762

Implementation

  1. Chaquopy enables Python within Android, allowing pyserial to run.

  2. usb-serial-for-android is integrated to provide Android-compatible serial drivers.

  3. Example Usage: Call pyserial-based functions from Android using Kotlin.

    val python = Python.getInstance()
    val pyScript = python.getModule("upload_firmware")
    pyScript.callAttr("upload_firmware", this)
    
  4. Python Script: Sample upload_firmware.py for ESP32-S3 flashing via esptool.py.

import os
import esptool
from serial.android import set_android_context
from os.path import dirname, join
from android.content import Context



def upload_firmware(context:Context):
    firmware_path = (join(dirname(__file__), 'ESP32S3-Firmware.bin'))

    if not os.path.isfile(firmware_path):
        raise Exception(f"Firmware file not found at {firmware_path}")

    print("Starting firmware upload...")
    set_android_context(context)

    esptool.main(['--chip', 'esp32s3', '--baud', '230400', 'write_flash', '-z', '0x10000', firmware_path])

Testing

Demo App: https://github.com/xCarlost/FirmwareFlasher
This app flashes a test firmware onto an ESP32-S3.

@mcuee, @bdureau


This issue has been cloned from: pyserial#780

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant