star-micronics / StarXpand-SDK-Android

StarXpand SDK for Android is a software development kit for supporting application development for Star Micronics devices.
https://star-m.jp/starxpandsdk-oml.html
Other
13 stars 0 forks source link

Occasional StarIO10NotFoundException : Device not found. Connect the USB cable or turn on the device #20

Closed sk-huang closed 3 days ago

sk-huang commented 7 months ago

Description

Printing getExceptionStr: com.starmicronics.stario10.StarIO10NotFoundException: Device not found. Connect the USB cable or turn on the device (if the StarPrinter instance has been opened, call the closeAsync method first), and try again. errorCode: None at com.starmicronics.stario10.rawport.g.u(SourceFile) at com.starmicronics.stario10.rawport.g.n(SourceFile) at com.starmicronics.stario10.rawport.a.c(SourceFile) at com.starmicronics.stario10.a$a.b(SourceFile) at com.starmicronics.stario10.StarPrinter.p(SourceFile) at com.starmicronics.stario10.StarPrinter.l(SourceFile) at com.starmicronics.stario10.StarPrinter$s.invokeSuspend(SourceFile) at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:12) at kotlinx.coroutines.t0.run(DispatchedTask.kt:119) at kotlinx.coroutines.scheduling.CoroutineScheduler.D(CoroutineScheduler.kt:1) at kotlinx.coroutines.scheduling.CoroutineScheduler$c.d(CoroutineScheduler.kt:15) at kotlinx.coroutines.scheduling.CoroutineScheduler$c.n(CoroutineScheduler.kt:29) at kotlinx.coroutines.scheduling.CoroutineScheduler$c.run(CoroutineScheduler.kt:1)

Your device where the bug occurs

Your printer

Your development environment

android studio ProductName: macOS ProductVersion: 13.4.1 ProductVersionExtra: (c) BuildVersion: 22F770820d

Additional context

My devices is android 7.1.2, when i use tag v1.3 in my project, the online apps occasionally report device not found error. How to this bug?

My code snippet (the starPrinter is singleton):

val interfaceTypes = mutableListOf<InterfaceType>()
            interfaceTypes += InterfaceType.Usb
            deviceDiscoveryManager = StarDeviceDiscoveryManagerFactory.create(
                interfaceTypes,
                mContext
            )
            deviceDiscoveryManager?.discoveryTime = 5000
            deviceDiscoveryManager?.callback = object : StarDeviceDiscoveryManager.Callback {
                override fun onPrinterFound(printer: StarPrinter) {
                    if (printer.information?.emulation == StarPrinterEmulation.StarGraphic) {
                        MMKV.defaultMMKV()
                            .encode(PRINTER_IDENTIFIER, printer.connectionSettings.identifier)
                        starPrinter = printer
                    }
                    Logger.i("Found printer: ${printer}")
                }

                override fun onDiscoveryFinished() {
                    if (starPrinter == null) {
                        val usbIdentifier = MMKV.defaultMMKV().decodeString(PRINTER_IDENTIFIER)
                        usbIdentifier?.also {
                            val settings = StarConnectionSettings(InterfaceType.Usb, it)
                            starPrinter = StarPrinter(settings, mContext)
                        }
                    }

                    Logger.i("printer Discovery finished.  starPrinter: ${starPrinter}")
                }
            }

            deviceDiscoveryManager?.startDiscovery()
        } catch (e: Exception) {
            Logger.i("printer Discovery Error. ${e}")
        }
