none
Text line order is changed when printed through parallel printer RRS feed

  • Question

  • I made parallel printing module like following.

    public int PrintText(string parallelport, string receiptText, Int16 nLen, Encoding encoding)
            {
                IntPtr ptr = CreateFile(parallelport, GENERIC_WRITE, 0,
                         IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
    
                /* Is bad handle? INVALID_HANDLE_VALUE */
                if (ptr.ToInt32() == -1)
                {
                    /* ask the framework to marshall the win32 error code to an exception */
                    //                Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
                    return (-1);
                }
    
                FileStream lpt = new FileStream(ptr, FileAccess.ReadWrite);
                Byte[] buffer = new Byte[2048];
                
                //Check to see if your printer support ASCII encoding or Unicode.
                //If unicode is supported, use the following:
                if (encoding == Encoding.Unicode)
                {
                    buffer = System.Text.Encoding.Unicode.GetBytes(receiptText);
                }
                else if (encoding == Encoding.ASCII)
                {
                    buffer = System.Text.Encoding.ASCII.GetBytes(receiptText);
                }
                lpt.Write(buffer, 0, buffer.Length);
                lpt.Close();
                return (0);
            }
     

    This module works fine when save as a file.

    However the line order is changed when print to a parallel printer.

    For example, if following string is passed to the parallelport, 

    AAAA
    BBBBBBBB
    CCCCCCC
    DDD

    Then the output result is always like following.

    BBBBBBBB
    CCCCCCC
    DDDAAAA

    Can anybody give me some advice?





    • Edited by Jeff0803 Tuesday, April 9, 2019 1:24 PM
    Monday, April 8, 2019 7:13 PM

Answers

  • Hi

    Thanks for the feedback.

    I could not test your code because of the lack of a parallel port. However, I think the following could be helpful for you.

    https://stackoverflow.com/questions/4692937/send-print-commands-directly-to-lpt-parallel-port-using-c-net

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Jeff0803 Thursday, April 11, 2019 1:20 PM
    Wednesday, April 10, 2019 8:29 AM
    Moderator
  • Are you doing any parallel (meaning "multithreaded") processing here? It's certainly possible to see results like this when mixing write from different threads

    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    • Marked as answer by Jeff0803 Tuesday, April 16, 2019 1:36 PM
    Wednesday, April 10, 2019 9:26 PM

All replies

  • Hi

    Thank you for posting here.

    >> CreateFile(parallelport, GENERIC_WRITE, 0,IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);

    For your question, could you provide the related code about CreateFile method?

    We are waiting for your update.

    Best regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, April 9, 2019 7:20 AM
    Moderator
  • The CreateFile() is Win32 API.

    Here is the declaration part.

    #region Declaration - Enumerate Parallel Ports
            //PortType enum
            //struct for PORT_INFO_2
            [StructLayout(LayoutKind.Sequential)]
            public struct PORT_INFO_2
            {
                public string pPortName;
                public string pMonitorName;
                public string pDescription;
                public PortType fPortType;
                internal int Reserved;
            }
    
    
            [Flags]
            public enum PortType : int
            {
                write = 0x1,
                read = 0x2,
                redirected = 0x4,
                net_attached = 0x8
            }
    
            //Win32 API
            [DllImport("winspool.drv", EntryPoint = "EnumPortsA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            public static extern int EnumPorts(string pName, int Level, IntPtr lpbPorts, int cbBuf, ref int pcbNeeded, ref int pcReturned);
    #endregion
    
    #region Declaration - Parallel Port Open/Send/Close
            public const short FILE_ATTRIBUTE_NORMAL = 0x80;
            public const short INVALID_HANDLE_VALUE = -1;
            public const uint GENERIC_READ = 0x80000000;
            public const uint GENERIC_WRITE = 0x40000000;
            public const uint CREATE_NEW = 1;
            public const uint CREATE_ALWAYS = 2;
            public const uint OPEN_EXISTING = 3;
    
            [DllImport("kernel32.dll", SetLastError = true)]
            static extern IntPtr CreateFile(string lpFileName, uint dwDesiredAccess,
                uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition,
                uint dwFlagsAndAttributes, IntPtr hTemplateFile);
    #endregion

    And here is calling part.

    ParallelOutput po = new ParallelOutput();
    po.PrintText("LPT1", strPrintText, Convert.ToInt16(strPrintText.Length), Encoding.ASCII);





    • Edited by Jeff0803 Tuesday, April 9, 2019 1:23 PM
    Tuesday, April 9, 2019 1:16 PM
  • Hi

    Thanks for the feedback.

    I could not test your code because of the lack of a parallel port. However, I think the following could be helpful for you.

    https://stackoverflow.com/questions/4692937/send-print-commands-directly-to-lpt-parallel-port-using-c-net

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Jeff0803 Thursday, April 11, 2019 1:20 PM
    Wednesday, April 10, 2019 8:29 AM
    Moderator
  • Are you doing any parallel (meaning "multithreaded") processing here? It's certainly possible to see results like this when mixing write from different threads

    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    • Marked as answer by Jeff0803 Tuesday, April 16, 2019 1:36 PM
    Wednesday, April 10, 2019 9:26 PM
  • No, it's only single processing.

    Just open parallel port, send text and close port.

    When I write text to the file, everything is OK, but that symptom occur only in parallel port.


    • Edited by Jeff0803 Wednesday, April 17, 2019 12:49 AM
    Tuesday, April 16, 2019 1:36 PM