usingSystem;usingSystem.Collections.Generic;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Net;usingSystem.Net.Sockets;namespacePOSPrinter
{///
///POSPrinter的摘要说明。///此类处理网络打印,使用了IP端口.///
public classNetPOSPrinter
{string ipPort = "127.0.0.1";publicNetPOSPrinter()
{
}public NetPOSPrinter(stringIpPort)
{this.ipPort = IpPort;//打印机端口
}///
///输出文字到打印机///
/// 要打印的内容
public void PrintLine(stringstr)
{//建立连接
IPAddress ipa =IPAddress.Parse(ipPort);
IPEndPoint ipe= new IPEndPoint(ipa, 9100);//9100为小票打印机指定端口
Socket soc = newSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
soc.Connect(ipe);//string str= "hello,123456789,大家好! ";
byte[] b = System.Text.Encoding.GetEncoding("GB2312").GetBytes(str);
soc.Send(b);
soc.Close();
}public voidPrintPic(Bitmap bmp)
{//把ip和端口转化为IPEndPoint实例
IPEndPoint ip_endpoint = new IPEndPoint(IPAddress.Parse(ipPort), 9100);//创建一个Socket
Socket socket = newSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//连接到服务器
socket.Connect(ip_endpoint);//应对同步Connect超时过长的办法,猜测应该是先用异步方式建立以个连接然后,//确认连接是否可用,然后报错或者关闭后,重新建立一个同步连接//socket.SendTimeout = 1000;//初始化打印机,并打印
Byte[] byte_send= Encoding.GetEncoding("gb18030").GetBytes("\x1b\x40");//发送测试信息
socket.Send(byte_send, byte_send.Length, 0);byte[] data = new byte[] { 0x1B, 0x33, 0x00};
socket.Send(data, data.Length,0);
data[0] = (byte)'\x00';
data[1] = (byte)'\x00';
data[2] = (byte)'\x00'; //Clear to Zero.
Color pixelColor;//ESC * m nL nH 点阵图
byte[] escBmp = new byte[] { 0x1B, 0x2A, 0x00, 0x00, 0x00};
escBmp[2] = (byte)'\x21';//nL, nH
escBmp[3] = (byte)(bmp.Width % 256);
escBmp[4] = (byte)(bmp.Width / 256);//data
for (int i = 0; i < (bmp.Height / 24) + 1; i++)
{
socket.Send(escBmp, escBmp.Length,0);for (int j = 0; j < bmp.Width; j++)
{for (int k = 0; k < 24; k++)
{if (((i * 24) + k) < bmp.Height) //if within the BMP size
{
pixelColor= bmp.GetPixel(j, (i * 24) +k);if (pixelColor.R == 0)
{
data[k/ 8] += (byte)(128 >> (k % 8));
}
}
}
socket.Send(data,3, 0);
data[0] = (byte)'\x00';
data[1] = (byte)'\x00';
data[2] = (byte)'\x00'; //Clear to Zero.
}
byte_send= Encoding.GetEncoding("gb18030").GetBytes("\n");//发送测试信息
socket.Send(byte_send, byte_send.Length, 0);
}//data
byte_send= Encoding.GetEncoding("gb18030").GetBytes("\n");//发送测试信息
socket.Send(byte_send, byte_send.Length, 0);
socket.Close();
}///
///打开钱箱///
public voidOpenCashBox()
{
IPAddress ipa=IPAddress.Parse(ipPort);
IPEndPoint ipe= new IPEndPoint(ipa, 9100);//9100为小票打印机指定端口
Socket soc = newSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
soc.Connect(ipe);char[] c = { Convert.ToChar(27), 'p', Convert.ToChar(0), Convert.ToChar(60), Convert.ToChar(255) };byte[] b = System.Text.Encoding.GetEncoding("GB2312").GetBytes(c);
soc.Send(b);
soc.Close();
}
}
}
本文档介绍如何使用C#通过Socket网络链接进行热敏打印机的图片打印。示例代码展示了如何建立连接,发送文字和图片数据到指定IP端口,以及打开钱箱的操作。
3027

被折叠的 条评论
为什么被折叠?



