Xamarin Zebra Sdk -蓝牙打印“读取失败,套接字可能关闭或超时,读取ret:-1英寸

8i9zcol2  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(486)

我正在使用最新的Xamarin Zebra SDK打印到ZQ 520打印机。大约70%的时间打印工作正常。另外30%的时间打印失败并出现错误
“读取失败,套接字可能已关闭或超时,读取ret:-1英寸
并且打印机需要被关闭/打开以进行打印。
我发送的内容是签名和标签,我通过不安全的蓝牙连接进行打印。
事实证明很难持续重现错误。我想这可能与我下面代码中的initialResponseTimeout和responseCompletionTimeout有关。有人有设置这些值的经验吗?

IConnection connection = null;

        try
        {

            connection = new BluetoothConnectionInsecure(address);
            connection.Open();

            using (var printer = ZebraPrinterFactory.GetInstance(
                               PrinterLanguage.Cpcl, connection))
            {

                using (var image = ZebraImageFactory.GetImage(signature))
                {
                   printer.StoreImage(SignatureFilename, image,
                              image.Width, image.Height);
                }

                // pause to ensure image is saved
                Thread.Sleep(1000);

                var initialResponseTimeout = 3000;
                var responseCompletionTimeout = 1000;

                // is the timeout too small or large ?
                connection.SendAndWaitForResponse(printLabel,
                initialResponseTimeout, responseCompletionTimeout, null);
            }

        }

        catch (Exception exception)
        {
            Microsoft.AppCenter.Crashes.Crashes.TrackError(exception);
        }
        finally
        {
            connection.Close();
        }
a0x5cqrl

a0x5cqrl1#

当我试图直接连接我的设备时,我遇到了相关的问题。我建议你在本地计算机中创建一个应用程序,以便与你的移动的和打印机进行通信。还可以尝试在代码中增加超时时间。

using (var printer = ZebraPrinterFactory.GetInstance(
                           PrinterLanguage.Cpcl, connection))
        {

            using (var image = ZebraImageFactory.GetImage(signature))
            {
               printer.StoreImage(SignatureFilename, image,
                          image.Width, image.Height);
            }

            // pause to ensure image is saved
            //Thread.Sleep(1000); I recommend you to remove this line

            var initialResponseTimeout = 15000;
            var responseCompletionTimeout = 15000;

            // is the timeout too small or large ?
            connection.SendAndWaitForResponse(printLabel,
            initialResponseTimeout, responseCompletionTimeout, null);
        }

相关问题