2010-06-24 87 views
3

我在写C#应用程序,需要使用RawPrinterHelper将数据打印到POS STAR打印机。如何在C#中编辑类似编辑的字符串?

我的打印效果很好,除非我发送像ŽĆČĐŠ这样的字符。 然后我得到打印出来的错误数据。

直到现在我的研究给了我以下结果。

如果我在PowerShell中开好老编辑和TXT文件写我的文字(ŽĆČĐŠ) 并发送至打印机,我得到打印出来,因为我想

我不能使用记事本

是重复

我如何在C#中编写我的sting从命令提示符(EIDTor)看起来像那样。因此,当我发送数据到打印机时,它打印Desire字体,就像它在Windows环境中看起来一样。

我也尝试使用Star驱动程序及其C#示例将数据直接发送到打印机,但没有成功。

编辑:


我做到了,和其他人在一般的麻烦,直接使用C# 星打印机进行打印这里是示例应用程序代码Star IO Programming Tool使用他们的司机。

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; 
     } 
    } 
} 
+0

这听起来像编码的文本编辑器采用的是与打印机使用的相同。您的编辑器是否指示正在使用哪种编码? – Kurt 2010-06-24 08:17:47

+0

@Kurt我在命令提示符中使用编辑器,因为它在MS DOS中。蓝色的一个没有鼠标,我认为我不知道它使用哪种编码 – adopilot 2010-06-24 08:22:22

+0

尝试在记事本中打开该文件。做一个“另存为”,看看它保存了什么编码?我猜测它会是ASCII码,你的C#代码试图用Unicode编码(UTF-8/16)。 – shahkalpesh 2010-06-24 08:36:22

回答

4

如果您正在使用DOS编辑器,您正在处理遗留编码。

看一看这个链接:http://msdn.microsoft.com/en-us/library/cc488003.aspx

在一段时间你转换字符字节将它们发送到打印机。你可能使用StreamWriter左右。现在,您必须为此“转换器”提供正确的编码,最终将字符转换为打印机的正确字节表示形式,以便扩展字符匹配。

可在此处找到编码列表(适用于DOS代码页):http://msdn.microsoft.com/en-us/library/system.text.encoding.aspx - 常见的编码是ibm850(西欧)。

编辑:我发现下面的信息网上,也许它可以帮助(我倒认为代码页0“正常”是#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 
+0

感谢所有有用的信息。我正在讨论MsDos编辑器,我的代码页是852,应该在打印机还是我的代码中设置。 – adopilot 2010-06-24 09:02:46

+0

@adopilot,他们只需要匹配。因此,如果在打印机上将其设置为“字符代码表5”,则必须在.NET端使用名为“ibm852”的编码,然后字符应该正确匹配。 – Lucero 2010-06-24 09:14:22