0
Answer

How to print over LPT or COM ports

Erhan Bahar

Erhan Bahar

17y
5.5k
1

I'am developing a sticker printing software. In my company we have printer with both COM and LPT port. So my software must support both.

For printing,
I create FileHandle to send data to printer. This method works both COM and LPT but I can't learn if printer finished its job.
with this methot printer gives overflow error when I try to print more than 50 sticker.

My code like this...


public const Int32 GENERIC_READ = -2147483648;
public const Int32 GENERIC_WRITE = 1073741824;
public const Int32 OPEN_EXISTING = 3;
public const Int32 FILE_ATTRIBUTE_NORMAL = 128;
public const Int32 NOPARITY = 0;
public const Int32 removed = 0;

[DllImport("kernel32", SetLastError = true)]
public static extern IntPtr CreateFile(string lpFileName, Int32 dwDesiredAccess, Int32 dwShareMode,IntPtr lpSecurityAttributes, Int32 dwCreationDisposition,Int32 dwFlagsAndAttributes, IntPtr hTemplateFile);

string port = ""// LPT or COM
IntPtr hParallelPort = CreateFile(port, GENERIC_READ | ENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL, IntPtr.Zero);

FileStream stream = new FileStream(hParallelPort, FileAccess.ReadWrite);


with this I'am send data to printer over LPT or COM
stream.Write(file);