I want to send control codes for Epson printer with c# application, I wrote some code, I have the connection with the printer, but when I am sending codes it not responding. On the other side, I found Java application for sending control codes, from that app some of the codes is working but not all of them. For example I want to use ESC EM 66 control code but when I am sending this code, printer not responding or just print the code in numbers. Can anyone to help me to find solution for this problem? I read many articles but without help.
The code who I use is:
When I am using the method GetDocument() printer is not responding, but it prints only text when I call the buffer variable:
The code who I use is:
Code:
class Program
{
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);
static void Main(string[] args)
{
IntPtr ptr = CreateFile("LPT1", GENERIC_WRITE, 0,
IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
if (ptr.ToInt32() == -1)
{
Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
}
else
{
string command = "test";
FileStream lpt = new FileStream(ptr, FileAccess.ReadWrite);
Byte[] buffer = new Byte[2048];
buffer = System.Text.Encoding.ASCII.GetBytes(command);
// lpt.Write(buffer, 0, buffer.Length);
lpt.Write(GetDocument(), 0, GetDocument().Length);
lpt.Close();
}
}
private static byte[] GetDocument()
{
using (var ms = new MemoryStream())
using (var bw = new BinaryWriter(ms))
{
// bw.Write((char)0x1B);//esc
// bw.Write('@');
// bw.Write((char)0x0C);//ff
bw.Write('a');
bw.Write((byte)66);
bw.Write((byte)3);
bw.Flush();
return ms.ToArray();
}
}
}
Code:
lpt.Write(buffer, 0, buffer.Length);
Comment