编写数据捕获程序,该程序使用POS中的COM4端口从POS(Ruby SuperSystem II)捕获发送给TM-U950收据打印机的数据?
因此,我正在研究这个项目,该项目将使用RS232 USB到串行电缆捕获并存储从POS(Ruby SuperSystem II)发送到TM-U950收据打印机的打印机数据。 这个项目可行吗?
以下图片显示了高级图,解释了物理体系结构: hdl
,这是C#编写的代码,c#看起来像:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
using System.IO;
using System.Text;
namespace ConsoleAppListener
{
internal class Program
{
static void Main(string[] args)
{
SerialPort mySerialPort = new SerialPort("COM6");
Console.WriteLine("");
Console.WriteLine("******* Serial COM:6 is Working ******");
mySerialPort.BaudRate = 9600;
mySerialPort.Parity = Parity.None;
mySerialPort.StopBits = StopBits.One;
mySerialPort.DataBits = 8;
mySerialPort.Encoding = Encoding.GetEncoding(65001);
mySerialPort.Handshake = Handshake.XOnXOff;
//mySerialPort.DtrEnable = true;
mySerialPort.Open();
mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
if (mySerialPort.IsOpen)
{
Console.WriteLine(mySerialPort.PortName + ": Open");
//System.Threading.Thread.Sleep(1000);
}
else
{
throw new InvalidOperationException("Serial Port is already Open..");
}
//Console.WriteLine("Press any key to continue...");
Console.WriteLine();
Console.ReadKey();
mySerialPort.Close();
}
private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
if (indata != null)
{
Console.WriteLine("Listen from Ruby --> "+ indata);
callmyfunction(indata.ToString());
}
}
private static void callmyfunction(string data)
{
// Does something but printing for now!
StreamWriter File = new StreamWriter("Report.txt", true);
File.WriteLine(data);
File.Close();
}
}
: }
}程序的输出时 打印时打印时打印是打印的。数据如HDL图像所示: 程序输出
So I am working on this project that will capture and store the printer data sent from POS (Ruby SuperSystem II) to TM-U950 Receipt Printer using RS232 USB-To-Serial cable. Is this project feasible?
Here is picture showing high level diagram explaining the physical architecture:
HDL
And this is code written in C# looks like:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
using System.IO;
using System.Text;
namespace ConsoleAppListener
{
internal class Program
{
static void Main(string[] args)
{
SerialPort mySerialPort = new SerialPort("COM6");
Console.WriteLine("");
Console.WriteLine("******* Serial COM:6 is Working ******");
mySerialPort.BaudRate = 9600;
mySerialPort.Parity = Parity.None;
mySerialPort.StopBits = StopBits.One;
mySerialPort.DataBits = 8;
mySerialPort.Encoding = Encoding.GetEncoding(65001);
mySerialPort.Handshake = Handshake.XOnXOff;
//mySerialPort.DtrEnable = true;
mySerialPort.Open();
mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
if (mySerialPort.IsOpen)
{
Console.WriteLine(mySerialPort.PortName + ": Open");
//System.Threading.Thread.Sleep(1000);
}
else
{
throw new InvalidOperationException("Serial Port is already Open..");
}
//Console.WriteLine("Press any key to continue...");
Console.WriteLine();
Console.ReadKey();
mySerialPort.Close();
}
private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
if (indata != null)
{
Console.WriteLine("Listen from Ruby --> "+ indata);
callmyfunction(indata.ToString());
}
}
private static void callmyfunction(string data)
{
// Does something but printing for now!
StreamWriter File = new StreamWriter("Report.txt", true);
File.WriteLine(data);
File.Close();
}
}
}
Output of the program while printing is printing data as shown in HDL image:
Program Output
Concern: The program prints data in "?" and "`" characters rather than plain text as printer is able to print as shown in Program Output Image.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能是因为它使用读取方法读取数据。
大多数ESC/POS命令数据是无法打印的二进制数据。
另外,可打印字符是相应的代码页中的字符,而不是C#中的默认UTF。
例如,建议使用 bytestoread property 和 bitconverter 等
c#序列数据丢失?
如何等待收集的字节附加下一个序列端口数据?
That's probably because it uses ReadExisting method to read the data.
Most of the ESC/POS command data are binary data that cannot be printed.
Also, the printable characters are the characters in the corresponding code page, not the default UTF in C#.
For example, it is recommended to read it as byte array data using the BytesToRead property and Read method as described in these articles, convert it to a hexadecimal character string with BitConverter, etc., and display it.
C# Serial Data Loss?
How to wait for next serial port data to be append with the collected byte?