Printing directly to a thermal printer using ESC/POS Commands executed in C# with an interface of TCP/IP

和自甴很熟 提交于 2019-11-30 07:29:35
Leonidas

Answering this question in case anyone else comes along with the same q. This is what worked for me:

Socket clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
clientSock.NoDelay = true;
IPAddress ip = IPAddress.Parse("192.168.0.18");
IPEndPoint remoteEP = new IPEndPoint(ip, 9100);
clientSock.Connect(remoteEP);
Encoding enc = Encoding.ASCII;

// Line feed hexadecimal values
byte[] bEsc = new byte[4];
bEsc[0] = 0x0A;
bEsc[1] = 0x0A;
bEsc[2] = 0x0A;
bEsc[3] = 0x0A;

// Send the bytes over 
clientSock.Send(bEsc);

// Sends an ESC/POS command to the printer to cut the paper
string output = Convert.ToChar(29) + "V" + Convert.ToChar(65) + Convert.ToChar(0);
char[] array = output.ToCharArray();
byte[] byData = enc.GetBytes(array);
clientSock.Send(byData);
clientSock.Close();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!