private void button2_Click(object sender, EventArgs e)
{
Int32 port = 9100;
string bigFont = ((char)27).ToString() + ((char)33).ToString() + ((char)213).ToString();//放大字体
TcpClient client = new TcpClient();
try
{
client.Connect("192.168.1.200", port);
NetworkStream stream = client.GetStream();
Byte[] data = null;
data = System.Text.Encoding.Default.GetBytes(bigFont);
stream.Write(data, 0, data.Length);
data = System.Text.Encoding.Default.GetBytes(bigFont);
stream.Write(data, 0, data.Length);
data = System.Text.Encoding.Default.GetBytes("DLE EOT n");
stream.Write(data, 0, data.Length);
data = System.Text.Encoding.Default.GetBytes("开始打印机测试\n");
stream.Write(data, 0, data.Length);
data = System.Text.Encoding.Default.GetBytes("打印机测试\n");
stream.Write(data, 0, data.Length);
data = System.Text.Encoding.Default.GetBytes("打印机测试\n");
stream.Write(data, 0, data.Length);
data = System.Text.Encoding.Default.GetBytes("打印机测试\n");
stream.Write(data, 0, data.Length);
data = System.Text.Encoding.Default.GetBytes("- -\n");
stream.Write(data, 0, data.Length);
data = System.Text.Encoding.Default.GetBytes("- -\n");
stream.Write(data, 0, data.Length);
data = System.Text.Encoding.Default.GetBytes("29");
stream.Write(data, 0, data.Length);
string cutPaper = ((char)27).ToString() + ((char)64).ToString() + ((char)29).ToString() + ((char)86).ToString() + ((char)66).ToString();//切纸
data = System.Text.Encoding.Default.GetBytes(cutPaper);
stream.Write(data, 0, data.Length);
stream.Close();
}
catch (Exception ex)
{
throw ex;
//如果打印机没开或者异常,在此处增加处理代码,我通常是把异常数据放到一个DataTable中,加个Timer来判断打印机正常就把
//数据发给打印机并从表中移除.
//打印机缺纸或者纸匣打开时,不会出现异常,不用特殊代码判断,数据不会丢失.
}
finally
{
client.Close();
}
}