tags:

views:

94

answers:

1

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;
        }
    }
}
+2  A: 

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
Lucero
Thanks on all that helpful informatin. I am talking aout MsDos editor and I my code page is 852 should I set that on printer or in my code.
adopilot
@adopilot, they just need to match. Therefore, if you set it to "character code table 5" on the printer, you have to use the encoding named "ibm852" on the .NET side and the character should then match correctly.
Lucero