我正在使用最新的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);
}