My problem is that when I use my port.Write() nothing seems to be sent to my zebra printer. I know I am getting a good string output that is printable. The method will exit completely with a true value. I think the issue is somewhere in the port communication. I am using a USB-serial adapter. Is this causing any issues? Thanks in advance for any help.
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;
}
}
Update: I changed my code to this after a little more research and information. Now it only prints when I step through. Not when I run normally.
[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;
}