2010-06-24 90 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