编写数据捕获程序,该程序使用POS中的COM4端口从POS(Ruby SuperSystem II)捕获发送给TM-U950收据打印机的数据?

发布于 2025-02-05 14:10:36 字数 2480 浏览 3 评论 0原文

因此,我正在研究这个项目,该项目将使用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 技术交流群。

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

发布评论

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

评论(1

天冷不及心凉 2025-02-12 14:10:37

这可能是因为它使用读取方法读取数据。

大多数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?

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