如何强制串行端口写入方法在发送数据之前等待线路清除?
以下是我正在尝试执行的操作的一些背景:
- 打开从移动设备到蓝牙打印机的串行端口。
- 将 EPL/2 表格发送到蓝牙打印机,以便它了解如何处理即将接收的数据。
- 收到表格后,将一些数据发送到打印机,这些数据将打印在标签纸上。
- 根据需要多次重复步骤 3,打印每个标签。
步骤 2 仅在第一次发生,因为表单不需要位于每个标签之前。 我的问题是,当我发送表单时,如果我发送标签数据太快,它将无法打印。 有时我会在标签上打印“蓝牙故障:无线电无法运行”,而不是我发送的数据。
我通过执行以下操作找到了解决该问题的方法:
for (int attempt = 0; attempt < 3; attempt++)
{
try
{
serialPort.Write(labelData);
break;
}
catch (TimeoutException ex)
{
// Log info or display info based on ex.Message
Thread.Sleep(3000);
}
}
所以基本上,我可以捕获 TimeoutException 并在等待一定时间后重试 write 方法(三秒似乎一直有效,但如果少于三秒,则似乎每次尝试都会抛出异常)。 经过三次尝试后,我只是假设串行端口有问题并让用户知道。
这种方法似乎工作正常,但我确信有更好的方法来处理这个问题。 SerialPort 类中有一些属性我认为我需要使用,但我找不到任何好的文档或如何使用它们的示例。 我尝试过使用其中的一些属性,但它们似乎都没有达到我想要实现的目标。
以下是我使用过的属性列表:
- CDHolding
- CtsHolding
- DsrHolding
- DtrEnable
- Handshake
- RtsEnable
我确信这些属性的某些组合将能够更优雅地处理我想要做的事情。
我正在使用 C#(2.0 框架)、Zebra QL 220+ 蓝牙打印机和 Windows Mobile 6 手持设备(如果这对解决方案有任何影响的话)。
任何建议,将不胜感激。
[更新]
我还应该注意到,移动设备使用蓝牙 2.0,而打印机仅使用 1.1 版本。 我假设速度差异是导致打印机接收数据滞后的原因。
Here's some background on what I'm trying to do:
- Open a serial port from a mobile device to a Bluetooth printer.
- Send an EPL/2 form to the Bluetooth printer, so that it understands how to treat the data it is about to receive.
- Once the form has been received, send some data to the printer which will be printed on label stock.
- Repeat step 3 as many times as necessary for each label to be printed.
Step 2 only happens the first time, since the form does not need to precede each label. My issue is that when I send the form, if I send the label data too quickly it will not print. Sometimes I get "Bluetooth Failure: Radio Non-Operational" printed on the label instead of the data I sent.
I have found a way around the issue by doing the following:
for (int attempt = 0; attempt < 3; attempt++)
{
try
{
serialPort.Write(labelData);
break;
}
catch (TimeoutException ex)
{
// Log info or display info based on ex.Message
Thread.Sleep(3000);
}
}
So basically, I can catch a TimeoutException and retry the write method after waiting a certain amount of time (three seconds seems to work all the time, but any less and it seems to throw the exception every attempt). After three attempts I just assume the serial port has something wrong and let the user know.
This way seems to work ok, but I'm sure there's a better way to handle this. There are a few properties in the SerialPort class that I think I need to use, but I can't really find any good documentation or examples of how to use them. I've tried playing around with some of the properties, but none of them seem to do what I'm trying to achieve.
Here's a list of the properties I have played with:
- CDHolding
- CtsHolding
- DsrHolding
- DtrEnable
- Handshake
- RtsEnable
I'm sure some combination of these will handle what I'm trying to do more gracefully.
I'm using C# (2.0 framework), a Zebra QL 220+ Bluetooth printer and a windows Mobile 6 handheld device, if that makes any difference for solutions.
Any suggestions would be appreciated.
[UPDATE]
I should also note that the mobile device is using Bluetooth 2.0, whereas the printer is only at version 1.1. I'm assuming the speed difference is what's causing the printer to lag behind in receiving the data.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
嗯,我已经根据已经给出的两个建议找到了一种方法来做到这一点。 我需要使用以下内容设置串行端口对象:
然后我只需要执行写入调用:
由于 Zebra 打印机支持软件流控制,因此当缓冲区接近满时,它将向移动设备发送 XOff 值。 这会导致移动设备等待从打印机发送 XOn 值,从而有效地通知移动设备它可以继续传输。
通过设置写入超时属性,我给出了抛出写入超时异常之前允许传输的总时间。 您仍然希望捕获写入超时,就像我在问题中的示例代码中所做的那样。 然而,没有必要循环 3 次(或任意数量),每次都尝试写入,因为软件流控制将启动和停止串行端口写入传输。
Well I've found a way to do this based on the two suggestions already given. I need to set up my serial port object with the following:
Then I just need to do the write call:
Since the Zebra printer supports software flow control, it will send an XOff value to the mobile device when the buffer is nearly full. This causes the mobile device to wait for an XOn value to be sent from the printer, effectively notifying the mobile device that it can continue transmitting.
By setting the write time out property, I'm giving a total time allowed for the transmission before a write timeout exception is thrown. You would still want to catch the write timeout, as I had done in my sample code in the question. However, it wouldn't be necessary to loop 3 (or an arbitrary amount of) times, trying to write each time, since the software flow control would start and stop the serial port write transmission.
流量控制是此处的正确答案,它可能不存在/实现/适用于您的蓝牙连接。
查看 Zebra 规范,看看它们是否实现了软件流控制(xon、xoff),或者您是否可以打开软件流控制(xon、xoff),这将允许您查看各个缓冲区何时已满。
此外,蓝牙无线电的最大传输速度不太可能超过 250k。 您可能会考虑人为地将其限制为 9,600bps - 这将为无线电提供大量喘息空间,用于重传、纠错、检测及其自身的流量控制。
如果所有其他方法都失败了,那么您现在使用的黑客方法还不错,但我会致电 Zebra 技术支持并在放弃之前了解他们的建议。
-亚当
Flow control is the correct answer here, and it may not be present/implemented/applicable to your bluetooth connection.
Check out the Zebra specification and see if they implement, or if you can turn on, software flow control (xon, xoff) which will allow you to see when the various buffers are getting full.
Further, the bluetooth radio is unlikely to be capable of transmitting faster than 250k at the maximum. You might consider artificially limiting it to 9,600bps - this will allow the radio a lot of breathing room for retransmits, error correction, detection, and its own flow control.
If all else fails, the hack you're using right now isn't bad, but I'd call Zebra tech support and find out what they recommend before giving up.
-Adam
问题可能不在于串行端口代码,而在于底层蓝牙堆栈。 您使用的端口是纯虚拟的,并且不太可能实现任何握手(因为这在很大程度上没有意义)。 CTS/RTS DTR/DSR 根本不适用于您正在处理的内容。
根本问题是,当您创建虚拟端口时,它必须绑定到蓝牙堆栈并连接到配对的串行设备。 端口本身不知道这可能需要多长时间,并且它可能被设置为异步执行此操作(尽管这完全取决于设备 OEM 如何完成),以防止调用者在没有条件的情况下长时间锁定配对设备或配对设备超出范围。
虽然您的代码可能感觉像是黑客攻击,但它可能是完成您正在做的事情的最佳、最便携的方式。
您可以使用蓝牙堆栈 API 在连接之前尝试查看设备是否存在并处于活动状态,但堆栈 API 没有标准化,因此 Widcom 和 Microsoft API 在如何执行此操作方面有所不同,并且 Widcom 是专有的,并且昂贵的。 您最终会遇到混乱的情况,试图发现堆栈类型,动态加载适当的验证器类,让它调用堆栈并查找设备。 鉴于此,您的简单民意调查似乎更加干净,并且您不必为 Widcom SDK 花费几美元。
The issue is likely not with the serial port code, but with the underlying bluetooth stack. The port you're using is purely virtual, and it's unlikely that any of the handshaking is even implemented (as it would be largely meaningless). CTS/RTS DTR/DSR are simply non-applicable for what you're working on.
The underlying issue is that when you create the virtual port, underneath it has to bind to the bluetooth stack and connect to the paired serial device. The port itself has no idea how long that might take and it's probably set up to do this asynchronously (though it would be purely up to the device OEM how that's done) to prevent the caller from locking up for a long period if there is no paired device or the paired device is out of range.
While your code may feel like a hack, it's probably the best, most portable way to do what you're doing.
You could use a bluetooth stack API to try to see if the device is there and alive before connecting, but there is no standardization of stack APIs, so the Widcom and Microsoft APIs differ on how you'd do that, and Widcom is proprietary and expensive. What you'd end up with is a mess of trying to discover the stack type, dynamically loading an appropriate verifier class, having it call the stack and look for the device. In light of that, your simple poll seems much cleaner, and you don't have to shell out a few $k for the Widcom SDK.