1.我是C#的斑马USB条码打印,程序运行后测试打印是没问题的。但进行一系列COM口的SerialPort通信操作后,再打印就卡死。这时如果重启打印机,就可以继续打印。请问这是什么原因,是线程卡死吗?怎样破啊。以下是打印的两个主要方法代码,谢谢。
2.
//运行这个方法就可以打印。
public static void PrintBarCode(string devicePath, int leftMarginAdd, int topMarginAdd, string barcode, string rosh, string productModel, string lineCode, string vendorId, string cusModel, string BOSerialNo, string size)
{
StringBuilder cmd = new StringBuilder();
cmd.Append("I8,A,001\r\n");
cmd.Append("Q200,016\r\n");
cmd.Append("q863\r\n");
cmd.Append("rN\r\n");
cmd.Append("S2\r\n");
cmd.Append("D7\r\n");
cmd.Append("ZT\r\n");
cmd.Append("JF\r\n");
cmd.Append("O,S\r\n");
cmd.Append("R" + (300 + leftMarginAdd).ToString() + "," + (20 + topMarginAdd).ToString() + "\r\n");
cmd.Append("f100\r\n");
cmd.Append("N\r\n");
cmd.Append("A35,1,0,1,1,1,N,\"Q.C.Pass\"\r\n");
cmd.Append("b32,16,D," + size + ",\"" + barcode + "\"\r\n");//二维码
cmd.Append("A125,1,0,1,1,1,N,\"" + rosh + "\"\r\n");
cmd.Append("A15,149,3,1,1,1,N,\"" + productModel + "\"\r\n");//产品型号(左边)
cmd.Append("A17,162,0,2,1,1,R,\"" + lineCode + "\"\r\n");//设备编码(左下角)
cmd.Append("P1\r\n");
Printer printer = new Printer();
printer.DevicePath = devicePath;
if (!printer.Open())
{
return;
}
bool tempBool = printer.Write(cmd.ToString()); //过了这里就可以打印。下面的Write方法
if (!printer.Close())
{
return;
}
//打印
public bool Write(string Mystring)
{
if (iHandle != -1)
{
int i;
OVERLAPPED x;
byte[] mybyte = System.Text.Encoding.Default.GetBytes(Mystring);
return WriteFile(iHandle, mybyte, mybyte.Length, out i, out x); //程序刚跑起来是可以打印,但进行一系统通信操作后,到这里就卡死。怎样破?
}
else
{
throw new Exception("端口未打开!");
}
}
//WriteFile方法
[DllImport("kernel32.dll")]
private static extern bool WriteFile(int hFile, byte[] lpBuffer, int nNumberOfBytesToWriter, out int lpNumberOfBytesWriten, out OVERLAPPED lpOverLapped);