From 28cf2b1e98c2ad197d74127e31857e0d30d96565 Mon Sep 17 00:00:00 2001 From: James Rich <2199651+jamesarich@users.noreply.github.com> Date: Mon, 26 Jan 2026 06:55:11 -0600 Subject: [PATCH] fix(usb): Add error handling for serial write operations (#4318) Signed-off-by: James Rich <2199651+jamesarich@users.noreply.github.com> --- .../mesh/repository/usb/SerialConnectionImpl.kt | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/geeksville/mesh/repository/usb/SerialConnectionImpl.kt b/app/src/main/java/com/geeksville/mesh/repository/usb/SerialConnectionImpl.kt index 508e9642e..20a1b0d78 100644 --- a/app/src/main/java/com/geeksville/mesh/repository/usb/SerialConnectionImpl.kt +++ b/app/src/main/java/com/geeksville/mesh/repository/usb/SerialConnectionImpl.kt @@ -1,5 +1,5 @@ /* - * Copyright (c) 2025 Meshtastic LLC + * Copyright (c) 2025-2026 Meshtastic LLC * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -14,7 +14,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package com.geeksville.mesh.repository.usb import android.hardware.usb.UsbManager @@ -23,6 +22,7 @@ import com.geeksville.mesh.util.ignoreException import com.hoho.android.usbserial.driver.UsbSerialDriver import com.hoho.android.usbserial.driver.UsbSerialPort import com.hoho.android.usbserial.util.SerialInputOutputManager +import java.nio.BufferOverflowException import java.util.concurrent.CountDownLatch import java.util.concurrent.TimeUnit import java.util.concurrent.atomic.AtomicBoolean @@ -38,10 +38,17 @@ internal class SerialConnectionImpl( private val closed = AtomicBoolean(false) private val ioRef = AtomicReference() + @Suppress("TooGenericExceptionCaught") override fun sendBytes(bytes: ByteArray) { ioRef.get()?.let { Logger.d { "writing ${bytes.size} byte(s }" } - it.writeAsync(bytes) + try { + it.writeAsync(bytes) + } catch (e: BufferOverflowException) { + Logger.e(e) { "Buffer overflow while writing to serial port" } + } catch (e: Exception) { + Logger.e(e) { "Failed to write to serial port" } + } } }