C# 如何强制串行端口写入方法在发送数据之前等待线路清除?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/222455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 18:45:22  来源:igfitidea点击:

How do I force a serial port write method to wait for the line to clear before sending its data?

c#windows-mobilebluetoothserial-portzebra-printers

提问by Jason Down

Here's some background on what I'm trying to do:

以下是我正在尝试做的一些背景:

  1. Open a serial port from a mobile device to a Bluetooth printer.
  2. Send an EPL/2 form to the Bluetooth printer, so that it understands how to treat the data it is about to receive.
  3. Once the form has been received, send some data to the printer which will be printed on label stock.
  4. Repeat step 3 as many times as necessary for each label to be printed.
  1. 打开从移动设备到蓝牙打印机的串行端口。
  2. 向蓝牙打印机发送 EPL/2 表格,以便它了解如何处理即将接收的数据。
  3. 收到表格后,将一些数据发送到打印机,这些数据将打印在标签纸上。
  4. 对于要打印的每个标签,根据需要多次重复步骤 3。

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.

第 2 步只发生在第一次,因为表单不需要在每个标签之前。我的问题是,当我发送表格时,如果我发送标签数据太快,它将无法打印。有时我会在标签上打印“蓝牙故障:无线电无法运行”而不是我发送的数据。

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.

所以基本上,我可以捕获 TimeoutException 并在等待一定时间后重试 write 方法(三秒似乎一直在工作,但更少,似乎每次尝试都会抛出异常)。经过三次尝试,我只是假设串行端口有问题并让用户知道。

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.

这种方式似乎工作正常,但我相信有更好的方法来处理这个问题。SerialPort 类中有一些我认为我需要使用的属性,但我真的找不到任何关于如何使用它们的好的文档或示例。我试过玩弄一些属性,但它们似乎都没有做我想要实现的目标。

Here's a list of the properties I have played with:

这是我玩过的属性列表:

  • CDHolding
  • CtsHolding
  • DsrHolding
  • DtrEnable
  • Handshake
  • RtsEnable
  • CDHolding
  • 中通控股
  • DsrHolding
  • Dtr启用
  • 握手
  • 启用 Rts

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.

我正在使用 C#(2.0 框架)、Zebra QL 220+ 蓝牙打印机和 Windows Mobile 6 手持设备,如果这对解决方案有任何影响的话。

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.

我还应该注意到移动设备使用的是蓝牙 2.0,而打印机只有 1.1 版。我假设速度差异是导致打印机接收数据滞后的原因。

采纳答案by Adam Davis

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.

查看 Zebra 规范,看看它们是否实现,或者您是否可以打开软件流控制(xon、xoff),这将允许您查看各种缓冲区何时变满。

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.

此外,蓝牙无线电的最大传输速度不太可能超过 250k。您可能会考虑人为地将其限制为 9,600bps - 这将为无线电提供大量喘息空间以进行重传、纠错、检测和自身的流量控制。

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.

如果所有其他方法都失败了,那么您现在使用的 hack 还不错,但我会致电 Zebra 技术支持并在放弃之前了解他们的建议。

-Adam

-亚当

回答by ctacke

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.

问题可能不在于串行端口代码,而在于底层蓝牙堆栈。您使用的端口是纯虚拟的,甚至不太可能实现任何握手(因为它在很大程度上毫无意义)。CTS/RTS DTR/DSR 根本不适用于您正在处理的内容。

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.

潜在的问题是,当您创建虚拟端口时,其下方必须绑定到蓝牙堆栈并连接到配对的串行设备。端口本身不知道这可能需要多长时间,并且它可能设置为异步执行此操作(尽管这完全取决于设备 OEM 是如何完成的)以防止调用者长时间锁定(如果没有)配对设备或配对设备超出范围。

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.

您可以使用蓝牙堆栈 API 来尝试在连接之前查看设备是否存在并且处于活动状态,但是堆栈 API 没有标准化,因此 Widcom 和 Microsoft API 在您如何做到这一点上有所不同,而 Widcom 是专有的,并且昂贵的。您最终会尝试发现堆栈类型,动态加载适当的验证程序类,让它调用堆栈并查找设备。有鉴于此,您的简单民意调查似乎更清晰,您不必为 Widcom SDK 支付几美元。

回答by Jason Down

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:

好吧,我根据已经给出的两个建议找到了一种方法。我需要使用以下内容设置我的串行端口对象:

serialPort.Handshake = Handshake.RequestToSendXOnXOff;
serialPort.WriteTimeout = 10000; // Could use a lower value here.

Then I just need to do the write call:

然后我只需要进行 write 调用:

serialPort.Write(labelData);

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.

由于 Zebra 打印机支持软件流控制,因此当缓冲区接近满时,它会向移动设备发送一个 XOff 值。这会导致移动设备等待从打印机发送的 XOn 值,从而有效地通知移动设备它可以继续传输。

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.

通过设置写超时属性,我给出了在抛出写超时异常之前允许传输的总时间。您仍然希望捕获写入超时,正如我在问题中的示例代码中所做的那样。但是,没有必要循环 3 次(或任意次数),每次都尝试写入,因为软件流控制将启动和停止串行端口写入传输。