c#使用pos58端口打印,打印数据不断重复前10行

compassion 2008-05-29 05:50:29
开发pos58打印出现数据不断重复前10行. 可以肯定的是pos机缓冲区没有刷新的问题了.不知道如何解决啊.求有经验的人解惑.
代码如下:
调用的方法如下:

POSPrinter pnt = new POSPrinter();
for (int i = 1; i <= 260; i++)
{
for (int j = 0; j < 4500; j++)
{
//此行为了等待数据打印出来,自动清空缓冲区后再打下一条 一个高人说的,也没有效果
}
pnt.PrintLine("第" + i + "行" + " 行号为:" + i);
}

使用的类如下:
#region 声明

using System;
using System.Runtime.InteropServices;
using System.IO;
using System.IO.Ports;
using System.Text;
using System.Threading;

#endregion

/// <summary>
/// POSPrinter 的摘要说明。
/// </summary>
public class POSPrinter
{
const int OPEN_EXISTING = 3;
string prnPort = "LPT1";
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr CreateFile(string lpFileName,
int dwDesiredAccess,
int dwShareMode,
int lpSecurityAttributes,
int dwCreationDisposition,
int dwFlagsAndAttributes,
int hTemplateFile);

public POSPrinter()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public POSPrinter(string prnPort)
{
this.prnPort = prnPort;//打印机端口
}
public string PrintLine(string str)
{
lock (this)//同步锁
{
IntPtr iHandle = CreateFile(prnPort, 0x40000000, 0, 0, OPEN_EXISTING, 0, 0);
if (iHandle.ToInt32() == -1)
{
return "没有连接打印机或者打印机端口不是LPT1";
}
else
{

FileStream fs = new FileStream(iHandle, FileAccess.ReadWrite);
StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Default); //写数据
//sw.WriteLine((((char)27).ToString()) + ((char)64).ToString()); //初始化打印机状态
sw.WriteLine(str);
sw.Close();
fs.Close();
return "";
}
}
}
...全文
245 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
compassion 2009-07-14
  • 打赏
  • 举报
回复
事实证明,打印机的问题。换了就好用了。
compassion 2008-06-01
  • 打赏
  • 举报
回复
有经验的人都哪里去了,嗷嗷郁闷中......
starlessnt 2008-05-30
  • 打赏
  • 举报
回复
纯顶..没玩过pos58
compassion 2008-05-30
  • 打赏
  • 举报
回复
就是pos机,T58票票通,最最普通的那种热敏的.

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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