C# SerialPort - 模拟 pos 键盘

发布于 2024-08-02 08:51:32 字数 1690 浏览 4 评论 0原文

我们正在尝试模拟 POS 键盘,以便将应用程序与现有销售点应用程序集成。

我们遇到了这个软件: 虚拟串行端口套件

它基本上创建了一个虚拟串口对,以便发送到 COM1 的数据可以从 COM4 输出,反之亦然。这允许我们的应用程序通过 COM4 发送数据,让 POS 应用程序看到它正在与 COM1 上的键盘进行通信。

非常巧妙,但似乎存在某种我们无法使用 .Net System.IO.Ports.SerialPort 类复制的信号......

从串行端口监控程序中我们可以看出,这就是如何启动序列有效:

  1. 8 字节命令发送到键盘
  2. 键盘蜂鸣声
  3. 从键盘发送某种信号
  4. 第二个 8 字节命令发送到键盘,由信号触发
  5. 键盘回复设备和版本信息

使用虚拟串行端口时,我们无法弄清楚如何复制从键盘发送的信号。我们可以看到所有数据都正确通过,因此我们相信 SerialPort 对象上的设置是正确的。以下是我们的串行端口设置的片段:

_port.BaudRate= 9600;
_port.Parity = Parity.None;
_port.DataBits = 8;
_port.StopBits = StopBits.One;
_port.DtrEnable = true;
_port.RtsEnable = true;

我们还注意到使用 portmon 我们看到 GET_MODEM_STATUS 请求,这是 POS 应用程序在发送第二个命令之前正在等待的请求。

关于如何诊断这个问题有什么想法吗?由于我们使用的是 .NET,整个情况比我们习惯的要低一些。

更新:我还想指出,我们在这里尝试了 SDK:Franson Serial Tools 但我们甚至无法获得使用此SDK时要经过的数据。

更新:我们已经放弃使用任何类型的虚拟串行端口。我们已经获得了一条从 POS PC 到另一台 PC 的电缆,并且可以看到模拟键盘的数据。现在我们的问题是,我们无法弄清楚如何发出信号表明键盘已准备好接收数据,正如最上面的答案提到的那样。 POS 应用程序似乎发送了发出蜂鸣声的命令,并等待最多 3 秒等待信号。因此,与我们的应用程序对话时会超时,但与真实键盘对话时不会超时,

我们如何使用 SerialPort 类来做到这一点?我们已经将 DtrEnable 和 RtsEnable 设置为 true,还需要设置其他内容吗?或者我们是否必须使用较低级别的串行端口 p/invoke 来完成此任务?

解决方案:

_port.RtsEnabled = false;
Thread.Sleep(1000);
_port.RtsEnabled = true;

这使得 POS 应用程序认为键盘已插入,这是有道理的。我将把#1 答案标记为答案,因为它极大帮助我们找到了解决方案。

We are trying to emulate a POS keyboard in order to integrate an application with an existing Point of Sale application.

We ran across this software: Virtual Serial Port Kit

It basically creates a virtual serial port pair so that data send to COM1 can come out of COM4 and vice versa. This allows our application to send data through COM4 to appear to the POS application that it is talking to a keyboard on COM1.

Pretty ingenious, but it seems there is some kind of signalling going on that we are not able to replicate with the .Net System.IO.Ports.SerialPort class...

From what we can tell from serial port monitoring programs, this is how the startup sequence works:

  1. 8 byte Command sent to keyboard
  2. Keyboard beeps
  3. Some kind of signal is sent from the keyboard
  4. Second 8 byte command is sent to keyboard, triggered by the signal
  5. Keyboard replies with device and version information

When using our virtual serial port, we cannot figure out how to replicate the signal sent from the keyboard. We can see all data coming through properly, so we believe the settings on our SerialPort object are correct. Here is a snippet of our SerialPort settings:

_port.BaudRate= 9600;
_port.Parity = Parity.None;
_port.DataBits = 8;
_port.StopBits = StopBits.One;
_port.DtrEnable = true;
_port.RtsEnable = true;

We also noticed from using portmon we see a GET_MODEM_STATUS request that is what the POS application is waiting on before sending the second command.

Any ideas on how to diagnose this? Since we are using .NET this whole situation is a little more low level than we're used to.

UPDATE: I also want to note that we tried the SDK here: Franson Serial Tools but we could not even get data to go through when using this SDK.

UPDATE: We have thrown out using any kind of virtual serial port. We have gotten a cable to run from the POS PC to another and can see data coming across to emulate the keyboard. Now our problem is that we can't figure out how to signal that the keyboard is ready to recieve data as the top answer mentions. It appears that the POS application sends the command to beep an waits up to 3 seconds waiting for a signal. So it times out when talking to our application, but not when talking to the real keyboard

