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.

1

There are 1 best solutions below

15
kunif On

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?