不装驱动,如何进行小票打印? 我想不装驱动,而直接通过API来打印;我之前已经实现了GDI+来实现了打印,因为通过串口打印,是要把数据先存入缓冲区的,所以我想应该如何把我的打印模板和程序动态生成的数据存入缓冲区,然后再进行打印,就是不知道该怎么实现这个?不知道我这样理解对不对,或者各位有什么更好的方法,都来说说。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 不装驱动直接往LTP里写数据只能用文件方式吧~先openfile之后write.这样还能有模板?也就控制文字在第几行了吧? 给你一个,以前写的类,这个类是以新北洋打印机为测试样机编写: /// <summary> /// 串口打印类 /// </summary> class CommControl { /// <summary> /// 打印位置 /// Left 居左打印 /// Center 居中打印 /// Right 居右打印 /// </summary> public enum HorPos { Left, Center, Right } private int ColWidth = 32; private System.IO.Ports.SerialPort serialPort; /// <summary> /// 构造函数 /// </summary> /// <param name="PortName">打印机所在的串口</param> public CommControl(string PortName) { try { serialPort = new System.IO.Ports.SerialPort(PortName); serialPort.BaudRate = 9600; serialPort.DataBits = 8; serialPort.StopBits = System.IO.Ports.StopBits.One; serialPort.Parity = System.IO.Ports.Parity.None; serialPort.Open(); SetNormalFont(); } catch { } } /// <summary> /// 析构函数,不调用此函数,不能释放串口 /// </summary> /// <returns>是否释放成功</returns> public bool Dispose() { bool Result = true; if (serialPort != null) { if (serialPort.IsOpen) { try { serialPort.Close(); serialPort.Dispose(); } catch { Result = false; } } } return Result; } /// <summary> /// 打印机所在串口是否打开 /// </summary> /// <returns>true 串口打开成功;false串口打开失败</returns> public bool IsOpen() { bool Result = false; if (serialPort != null && serialPort.IsOpen) Result = true; return Result; } /// <summary> /// 像打印机发送byte类型的数据 /// </summary> /// <param name="bdata">要打印的数据</param> /// <returns>打印是否成功</returns> public bool Write(byte[] bdata) { try { if (IsOpen()) { serialPort.Write(bdata, 0, bdata.Length); return true; } else { return false; } } catch { return false; } } /// <summary> /// 向打印机发送字符串类型的数据 /// </summary> /// <param name="Data">要打印的数据</param> /// <returns>打印是否成功</returns> public bool Write(string Data) { try { if (IsOpen()) { byte[] bData = System.Text.Encoding.Default.GetBytes(Data); Write(bData); return true; } else { return false; } } catch { return false; } } /// <summary> /// 发送数据到打印机,打印完成后,自动跳到下一行 /// </summary> /// <param name="Data">要打印的数据</param> /// <returns>是否打印成功</returns> public bool WriteLine(string Data) { bool Result = Write(Data); if (Result) Result = NewRow(); return Result; } /// <summary> /// 发送数据到打印机,打印完成后,自动跳到下一行,并可指定打印位置 /// </summary> /// <param name="Data">要打印的数据</param> /// <returns>是否打印成功</returns> public bool WriteLine(string Data, HorPos horpos) { int Length = Encoding.Default.GetBytes(Data).Length; if (Length > ColWidth || HorPos.Left == horpos) return WriteLine(Data); switch (horpos) { case HorPos.Center: Data = Data.PadLeft(Length + (ColWidth - Length) / 2 - (Length - Data.Length), ' '); break; case HorPos.Right: Data = Data.PadLeft(ColWidth - (Length - Data.Length), ' '); break; default: break; } return WriteLine(Data); } /// <summary> /// 打印一行==== /// </summary> /// <returns>是否打印成功</returns> public bool PrintLine() { return WriteLine("================================"); } /// <summary> /// 打印日期 格式:2009-01-01 01:01:01 /// </summary> /// <returns>是否打印成功</returns> public bool PrintDate() { return WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); } /// <summary> /// 打印头移动到下一行 /// </summary> /// <returns>是否移动成功</returns> public bool NewRow() { byte[] temp = new byte[] { 0x0A }; return Write(temp); } /// <summary> /// 打印头移动多行 /// </summary> /// <param name="iRow">要移动的行数</param> /// <returns>是否移动成功</returns> public bool NewRow(int iRow) { bool Result = false; for (int i = 0; i < iRow; i++) { Result = NewRow(); if (!Result) break; } return Result; } /// <summary> /// 切纸 /// </summary> /// <returns>切纸是否成功</returns> public bool CutPaper() { byte[] temp = new byte[] { 0x1D, 0x56, 0x00, 0x05 }; return Write(temp); } /// <summary> /// 设置打印机初始化状态 /// </summary> /// <returns></returns> public bool SetNormalFont() { if (!IsOpen()) return false; byte[] temp; try { //1D, 50 设置横向和纵向移动单位 temp = new byte[] { 0x1D, 0x50, 0xB4, 0xB4 }; serialPort.Write(temp, 0, temp.Length); //1B, 53 选择标准模式 temp = new byte[] { 0x1B, 0x53 }; serialPort.Write(temp, 0, temp.Length); //1B, 20 设置字符右间距 temp = new byte[] { 0x1B, 0x20, 0x00 }; serialPort.Write(temp, 0, temp.Length); //设置汉字字符左右间距 temp = new byte[] { 0x1C, 0x53, 0x00, 0x00 }; serialPort.Write(temp, 0, temp.Length); //1D 42 是否反选打印 01反选/00取消 temp = new byte[] { 0x1D, 0x42, 0x00 }; serialPort.Write(temp, 0, temp.Length); //1B 45 选择/取消加粗模式 01选择/00取消 temp = new byte[] { 0x1B, 0x45, 0x00 }; serialPort.Write(temp, 0, temp.Length); //1B 7B 选择/取消倒置打印模式 01选择/00取消 temp = new byte[] { 0x1B, 0x7B, 0x00 }; serialPort.Write(temp, 0, temp.Length); //1B 2D 设置/取消下划线 01设置/00取消 temp = new byte[] { 0x1B, 0x2D, 0x00 }; serialPort.Write(temp, 0, temp.Length); //1B 2D 设置/取消汉字下划线 01设置/00取消 temp = new byte[] { 0x1C, 0x2D, 0x00 }; serialPort.Write(temp, 0, temp.Length); //选择取消顺时针旋转90度 01选择 00取消 temp = new byte[] { 0x1B, 0x56, 0x00 }; serialPort.Write(temp, 0, temp.Length); //1B 45 选择/取消加粗模式 01选择/00取消 temp = new byte[] { 0x1B, 0x45, 0x00 }; serialPort.Write(temp, 0, temp.Length); //1B 45 设置绝对打印位置 temp = new byte[] { 0x1B, 0x24, 0x00, 0x00 }; serialPort.Write(temp, 0, temp.Length); //1B, 33 设置行高, 18个像素 temp = new byte[] { 0x1B, 0x33, 0x20 }; serialPort.Write(temp, 0, temp.Length); //1B 4D 选择字体 03为汉字字体 temp = new byte[] { 0x1B, 0x4D, 0x03 }; serialPort.Write(temp, 0, temp.Length); //1D 21 选择字体大小,默认 temp = new byte[] { 0x1D, 0x21, 0x00 }; serialPort.Write(temp, 0, temp.Length); return true; } catch { return false; } }类有点大,不让一次发完,分两次吧. 再来 /// <summary> /// 以大一倍的字体打印数据 /// </summary> /// <param name="Data">需要打印的数据</param> /// <returns>是否打印成功</returns> public bool WriteBig(string Data) { bool Result = false; Result = SetNormalFont(); if (!Result) return Result; try { byte[] temp; //1B, 33 设置行高, 54个像素 temp = new byte[] { 0x1B, 0x33, 0x48 }; serialPort.Write(temp, 0, temp.Length); //1B 4D 选择字体 03为汉字字体 temp = new byte[] { 0x1B, 0x4D, 0x03 }; serialPort.Write(temp, 0, temp.Length); //横向放大和纵向放大不可同时作用 //1D 21 选择字体大小,横向放大1倍 temp = new byte[] { 0x1D, 0x21, 0x10 }; serialPort.Write(temp, 0, temp.Length); //1D 21 选择字体大小,纵向放大1倍 //temp = new byte[] { 0x1D, 0x21, 0x01 }; //serialPort.Write(temp, 0, temp.Length); //1B 45 选择/取消加粗模式 01选择/00取消 temp = new byte[] { 0x1B, 0x45, 0x01 }; serialPort.Write(temp, 0, temp.Length); Write(Data); Result = true; } catch { Result = false; } Result = SetNormalFont(); return Result; } /// <summary> /// 以大一倍的字体打印数据,打印完成换行 /// </summary> /// <param name="Data">需要打印的数据</param> /// <returns>是否打印成功</returns> public bool WriteBigLine(string Data) { bool Result = false; Result = SetNormalFont(); if (!Result) return Result; try { byte[] temp; //1B, 33 设置行高, 54个像素 temp = new byte[] { 0x1B, 0x33, 0x48 }; serialPort.Write(temp, 0, temp.Length); //1B 4D 选择字体 03为汉字字体 temp = new byte[] { 0x1B, 0x4D, 0x03 }; serialPort.Write(temp, 0, temp.Length); //横向放大和纵向放大不可同时作用 //1D 21 选择字体大小,横向放大1倍 temp = new byte[] { 0x1D, 0x21, 0x10 }; serialPort.Write(temp, 0, temp.Length); //1D 21 选择字体大小,纵向放大1倍 //temp = new byte[] { 0x1D, 0x21, 0x01 }; //serialPort.Write(temp, 0, temp.Length); //1B 45 选择/取消加粗模式 01选择/00取消 temp = new byte[] { 0x1B, 0x45, 0x01 }; serialPort.Write(temp, 0, temp.Length); Write(Data); Result = true; } catch { Result = false; } Result = SetNormalFont(); if (Result) Result = NewRow(); return Result; } /// <summary> /// 设置带下划线的行 /// </summary> /// <returns>是否设置成功</returns> public bool SetUnderLine() { bool Result = false; //Result = SetNormalFont(); // if (!Result) return Result; try { byte[] temp; //1B 2D 设置/取消下划线 01设置/00取消 temp = new byte[] { 0x1B, 0x2D, 0x02 }; serialPort.Write(temp, 0, temp.Length); //1B 2D 设置/取消汉字下划线 01设置/00取消 temp = new byte[] { 0x1C, 0x2D, 0x02 }; serialPort.Write(temp, 0, temp.Length); Write(" "); Result = true; } catch { Result = false; } Result = SetNormalFont(); return Result; } }调用:CommControl commcon = new CommControl("COM7"); if (commcon.IsOpen()) { commcon.WriteBigLine(" 这是题目"); commcon.PrintLine(); commcon.WriteLine("姓名:东方不败"); commcon.SetUnderLine(); commcon.NewRow(); commcon.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), CommControl.HorPos.Right); commcon.NewRow(3); commcon.CutPaper(); } commcon.Dispose();完整的代码 mjp1234airen4385,谢谢你的帮助,现在我这里又碰到了一个问题,通过API可以实现打印了,但是为什么每次打印的时候,它开头部门就空了四行的空白区,而打印完成后,我必须走纸四行才能把打印的东西完整的打印出来,开头走了4行的空白部分,太浪费了点,不知道该怎么处理,在网上查了下,有的人说是pos打印机的问题。 就是Pos打印机驱动的默认设置。这属于正常现象。使用驱动,就只能这样了,除非有设置的地方。使用命令行打印就没有这样的问题了的,一切都是通过命令控制。 感谢 mjp1234airen4385我直接拿来改装了 vs2008下c#中如何开大数组 为什么用VS将一个字符串转化为整型时总是说我输入的格式有错误? 谁有c#开发的web应用程序? 窗体之间访问需要哪些技术 程序注册码制作 winform程序三层构架。datagridview修改空间数据更新到数据库,遇到问题 关于在另一个类中改变上一个类中窗口的属性值 windows 服务创建问题 再c#中遍历一个Form中的所有控件,在线等待?急!!! c#与java的连接 flex C# c/s 怎么做 用C#编写的 文件加密传输程序 求大家帮忙~!!!急急急急!
/// 串口打印类
/// </summary>
class CommControl
{
/// <summary>
/// 打印位置
/// Left 居左打印
/// Center 居中打印
/// Right 居右打印
/// </summary>
public enum HorPos { Left, Center, Right }
private int ColWidth = 32;
private System.IO.Ports.SerialPort serialPort; /// <summary>
/// 构造函数
/// </summary>
/// <param name="PortName">打印机所在的串口</param>
public CommControl(string PortName)
{
try
{
serialPort = new System.IO.Ports.SerialPort(PortName);
serialPort.BaudRate = 9600;
serialPort.DataBits = 8;
serialPort.StopBits = System.IO.Ports.StopBits.One;
serialPort.Parity = System.IO.Ports.Parity.None;
serialPort.Open();
SetNormalFont();
}
catch { }
} /// <summary>
/// 析构函数,不调用此函数,不能释放串口
/// </summary>
/// <returns>是否释放成功</returns>
public bool Dispose()
{
bool Result = true;
if (serialPort != null)
{
if (serialPort.IsOpen)
{
try
{
serialPort.Close();
serialPort.Dispose();
}
catch { Result = false; }
}
} return Result;
} /// <summary>
/// 打印机所在串口是否打开
/// </summary>
/// <returns>true 串口打开成功;false串口打开失败</returns>
public bool IsOpen()
{
bool Result = false;
if (serialPort != null && serialPort.IsOpen) Result = true;
return Result;
} /// <summary>
/// 像打印机发送byte类型的数据
/// </summary>
/// <param name="bdata">要打印的数据</param>
/// <returns>打印是否成功</returns>
public bool Write(byte[] bdata)
{
try
{
if (IsOpen())
{
serialPort.Write(bdata, 0, bdata.Length);
return true;
}
else
{
return false;
}
}
catch { return false; }
} /// <summary>
/// 向打印机发送字符串类型的数据
/// </summary>
/// <param name="Data">要打印的数据</param>
/// <returns>打印是否成功</returns>
public bool Write(string Data)
{
try
{
if (IsOpen())
{
byte[] bData = System.Text.Encoding.Default.GetBytes(Data);
Write(bData);
return true;
}
else
{
return false;
}
}
catch { return false; }
} /// <summary>
/// 发送数据到打印机,打印完成后,自动跳到下一行
/// </summary>
/// <param name="Data">要打印的数据</param>
/// <returns>是否打印成功</returns>
public bool WriteLine(string Data)
{
bool Result = Write(Data);
if (Result) Result = NewRow();
return Result;
} /// <summary>
/// 发送数据到打印机,打印完成后,自动跳到下一行,并可指定打印位置
/// </summary>
/// <param name="Data">要打印的数据</param>
/// <returns>是否打印成功</returns>
public bool WriteLine(string Data, HorPos horpos)
{
int Length = Encoding.Default.GetBytes(Data).Length;
if (Length > ColWidth || HorPos.Left == horpos) return WriteLine(Data);
switch (horpos)
{
case HorPos.Center:
Data = Data.PadLeft(Length + (ColWidth - Length) / 2 - (Length - Data.Length), ' ');
break;
case HorPos.Right:
Data = Data.PadLeft(ColWidth - (Length - Data.Length), ' ');
break;
default:
break;
}
return WriteLine(Data);
} /// <summary>
/// 打印一行====
/// </summary>
/// <returns>是否打印成功</returns>
public bool PrintLine()
{
return WriteLine("================================");
} /// <summary>
/// 打印日期 格式:2009-01-01 01:01:01
/// </summary>
/// <returns>是否打印成功</returns>
public bool PrintDate()
{
return WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
} /// <summary>
/// 打印头移动到下一行
/// </summary>
/// <returns>是否移动成功</returns>
public bool NewRow()
{
byte[] temp = new byte[] { 0x0A };
return Write(temp);
} /// <summary>
/// 打印头移动多行
/// </summary>
/// <param name="iRow">要移动的行数</param>
/// <returns>是否移动成功</returns>
public bool NewRow(int iRow)
{
bool Result = false;
for (int i = 0; i < iRow; i++)
{
Result = NewRow();
if (!Result) break;
}
return Result;
} /// <summary>
/// 切纸
/// </summary>
/// <returns>切纸是否成功</returns>
public bool CutPaper()
{
byte[] temp = new byte[] { 0x1D, 0x56, 0x00, 0x05 };
return Write(temp);
} /// <summary>
/// 设置打印机初始化状态
/// </summary>
/// <returns></returns>
public bool SetNormalFont()
{
if (!IsOpen()) return false;
byte[] temp; try
{
//1D, 50 设置横向和纵向移动单位
temp = new byte[] { 0x1D, 0x50, 0xB4, 0xB4 };
serialPort.Write(temp, 0, temp.Length);
//1B, 53 选择标准模式
temp = new byte[] { 0x1B, 0x53 };
serialPort.Write(temp, 0, temp.Length);
//1B, 20 设置字符右间距
temp = new byte[] { 0x1B, 0x20, 0x00 };
serialPort.Write(temp, 0, temp.Length);
//设置汉字字符左右间距
temp = new byte[] { 0x1C, 0x53, 0x00, 0x00 };
serialPort.Write(temp, 0, temp.Length);
//1D 42 是否反选打印 01反选/00取消
temp = new byte[] { 0x1D, 0x42, 0x00 };
serialPort.Write(temp, 0, temp.Length);
//1B 45 选择/取消加粗模式 01选择/00取消
temp = new byte[] { 0x1B, 0x45, 0x00 };
serialPort.Write(temp, 0, temp.Length);
//1B 7B 选择/取消倒置打印模式 01选择/00取消
temp = new byte[] { 0x1B, 0x7B, 0x00 };
serialPort.Write(temp, 0, temp.Length);
//1B 2D 设置/取消下划线 01设置/00取消
temp = new byte[] { 0x1B, 0x2D, 0x00 };
serialPort.Write(temp, 0, temp.Length);
//1B 2D 设置/取消汉字下划线 01设置/00取消
temp = new byte[] { 0x1C, 0x2D, 0x00 };
serialPort.Write(temp, 0, temp.Length);
//选择取消顺时针旋转90度 01选择 00取消
temp = new byte[] { 0x1B, 0x56, 0x00 };
serialPort.Write(temp, 0, temp.Length);
//1B 45 选择/取消加粗模式 01选择/00取消
temp = new byte[] { 0x1B, 0x45, 0x00 };
serialPort.Write(temp, 0, temp.Length);
//1B 45 设置绝对打印位置
temp = new byte[] { 0x1B, 0x24, 0x00, 0x00 };
serialPort.Write(temp, 0, temp.Length); //1B, 33 设置行高, 18个像素
temp = new byte[] { 0x1B, 0x33, 0x20 };
serialPort.Write(temp, 0, temp.Length);
//1B 4D 选择字体 03为汉字字体
temp = new byte[] { 0x1B, 0x4D, 0x03 };
serialPort.Write(temp, 0, temp.Length);
//1D 21 选择字体大小,默认
temp = new byte[] { 0x1D, 0x21, 0x00 };
serialPort.Write(temp, 0, temp.Length);
return true;
}
catch { return false; }
}类有点大,不让一次发完,分两次吧.
/// <summary>
/// 以大一倍的字体打印数据
/// </summary>
/// <param name="Data">需要打印的数据</param>
/// <returns>是否打印成功</returns>
public bool WriteBig(string Data)
{
bool Result = false;
Result = SetNormalFont();
if (!Result) return Result;
try
{
byte[] temp;
//1B, 33 设置行高, 54个像素
temp = new byte[] { 0x1B, 0x33, 0x48 };
serialPort.Write(temp, 0, temp.Length);
//1B 4D 选择字体 03为汉字字体
temp = new byte[] { 0x1B, 0x4D, 0x03 };
serialPort.Write(temp, 0, temp.Length);
//横向放大和纵向放大不可同时作用
//1D 21 选择字体大小,横向放大1倍
temp = new byte[] { 0x1D, 0x21, 0x10 };
serialPort.Write(temp, 0, temp.Length);
//1D 21 选择字体大小,纵向放大1倍
//temp = new byte[] { 0x1D, 0x21, 0x01 };
//serialPort.Write(temp, 0, temp.Length); //1B 45 选择/取消加粗模式 01选择/00取消
temp = new byte[] { 0x1B, 0x45, 0x01 };
serialPort.Write(temp, 0, temp.Length);
Write(Data);
Result = true;
}
catch { Result = false; }
Result = SetNormalFont();
return Result;
} /// <summary>
/// 以大一倍的字体打印数据,打印完成换行
/// </summary>
/// <param name="Data">需要打印的数据</param>
/// <returns>是否打印成功</returns>
public bool WriteBigLine(string Data)
{
bool Result = false;
Result = SetNormalFont();
if (!Result) return Result;
try
{
byte[] temp;
//1B, 33 设置行高, 54个像素
temp = new byte[] { 0x1B, 0x33, 0x48 };
serialPort.Write(temp, 0, temp.Length);
//1B 4D 选择字体 03为汉字字体
temp = new byte[] { 0x1B, 0x4D, 0x03 };
serialPort.Write(temp, 0, temp.Length);
//横向放大和纵向放大不可同时作用
//1D 21 选择字体大小,横向放大1倍
temp = new byte[] { 0x1D, 0x21, 0x10 };
serialPort.Write(temp, 0, temp.Length);
//1D 21 选择字体大小,纵向放大1倍
//temp = new byte[] { 0x1D, 0x21, 0x01 };
//serialPort.Write(temp, 0, temp.Length);
//1B 45 选择/取消加粗模式 01选择/00取消
temp = new byte[] { 0x1B, 0x45, 0x01 };
serialPort.Write(temp, 0, temp.Length);
Write(Data);
Result = true;
}
catch { Result = false; }
Result = SetNormalFont();
if (Result) Result = NewRow();
return Result;
} /// <summary>
/// 设置带下划线的行
/// </summary>
/// <returns>是否设置成功</returns>
public bool SetUnderLine()
{
bool Result = false;
//Result = SetNormalFont();
// if (!Result) return Result;
try
{
byte[] temp;
//1B 2D 设置/取消下划线 01设置/00取消
temp = new byte[] { 0x1B, 0x2D, 0x02 };
serialPort.Write(temp, 0, temp.Length);
//1B 2D 设置/取消汉字下划线 01设置/00取消
temp = new byte[] { 0x1C, 0x2D, 0x02 };
serialPort.Write(temp, 0, temp.Length);
Write(" ");
Result = true;
}
catch { Result = false; }
Result = SetNormalFont();
return Result;
}
}
调用:
CommControl commcon = new CommControl("COM7");
if (commcon.IsOpen())
{
commcon.WriteBigLine(" 这是题目");
commcon.PrintLine();
commcon.WriteLine("姓名:东方不败");
commcon.SetUnderLine();
commcon.NewRow();
commcon.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), CommControl.HorPos.Right);
commcon.NewRow(3);
commcon.CutPaper();
}
commcon.Dispose();完整的代码
这属于正常现象。
使用驱动,就只能这样了,除非有设置的地方。
使用命令行打印就没有这样的问题了的,一切都是通过命令控制。
mjp1234airen4385
我直接拿来改装了