1

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 Answers1

0

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?

kunif
  • 4,060
  • 2
  • 10
  • 30
  • Hi Kunif thank you for input - you mean implementation of a C# program alone cannot print plain text from POS unit? Do you think adding hardware like MicroController such as Raspberry Pi could help solve the problem? – Jaskaran Chahal Jun 10 '22 at 05:07
  • The mistake is to use a method that treats the data as a string from the beginning. At first, it is necessary to read by the method treated as a byte array, cut out only the printable character string part, and display it while converting it with appropriate encoding/decoding. – kunif Jun 10 '22 at 05:18
  • Kunif - can we connect like over the week end - either Saturday or Sunday which ever day works for you? I can share Zoom meeting link. – Jaskaran Chahal Jun 10 '22 at 21:07
  • Unfortunately, I don't have the language ability to speak English in real time. Instead, in the early stages, I would like to introduce my library for ESC/POS command decoding. [kunif/EscPosUtils](https://github.com/kunif/EscPosUtils) This also does not analyze stream data in real time, but processes a set of print request data stored in a file. I don't know if it will be helpful because the source code is not conscious of software engineering knowledge. – kunif Jun 10 '22 at 23:24
  • I see no problem. I will go over your github repo thanks for sharing! – Jaskaran Chahal Jun 12 '22 at 22:10
  • I tried BytesToRead method and convert to hex string but only prints B0 while listening to POS and if printer is printing something program outputs 60 only? – Jaskaran Chahal Jun 21 '22 at 18:29
  • I'm sorry, I don't understand what kind of data you are talking about in what situation. Was the question about how to capture the command that the application sent to the printer and parse it? It looks like you're asking about a byte of data that the printer tells your application. If it's a new issue that's different from the first question, write a new article. – kunif Jun 21 '22 at 22:41
  • My apologies for any misunderstanding. The scope of problem is still reading POS data that has been sent to receipt printer and parse it. My program currently outputs garbage hex data i.e b'\xff\xff\xff\xff\xff\xff\xff\xff' ---> �������� in normal text. – Jaskaran Chahal Jun 22 '22 at 16:19
  • Therefore, I need to have parser or interpreter that can translate the serial data from POS into human readable alphanumeric characters ['a-zA-z-0-9_*/:'] – Jaskaran Chahal Jun 22 '22 at 16:26
  • For example, why not use the [Send Data Tool](https://download.epson-biz.com/modules/pos/index.php?page=single_soft&cid=5027&scat=47&pcat=52) distributed by EPSON and their usage examples [Issuing Receipts](https://reference.epson-biz.com/modules/ref_escpos/index.php?content_id=269), [Select Cut Mode and Cut Paper](https://reference.epson-biz.com/modules/ref_escpos/index.php?content_id=273) to find out what kind of data is sent instead of the program you created? – kunif Jun 22 '22 at 23:18
  • I can try that. But all the magic of encoding and decoding happens to be inside the sendat.exe – Jaskaran Chahal Jun 24 '22 at 18:10
  • Probably so. In order to handle the data that is visible as a string in the script file, it seems that it needs to be kept within the ASCII range. If the default code page of Windows OS matches the code page you want to use on the printer, you may be able to use it as it is, but it is better to describe the characters 0x80-0xFF as hexadecimal values or escape characters. – kunif Jun 24 '22 at 21:09
  • But the problem is how to read and parse incoming serial data from POS to similar script file. – Jaskaran Chahal Jun 24 '22 at 23:16
  • Is that the problem with the HDL you are making? Then, the essential object of the question is how to realize HDL, not the content of the print request program to the printer that you wrote in the question. The essence of the problem you want to solve may be off-topic on this site. – kunif Jun 25 '22 at 00:14
  • Hi Kunif - The problem was in hardware design. I changed the hardware design its working now. Thanks! – Jaskaran Chahal Jul 13 '22 at 15:59