POS机精确走纸
2010-01-22 17:46 刘少侠 阅读(460) 评论(0) 编辑 收藏 举报代码
private IntPtr iHandle;
private FileStream fs;
private StreamWriter sw;
private string prnPort = "LPT1"; //打印机端口
public ClsPrintLPT( )
{
}
private const uint GENERIC_READ = 0x80000000;
private const uint GENERIC_WRITE = 0x40000000;
private const int OPEN_EXISTING = 3;
/// <summary>
/// 打开一个vxd(设备)
/// </summary>
[DllImport( "kernel32.dll", EntryPoint = "CreateFile", CharSet = CharSet.Auto )]
private static extern IntPtr CreateFile( string lpFileName, uint dwDesiredAccess, int dwShareMode, int lpSecurityAttributes,
int dwCreationDisposition, int dwFlagsAndAttributes, int hTemplateFile );
/// <summary>
/// 开始连接打印机
/// </summary>
private bool PrintOpen( )
{
iHandle = CreateFile( prnPort, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0 );
if ( iHandle.ToInt32( ) == -1 )
{
MessageBox.Show( "没有连接打印机或者打印机端口不是LPT1!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information );
return false;
}
else
{
fs = new FileStream( iHandle, FileAccess.ReadWrite );
sw = new StreamWriter( fs, System.Text.Encoding.Default ); //写数据
return true;
}
}
/// <summary>
/// 打印字符串
/// </summary>
/// <param name="str">要打印的字符串</param>
private void PrintLine( string str )
{
sw.WriteLine( str );
;
}
/// <summary>
/// 关闭打印连接
/// </summary>
private void PrintEnd( )
{
sw.Close( );
fs.Close( );
}
/// <summary>
/// 打印票据
/// </summary>
/// <param name="ds">tb_Temp 全部字段数据集合</param>
/// <returns>true:打印成功 false:打印失败</returns>
public bool PrintDataSet( string[ ] dsPrint )
{
try
{
if ( PrintOpen( ) )
{
PrintLine( " " );
PrintLine( "[家乐福郑州旗舰店超市]" );
PrintLine( "Name : " + dsPrint[ 0 ] );
PrintLine( "Age : " + dsPrint[ 1 ] );
PrintLine( "Gender : " + dsPrint[ 2 ] );
PrintLine( "Beauty : " + dsPrint[ 3 ] );
PrintLine( "Married: " + dsPrint[ 4 ] );
PrintLine( "-------------------------------------------" );
}
PrintEnd( );
return true;
}
catch
{
return false;
}
}
/// <summary>
/// ESC/P 指令
/// </summary>
/// <param name="iSelect">0:退纸命令 1:进纸命令 2:换行命令</param>
public void PrintESC( int iSelect )
{
string send;
iHandle = CreateFile( prnPort, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0 );
if ( iHandle.ToInt32( ) == -1 )
{
MessageBox.Show( "没有连接打印机或者打印机端口不是LPT1!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information );
}
else
{
fs = new FileStream( iHandle, FileAccess.ReadWrite );
}
byte[ ] buf = new byte[ 80 ];
switch ( iSelect )
{
case 0:
//send = "" + ( char )( 27 ) + ( char )( 64 ) + ( char )( 27 ) + 'j' + ( char )( 255 ); //退纸1 255 为半张纸长
//send = send + ( char )( 27 ) + 'j' + ( char )( 125 ); //退纸2
send = "" + ( char )( 27 ) + ( char )( 64 ) + ( char )( 27 ) + 'j' + ( char )( 110 ); //退纸1 255 为半张纸长
send = send + ( char )( 27 ) + 'j' + ( char )( 125 ); //退纸2
break;
case 1:
send = "" + ( char )( 27 ) + ( char )( 64 ) + ( char )( 27 ) + 'J' + ( char )( 255 ); //进纸
break;
case 2:
send = "" + ( char )( 27 ) + ( char )( 64 ) + ( char )( 12 ); //换行
break;
default:
send = "" + ( char )( 27 ) + ( char )( 64 ) + ( char )( 12 ); //换行
break;
}
for ( int i = 0; i < send.Length; i++ )
{
buf[ i ] = ( byte )send[ i ];
}
fs.Write( buf, 0, buf.Length );
fs.Close( );
}
}
private FileStream fs;
private StreamWriter sw;
private string prnPort = "LPT1"; //打印机端口
public ClsPrintLPT( )
{
}
private const uint GENERIC_READ = 0x80000000;
private const uint GENERIC_WRITE = 0x40000000;
private const int OPEN_EXISTING = 3;
/// <summary>
/// 打开一个vxd(设备)
/// </summary>
[DllImport( "kernel32.dll", EntryPoint = "CreateFile", CharSet = CharSet.Auto )]
private static extern IntPtr CreateFile( string lpFileName, uint dwDesiredAccess, int dwShareMode, int lpSecurityAttributes,
int dwCreationDisposition, int dwFlagsAndAttributes, int hTemplateFile );
/// <summary>
/// 开始连接打印机
/// </summary>
private bool PrintOpen( )
{
iHandle = CreateFile( prnPort, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0 );
if ( iHandle.ToInt32( ) == -1 )
{
MessageBox.Show( "没有连接打印机或者打印机端口不是LPT1!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information );
return false;
}
else
{
fs = new FileStream( iHandle, FileAccess.ReadWrite );
sw = new StreamWriter( fs, System.Text.Encoding.Default ); //写数据
return true;
}
}
/// <summary>
/// 打印字符串
/// </summary>
/// <param name="str">要打印的字符串</param>
private void PrintLine( string str )
{
sw.WriteLine( str );
;
}
/// <summary>
/// 关闭打印连接
/// </summary>
private void PrintEnd( )
{
sw.Close( );
fs.Close( );
}
/// <summary>
/// 打印票据
/// </summary>
/// <param name="ds">tb_Temp 全部字段数据集合</param>
/// <returns>true:打印成功 false:打印失败</returns>
public bool PrintDataSet( string[ ] dsPrint )
{
try
{
if ( PrintOpen( ) )
{
PrintLine( " " );
PrintLine( "[家乐福郑州旗舰店超市]" );
PrintLine( "Name : " + dsPrint[ 0 ] );
PrintLine( "Age : " + dsPrint[ 1 ] );
PrintLine( "Gender : " + dsPrint[ 2 ] );
PrintLine( "Beauty : " + dsPrint[ 3 ] );
PrintLine( "Married: " + dsPrint[ 4 ] );
PrintLine( "-------------------------------------------" );
}
PrintEnd( );
return true;
}
catch
{
return false;
}
}
/// <summary>
/// ESC/P 指令
/// </summary>
/// <param name="iSelect">0:退纸命令 1:进纸命令 2:换行命令</param>
public void PrintESC( int iSelect )
{
string send;
iHandle = CreateFile( prnPort, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0 );
if ( iHandle.ToInt32( ) == -1 )
{
MessageBox.Show( "没有连接打印机或者打印机端口不是LPT1!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information );
}
else
{
fs = new FileStream( iHandle, FileAccess.ReadWrite );
}
byte[ ] buf = new byte[ 80 ];
switch ( iSelect )
{
case 0:
//send = "" + ( char )( 27 ) + ( char )( 64 ) + ( char )( 27 ) + 'j' + ( char )( 255 ); //退纸1 255 为半张纸长
//send = send + ( char )( 27 ) + 'j' + ( char )( 125 ); //退纸2
send = "" + ( char )( 27 ) + ( char )( 64 ) + ( char )( 27 ) + 'j' + ( char )( 110 ); //退纸1 255 为半张纸长
send = send + ( char )( 27 ) + 'j' + ( char )( 125 ); //退纸2
break;
case 1:
send = "" + ( char )( 27 ) + ( char )( 64 ) + ( char )( 27 ) + 'J' + ( char )( 255 ); //进纸
break;
case 2:
send = "" + ( char )( 27 ) + ( char )( 64 ) + ( char )( 12 ); //换行
break;
default:
send = "" + ( char )( 27 ) + ( char )( 64 ) + ( char )( 12 ); //换行
break;
}
for ( int i = 0; i < send.Length; i++ )
{
buf[ i ] = ( byte )send[ i ];
}
fs.Write( buf, 0, buf.Length );
fs.Close( );
}
}
然后再打印时,调用进纸或者退职命令即可!
PrintDocument.Print();
Instance.PrintESC(0);