0
我的問題是,當我用我的port.Write()似乎沒有發送到我的斑馬打印機。我知道我獲得了可打印的良好字符串輸出。該方法將以真實值完全退出。我認爲這個問題是在港口交流中的某個地方。我正在使用USB串行適配器。這是否導致任何問題?預先感謝您的幫助。C#SerialPort.Write()不打印斑馬打印機
public bool LabelPrinting(DataTable dt, string LblPath)
{
if (File.Exists(LblPath))
{
string strContent = "";
string comport = cboPort.Text.Trim();
StreamReader myFile = new StreamReader(LblPath);
strContent = myFile.ReadToEnd();
myFile.Close();
if (strContent.Length == 0)
{
return false;
}
else
{
SerialPort port = new SerialPort(comport,9600, Parity.None, 8, StopBits.One);
port.Dispose();
if (!(port.IsOpen == true))
{
port.Open();
}
else
{
port.Close();
}
port.Write(strContent);
port.Write(new byte[] { 0x0A, 0xE2, 0xFF }, 0, 3);
port.Close();
return true;
}
}
更新:我更改我的代碼後,多一點研究和信息。現在只有當我經過時纔會打印。不是當我正常運行。
[DllImport("kernel32.dll", SetLastError = true)]
static extern SafeFileHandle CreateFile(string lpFileName, FileAccess dwDesiredAccess,
uint dwShareMode, IntPtr lpSecurityAttributes, FileMode dwCreationDisposition,
uint dwFlagsAndAttributes, IntPtr hTemplateFile);`
[...]
public bool LabelPrinting(DataTable dt, string LblPath)
{
if (File.Exists(LblPath))
{
string strContent = "";
string comport = cboPort.Text.Trim();
StreamReader myFile = new StreamReader(LblPath);
strContent = myFile.ReadToEnd();
myFile.Close();
if (strContent.Length == 0)
{
return false;
}
else
{
char[] BigSpaceChars = { Convert.ToChar(0x09) };
strContent = strContent.Replace(new string(BigSpaceChars), " ");
strContent = strContent.Replace("<CPN>", dt.Rows[0]["CPN"].ToString().ToUpper());
strContent = strContent.Replace("<RACKASSET>", dt.Rows[0]["RACKASSET"].ToString().ToUpper());
strContent = strContent.Replace("<LOC>", dt.Rows[0]["LOC"].ToString().ToUpper());
Byte[] buffer = new byte[strContent.Length];
buffer = System.Text.Encoding.ASCII.GetBytes(strContent);
SafeFileHandle printer = CreateFile(comport,FileAccess.ReadWrite,0,IntPtr.Zero,FileMode.Open,0,IntPtr.Zero);
if (printer.IsInvalid == true)
{
return false;
}
FileStream com = new FileStream(printer, FileAccess.ReadWrite);
com.Write(buffer, 0, buffer.Length);
com.Close();
return true;
}
打印機已經使用純文本/通用。我將代碼更改爲不使用串口,現在只有在將字符串寫入打印機後,我才能遍歷代碼和查看變量,才能打印它。通常運行它只是通過該方法。 –
你可以發佈你的更新代碼? – etalon11