2014年5月6日 星期二

C# Epson熱敏印表機列印 Pos機用

參考引用來源
--
 public class EpsopnPrinter
{
//列印文字(埠號 字元)
public static string PrintString(int Port, string val)
{
System.IO.Ports.SerialPort sp = new System.IO.Ports.SerialPort();
sp.PortName = "COM" + Port.ToString();
try
{
sp.Open();
}
catch
{
return "埠被佔用";
}
List data = new List();
string[] lines = val.Split('\n');
for (int i = 0; i < lines.Length; i++)
{
byte[] content = System.Text.Encoding.Default.GetBytes(lines[i].Replace("\r", ""));
byte[] wapbt ={ 0x0a };
data.AddRange(content);
data.AddRange(wapbt);
}
byte[] cutbt={0x1d,0x56,0x42,0x11};
data.AddRange(cutbt);
byte[] databt = data.ToArray();
sp.Write(databt, 0, databt.Length);
sp.Close();
return null;
}

//測試印表機是否接在這個埠
public static bool PrintTest(int Port)
{
System.IO.Ports.SerialPort sp = new System.IO.Ports.SerialPort();
sp.PortName = "COM" + Port.ToString();
sp.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(sp_DataReceived);
try
{
sp.Open();
}
catch
{
return false;
}
Recived = false;
byte[] testbt={0x1D,0x49,0x01};
sp.Write(testbt, 0, testbt.Length);
System.Threading.Thread.Sleep(100);
sp.Close();
return Recived;
}

static bool Recived = false;
static void sp_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
Recived = true;
}
}

沒有留言:

張貼留言