开发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 "";
}
}
}
代码如下:
调用的方法如下: 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 "";
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货