c# - Moneris 半集成解决方案不起作用
问题描述
在这一点上我很沮丧,我想我会把这个作为最后的手段。
我正在开发一个 C# .NET 4.5 应用程序,该应用程序将通过 USB 与 Moneris 支付设备进行通信。它是 Moneris ICT-250,Moneris 将其称为“半集成”应用程序。我一直在尝试发送测试付款以使设备使用串行端口类工作,但似乎没有任何效果。
对于初学者,Moneris 确实提供了一个模拟器来启动和运行。我可以确认我可以继续,设置测试付款 - 比如 100.00 美元 - 发送它......然后设备亮起。它还输出请求和响应的详细日志。
每个请求都必须是一个特定格式的字符串,用于标识付款类型、金额等……我已将在日志中找到的字符串发送出去,但似乎没有任何效果。设备不会记录失败或成功。
我知道设备连接正确。如果我更改端口号或拔下设备,我的 catch 将处理它(如下)。
下面是一个简单的控制台应用程序。我的代码有问题吗?有没有其他人在连接半集成 Moneris 解决方案方面有任何经验?我对任何想法持开放态度。Moneris 无法提供任何支持或代码片段。至少可以说非常令人沮丧...
感谢大家!代码如下:)
using System;
using System.IO.Ports;
class Moneris_Integration
{
public static void Main()
{
SerialPort port = new SerialPort("COM8");
// These properties are required by the device
port.BaudRate = 19200;
port.Parity = Parity.Even;
port.StopBits = StopBits.One;
port.DataBits = 8;
port.Open();
// This is the request that is sent by the simulator to the device
port.Write("<STX>02<FS>0011000<FS>0020<ETX><LRC>");
port.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
Console.WriteLine("===| Moneris Test |===");
Console.ReadKey();
}
private static void DataReceivedHandler(
object sender,
SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string incomingData = sp.ReadExisting();
Console.WriteLine("Response:");
Console.Write(incomingData);
}
}
解决方案
正如其他人在您的问题评论中所建议的那样,它肯定看起来像您正在写入端口的内容:
port.Write("<STX>02<FS>0011000<FS>0020<ETX><LRC>");
需要完全转换为 ASCII。
首先,定义ASCII 控制字符:
private byte[] STX = new byte[] { 0x02 };
private byte[] EXT = new byte[] { 0x03 };
private byte[] FS = new byte[] { 0x1C };
您还需要一个函数来计算 LRC,它基于消息的其余部分。我拿了这个:
public static byte calculateLRC(byte[] bytes)
{
byte LRC = 0;
for (int i = 0; i < bytes.Length; i++)
{
LRC ^= bytes[i];
}
return LRC;
}
然后使用 ASCII 编码将消息上的数字字符串转换为字节:
byte[] bytes1 = System.Text.Encoding.ASCII.GetBytes("02");
byte[] bytes2 = System.Text.Encoding.ASCII.GetBytes("0011000");
byte[] bytes3 = System.Text.Encoding.ASCII.GetBytes("0011000");
我们创建一个新的内存块来存储消息:
var message = new MemoryStream();
将我们要发送的字节以块的形式附加到我们的消息中:
message.Write(STX, 0 , 1);
message.Write(bytes1, 0, bytes1.Length);
message.Write(FS, 0 , 1);
message.Write(bytes2, 0, bytes2.Length);
message.Write(FS, 0 , 1);
message.Write(bytes3, 0, bytes3.Length);
message.Write(EXT, 0 , 1);
计算 LRC:
var LRC_msg = calculateLRC(message)
将其附加到消息中:
message.Write(LRC_msg, 0, LRC_msg.Length);
最后,将其写入端口:
port.Write(message, 0, message.Length);
您还应该考虑到您看到的日志可能会误导您使用消息的数字部分。如果您仍然没有得到答案,可能是时候查看端口上的真实数据了。为此,您可以打开Termite或RealTerm 之类的终端。我不确定你提到的模拟器是如何工作的,但我认为它是软件,它需要一个串行端口来连接以发送数据。如果是这种情况,您可以尝试在您的计算机上转发两个真实或虚拟串行端口,正如我在此处解释的那样。
还建议您可能需要使用 CR 或 LF 终止命令。
推荐阅读
- compilation - 构建可以在 AWS EC2 上运行的独立 Octave 可执行文件
- python - 批处理循环 - 运行 python 命令时出错
- javascript - Leaflet.JS,尝试扩展 Circle 类时出错
- mysql - 如何将触发器'if inserting'从oracle转换为mysql
- c - 在 C 中使用字符串添加长数字
- python - 使用 ax 对象将 matplotlib 图的 x 标签更改为特定单词
- javascript - 关于在本机反应中正确添加到多维数组中的选定用户的两部分问题
- javascript - 从消息字符串中删除提及代码
- openlayers - 如何在 Openlayers 6 中自动调整形状的字体?
- javascript - 是否有任何理由不在 JavaScript 中的 Event 对象上定义自定义属性?