How can we do this with the SerialPort class? We already set DtrEnable and RtsEnable to true, do we need to set something else? Or do we have to use a lower level serial port p/invoke to accomplish this?

SOLUTION:

_port.RtsEnabled = false;
Thread.Sleep(1000);
_port.RtsEnabled = true;

This makes the POS application think the keyboard is plugged in, which makes sense. I'll mark the #1 answer as the answer since it greatly helped us find the solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

戈亓 2024-08-09 08:51:32

已编辑从模拟键盘的角度提供更多视角。

碰巧我很久以前就为 92R 键盘编写了低级驱动程序。

您确实需要专有协议的文档才能正确执行此操作 - 例如发送到键盘的命令包含序列号和校验和。我建议联系富士通并尝试获取此文档。

根据您的描述:

  • 您发送的第一个 8 字节命令可能是重置命令(因为它导致键盘发出蜂鸣声)。键盘发送响应以确认该命令,然后自行重置。

  • 发送重置命令后,POS应用需要等待键盘重置(我认为大约3000ms)才能发送其他命令。

  • 看起来第二次发送是请求固件版本的命令。

  • POS 应用程序还需要随后发送一条命令以在键盘实际发送击键之前启用“自动输入”。

    POS

  • 还有一些命令可用于请求键锁位置、使音频发生器发声、启用/禁用 MSR 以及写入可选的嵌入式 2 行显示屏。因此,您的模拟器需要能够重现对这些命令的响应。

  • 一旦 POS 应用程序启用“自动输入”,键盘将通过按下按键(或键锁位置更改或 MSR 输入)发送未经请求的消息。 IIRC 这些消息还有一个序列号和校验和,您需要在模拟器中重现它们。

我所知道的唯一信号是键盘在准备好接收数据时会升高 CTS。如果您连接 PC 上的两个端口,则需要一根特殊的零调制解调器电缆(见下文),以便当您的模拟器在 COM4 上启动 RTS 时,它将在另一个端口上被视为 CTS。

TeamPOS 主板上的 COM 端口为键盘提供电源。您可能不想将这些引脚连接到 COM4 端口,因此我建议使用仅连接以下引脚的零调制解调器电缆:

2(Tx 数据)- 3(Rx 数据)

3(Rx 数据)- 2(发送数据)

7 (RTS) - 8 (CTS)

8 (CTS) - 7 (RTS)

EDITED to give more perspective from the point of view of simulating the keyboard.

As it happens I have written low-level drivers for the 92R keyboard in the distant past.

You really need the documentation for the proprietary protocol to do this properly - for example commands sent to the keyboard contain a sequence number and a checksum. I'd recommend contacting Fujitsu and attempting to get hold of this documentation.

From what you've described:

  • The first 8-byte command you sent was probably a reset command (as it caused the keyboard to beep). The keyboard sends a response to acknowledge the command, then resets itself.

  • After sending a reset command, the POS app needs to wait for the keyboard to reset (I think about 3000ms) before sending other commands.

  • It looks like the second send is a command to request the firmware version.

  • The POS app will also need to subsequently send a command to enable "autoinput" before the keyboard will actually send keystrokes.

  • There are also commands available to request the keylock position, sound the tone generator, enable/disable the MSR, and write to the optional embedded 2-line display. So your simulator will need to be capable of reproducing the responses to these commands.

  • Once the POS app has enabled "autoinput" the keyboard will send unsolicited messages with the keystrokes being pressed (or keylock position changes, or MSR input). IIRC these messages also have a sequence number and checksum that you will need to reproduce in your simulator.

The only signalling I know of is that the keyboard raises CTS when it is ready to receive data. If you connect two ports on a PC, you need a special null modem cable (see below) so that when your simulator raises RTS on COM4 it will be seen as CTS on the other port.

The COM ports on a TeamPOS motherboard provide power to the keyboard. You probably don't want to connect these pins to your COM4 port, so I would suggest using a null modem cable that connects only the following pins:

2 (Tx data) - 3 (Rx data)

3 (Rx data) - 2 (Tx data)

7 (RTS) - 8 (CTS)

8 (CTS) - 7 (RTS)

抚你发端 2024-08-09 08:51:32

我已经很多年没有进行串口开发了,但是当我这样做时,我总是使用 交叉电缆 和第二台运行 Windows 超级终端的 PC。

I've not done serial port development for years, but when I did I always used a Crossover Cable and a second PC running Windows HyperTerminal.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文