xamarin - Xamarin Zebra Sdk - 蓝牙打印“读取失败,套接字可能关闭或超时,读取 ret:-1”
问题描述
我正在使用最新的 Xamarin Zebra SDK 打印到 ZQ520 打印机。大约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();
}
解决方案
当我尝试直接与我的设备连接时,我遇到了相关问题。我建议您在本地计算机中创建一个应用程序来与您的手机和打印机进行通信。还尝试增加代码中的超时。
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);
}
推荐阅读
- python - 每次用户要加密一个新文件时,我是否应该生成一个新的 RSA 密钥对?
- java - 使用java swing调用另一个类的方法来绘制当前类
- android - 当每个项目中有一个字段被内容更新时,设置更新过程的最佳方法是什么
- angular - 将像素偏移添加到角度材质 cdk 'scrollToIndex'
- c++ - 当使用 emplace_back 或 std::vector 的 push_back 创建时,在从只移动类型派生的类中定义析构函数会产生编译时错误
- css - 为什么字母间距在最后一个字符后添加一个空格?
- javascript - Knockout JS 突出显示在数组中检查的标签或行
- mysql - 数据库设计 - 多个外键
- bash - 在 bash 中创建 RS256 JWT
- javascript - 从数组和突变中删除元素的问题