我正在制作一个程序,使用C#通过套接字与Epson TM-T88IV打印机进行ESCP/POS命令通信。当我发送带有关闭盖子和纸张充足的DLE EOT n命令时,它会立即以字节数组返回正确的代码。然而,当我打开盖子或取出纸张尝试模拟错误时,没有DLE EOT命令(1、2、3或4)会返回任何内容。Socket.receive方法一直处于挂起状态直到超时。我能够向这台打印机打印并进行换行、切割等操作。
以下是我的代码:
以下是我的代码:
public class clsPrinter
{
TcpListener server;
MemoryStream ms = new MemoryStream();
BinaryWriter bw;
Socket clientSock = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
string printerIP;
Encoding enc = Encoding.ASCII;
public clsPrinter(string printerIP)
{
this.printerIP = printerIP;
bw = new BinaryWriter(ms);
clientSock.NoDelay = true;
clientSock = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
IPAddress ip = IPAddress.Parse(printerIP);
IPEndPoint remoteEP = new IPEndPoint(ip, 9100);
clientSock.Connect(remoteEP);
}
public void getPrinterStatus()
{
try
{
byte[] byData = new byte[] { 16, 4, 1 }; // DLE EOT 1
sendToPrinter(byData);
byte[] bytes = new byte[1024];
int bytesReceived = clientSock.Receive(bytes);
}
catch (Exception ex)
{
}
}
public void sendToPrinter(byte[] byteStream)
{
clientSock.Send(byteStream);
}
有什么想法吗?