我在寫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;
}
}
}
這聽起來像編碼的文本編輯器採用的是與打印機使用的相同。您的編輯器是否指示正在使用哪種編碼? – Kurt 2010-06-24 08:17:47
@Kurt我在命令提示符中使用編輯器,因爲它在MS DOS中。藍色的一個沒有鼠標,我認爲我不知道它使用哪種編碼 – adopilot 2010-06-24 08:22:22
嘗試在記事本中打開該文件。做一個「另存爲」,看看它保存了什麼編碼?我猜測它會是ASCII碼,你的C#代碼試圖用Unicode編碼(UTF-8/16)。 – shahkalpesh 2010-06-24 08:36:22