0

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;
            }
George Miranda
  • 150
  • 1
  • 13

1 Answers1

0

It would be better if you install the printer with attached USB as "Generic / Text only". You can send the ZPL-code to this printer without having serial communication.

You can also Check this questions: Send data to thermal printer

Community
  • 1
  • 1
etalon11
  • 895
  • 2
  • 13
  • 36
  • The printer was already using Generic/Text Only. I altered my code to not use serialport and now I can get it to print only when I step through code and view variables after I write the string to the printer. Running it normally just passes through the method. – George Miranda Dec 30 '15 at 00:52