3
votes

I am writing C# application that need to print data to POS STAR printer using RawPrinterHelper.

My printing works fine except when I sending characters like ŽĆČĐŠ. Then I get wrong data printed out.

Until now my research give me following results.

If I in PowerShell open good old edit and in txt file write my characters (ŽĆČĐŠ) and send that to printer I get print out as I wish

I can't that repeat using Notepad

How I can in C# encode my sting to looks like that one from command prompt (EIDTor). So when I send data to printer it print Desire fonts as it looks like in Windows environment.

I did also try to print using Star driver and their C# sample for sending data directly to printer but without success.

EDIT:


I did it, and for others Who in generally have troubles printing directly on Star printers using C# here is code for sample app from Star IO Programming Tool for using their driver.

using System;
using System.Text;
using StarMicronics.StarIO; // added as a reference from the "Dependencies" directory
                            // requires StarIOPort.dll, which is copied to the output directory by the Post-Build event

namespace TestEnkodera
{
    class Program
    {
        static void Main(string[] args)
        {
            string portName = "LPT1";
            string portSettings = string.Empty;
            string print = string.Empty;

            //Select code page  
            //Decimal  27  29  116  n
            print += string.Format("{0}{1}{2}{3}{4}", (char)27, (char)29, (char)116, (char)5, Environment.NewLine);

            print += "Đ Š Ž Ć Č ž ć č ć \n";            

            IPort port = null;
            port = StarMicronics.StarIO.Factory.I.GetPort(portName, portSettings, 10 * 1000);
            //byte[] command = ASCIIEncoding.ASCII.GetBytes(print); //This was orginal code provided by STAR

            Encoding ec = Encoding.GetEncoding(852); //Here is way to set CODEPAGE to match with printer CODE PAGE
            byte[] command = ec.GetBytes(print);
            uint totalSizeCommunicated = WritePortHelper(port, command);
            StarMicronics.StarIO.Factory.I.ReleasePort(port);
            Console.ReadKey();
        }
        private static uint WritePortHelper(IPort port, byte[] writeBuffer)
        {
            uint zeroProgressOccurances = 0;
            uint totalSizeCommunicated = 0;
            while ((totalSizeCommunicated < writeBuffer.Length) && (zeroProgressOccurances < 2)) // adjust zeroProgressOccurances as needed
            {
                uint sizeCommunicated = port.WritePort(writeBuffer, totalSizeCommunicated, (uint)writeBuffer.Length - totalSizeCommunicated);
                if (sizeCommunicated == 0)
                {
                    zeroProgressOccurances++;
                }
                else
                {
                    totalSizeCommunicated += sizeCommunicated;
                    zeroProgressOccurances = 0;
                }
            }
            return totalSizeCommunicated;
        }
    }
}
1
It sounds like the encoding the text editor uses is the same as the one the printer uses. Does your editor indicate which encoding is being used?Kurt
@Kurt I am using EDITOR in command prompt as it was in MS DOS. Blue one without mouse, I think that I can't figure out which encoding it useadopilot
Try opening that file in Notepad. Do a "Save As" on it to see what encoding is that saved in? I am guessing that it will be ASCII & your c# code is trying to write it in Unicode (UTF-8/16).shahkalpesh
thanks for the feedback and your code sample.Lucero
Getting this excpetion starIO native openport operation failed on opening port. Code is same. Any Ideas ?Ali Umair

1 Answers

4
votes

If you're taking of the DOS editor, you're dealing with a legacy encoding.

Have a look at this link: http://msdn.microsoft.com/en-us/library/cc488003.aspx

At some time you're converting characters to bytes to send them to the printer. You probably do that using an StreamWriter or so. Now you have to supply this "converter" the correct encoding, which in the end does convert the characters to the correct byte representation for the printer, so that the extended characters match.

A list of encodings (for DOS codepages) can be found here: http://msdn.microsoft.com/en-us/library/system.text.encoding.aspx - a common one is ibm850 (western european).

Edit: I found the following information online, maybe it helps (I actually think that code page 0 "Normal" is #850 Latin-1):

Star Micronics Character Code Tables Reference

Code Page | Description 
0         | Normal
1         | #437 USA, Std Europe
2         | Katakana
3         | #437 USA, Std Europe
4         | #858 Multilingual
5         | #852 Latin-2
6         | #860 Portuguese
7         | #861 Icelandic
8         | #863 Canadian French
9         | #865 Nordic
10        | #866 Cyrillic Russian
11        | #855 Cyrillic Bulgarian
12        | #857 Turkey
13        | #852 Israel
14        | #864 Arabic
15        | #737 Greek
16        | #851 Greek
17        | #869 Greek
18        | #929 Greek
19        | #772 Lithuanian
20        | #774 Lithuanian
21        | #874 Thai