打印机---并口打印机设置并口问题

我在深圳搬砖-Justin 2010-03-27 08:13:01
这一个星期被打印机深深的折磨着
关于发送数据到并口操作EPSON TM-U220 PB打印机
网上找的一段代码,

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public class DOCINFOA
{
[MarshalAs(UnmanagedType.LPStr)]
public string pDocName;
[MarshalAs(UnmanagedType.LPStr)]
public string pOutputFile;
[MarshalAs(UnmanagedType.LPStr)]
public string pDataType;
}
[DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd);

[DllImport("winspool.Drv", EntryPoint = "ClosePrinter", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool ClosePrinter(IntPtr hPrinter);

[DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool StartDocPrinter(IntPtr hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);

[DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool EndDocPrinter(IntPtr hPrinter);

[DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool StartPagePrinter(IntPtr hPrinter);

[DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool EndPagePrinter(IntPtr hPrinter);

[DllImport("winspool.Drv ", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
public static extern bool WritePrinter(IntPtr hPrinter, byte[] pBytes, int dwCount, ref int dwWritten);

public static bool SendBytesToPrinter(string szPrinterName, byte[] pBytes, Int32 dwCount)
{//最主要的方法,发送数据到并口的
Int32 dwError = 0, dwWritten = 0;
IntPtr hPrinter = new IntPtr(0);
DOCINFOA di = new DOCINFOA();
bool bSuccess = false;
di.pDocName = " C#.NET并口打印";
di.pDataType = "RAW";

if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero))
{
if (StartDocPrinter(hPrinter, 1, di))
{
if (StartPagePrinter(hPrinter))
{
bSuccess = WritePrinter(hPrinter, pBytes, dwCount, ref dwWritten);
EndPagePrinter(hPrinter);
}
EndDocPrinter(hPrinter);
}
ClosePrinter(hPrinter);
}
if (bSuccess == false)
{
dwError = Marshal.GetLastWin32Error();
}
return bSuccess;

}

public static void SendStringToPrinter(string szPrinterName, string szString)
{
Int32 dwCount;
byte[] f = Encoding.GetEncoding("Gb18030").GetBytes(szString);

dwCount = f.Length;
SendBytesToPrinter(szPrinterName, f, dwCount);
}


一个很郁闷的问题 打印机不是默认的时候 就无法打印,
这个时候应该是LPT端口的问题,但是不知道怎么设置
大家有做过的可以帮忙指点一下.
源程序上传到了:http://jijunwu.download.csdn.net/user/jijunwu/all/1这里
...全文
704 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
并口打印机啊...
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 wowmboy 的回复:]
MARK~~~~~~~~~
[/Quote]沉的找不到了
小生我怕怕 2010-03-27
  • 打赏
  • 举报
回复
MARK~~~~~~~~~
  • 打赏
  • 举报
回复
相当杯具,沉的找不到了
susan_wenjuan 2010-03-27
  • 打赏
  • 举报
回复
看不明白,好复杂啊!
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 bancxc 的回复:]

sf.
[/Quote]
哥哥 别只顾着sf啊 能解决不?
打印控制命令列表非常的有用,有详细的解释啊。 序号 命 令 说 明 01 HT 横向跳格 02 LF 打印并走纸一行 03 CR 打印并回车 04 ESC SO 设置所有字符倍宽打印 05 ESC DC4 取消所有字符倍宽打印 06 ESC SP n 设置字符右间距 07 ESC ! n 设置字符打印模式 08 ESC $ nL nH 设置绝对打印位置 09 ESC % n 选择/取消用户自定义非中文字符集 10 ESC & 定义用户自定义非中文字符 11 ESC * 选择位图模式 12 ESC - n 选择/取消下划线模式 13 ESC 2 设置字符行间距为默认 14 ESC 3 n 设置行间距 15 ESC ? n 取消用户自定义字符 16 ESC @ 初始化打印机 17 ESC D 设置横向跳格位置 18 ESC E n 选择/取消加粗模式 19 ESC G n 选择/取消双重打印模式 20 ESC J n 打印并走纸 21 ESC R n 选择国际字符表 22 ESC \ nL nH 设置相对横向打印位置 23 ESC a n 选择字符对齐模式 24 ESC c 5 n 允许/禁止按键 25 ESC d n 打印并向前走纸n 行 26 ESC p m t1 t2 产生钱箱控制脉冲 27 ESC t n 选择字符代码表 28 ESC { n 选择/取消倒置打印模式 29 GS * x y d1…dk 定义下载位图 30 GS / m 打印下载位图 31 GS H n 选择HRI 字符的打印位置 32 GS L nL nH 设置左边距 33 GS W nL nH 设置打印区域宽度 34 GS h n 选择条码高度 35 ①GS k m d…dk NUL ②GS k m n d1…dn 打印条码 36 GS v 0 m 打印光栅位图 THERMAL RECEIPT PRINTER 18 37 GS w n 设置条码宽度 38 FS ! n 设置汉字字符模式 39 FS & 选择汉字模式 40 FS - n 选择/取消汉字下划线模式 41 FS . 取消汉字模式 42 FS 2 c1 c2 d1...dk 定义用户自定义汉字

13,346

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 .NET技术前瞻
社区管理员
  • .NET技术前瞻社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