try {
            RTLogger.i("Printing", "printBitmap start")
            delay(200)
            starPrinter?.closeAsync()?.await()
            val imageBuilder = PrinterBuilder()
                .actionPrintImage(ImageParameter(bitmap, 384))
                .styleInternationalCharacter(InternationalCharacterType.China)
                .styleAlignment(Alignment.Center)
            if (isShouldCutPaper) {
                imageBuilder.actionCut(CutType.Partial)
            }

            val builder = StarXpandCommandBuilder()
            builder.addDocument(
                DocumentBuilder().addPrinter(imageBuilder)
            )
            val commands = builder.getCommands()
            RTLogger.i("Printing", "printBitmap openAsync, the printer is [${starPrinter}]")
            starPrinter?.openAsync()?.await()
            RTLogger.i("Printing", "printBitmap printAsync")
            starPrinter?.printAsync(commands)?.await()
            printResult = true
            RTLogger.i("Printing", "printBitmap Success")
        } catch (e: Exception) {
            printResult = false
            hasException = true
            val stackTrace = getExceptionStr(e)
            RTLogger.i("Printing", "Printing Error: ${stackTrace}")
            e.printStackTrace()
        } finally {
            RTLogger.i("Printing", "finally closeAsync")
            starPrinter?.closeAsync()?.await()
        }
sk-huang commented 6 months ago

I change the code , now starPrinter in application is always opend, not closed. But this printerDelegate will occasionally execute the callback of the onCommunicationError method. thie is my exception:

  1. RTPos-Monitor,Printer: Communication Error com.starmicronics.stario10.StarIO10CommunicationException: Write failed. errorCode: None
  2. RTPos-Monitor,Printer: Communication Error com.starmicronics.stario10.StarIO10CommunicationException: Request timed out. errorCode: None

and then call the printAsync method, an exception will be thrown: com.starmicronics.stario10.StarIO10InvalidOperationException: Not opened. errorCode: None

who can tell me why?

Tatsuki-Yamamoto2731 commented 5 months ago

@sk-huang It seems that you use V1.3.0 which is a previous version. Could you try the latest version V1.6.0?

Tatsuki-Yamamoto2731 commented 4 months ago

@sk-huang The causes of communication error are varied so I have not yet been able to identify the cause of your problem. Maybe the cable contact is not good. If possible, could you try replacing the cable?

Also, when "onCommunicationError" is called, you need to redo openAsync() before printAsync() because the instance of StarPrinter will be closed.

sk-huang commented 4 months ago

@sk-huang It seems that you use V1.3.0 which is a previous version. Could you try the latest version V1.6.0?

My device is android 7.1 , so can not upgrade to a new version.

sk-huang commented 4 months ago

@Tatsuki-Yamamoto2731
You say "when 'onCommunicationError'is called, you need to redo openAsync() before printAsync() because the instance of StarPrinter will be closed". But my code always calls the openAsync() before printAsync() . Once onCommunicationError occurs, the current printer instance call openAsync() or printAsync() will give an error.

Now, inside the catch method block, I'll recreate the printer instance, call the openAsync() and printAsync() again. I watched for a while, the probability of error becomes smaller

Tatsuki-Yamamoto2731 commented 4 months ago

@sk-huang You have changed your source code to create a new StarPrinter instance in the block of the catch method, but the problem is not completely resolved. From this, I think that the root cause may be a cable connection. I would appreciate it if you tried to replace it.

Tatsuki-Yamamoto2731 commented 4 months ago

@sk-huang Just in case, there is one more thing I would like to confirm. For Android, TSP100IIIU needs to connect an Android terminal via USB-B. (cannot use USB-A for communication.) You can refer to this page

Could you please confirm this?

sk-huang commented 4 months ago

@Tatsuki-Yamamoto2731
My device is a pos machine, and the manufacturer is elo. It integrates printing and scanning code. I see that the cables in the machine are designated interfaces, and the types of cables should be correct.

These machines have been used for several years, and it is also possible that individual machine cables have aged, resulting in poor contact

Last time I changed the code to create a new printer instance in the catch method block and print again. So far online environment has not reported this issue.

Next time this problem happens, i'll ask Dev Ops to try a new cable.

Tatsuki-Yamamoto2731 commented 3 days ago

@sk-huang Since there have been no reports of problems for more than 3 months, I am closing this Issue. If the problem is still ongoing, please open a new Issue.