做个打印的小例子,实现串口通信,打印、关闭串口、打印功能。
.CS文件
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using BTD080LIB;
namespace BT_D080
{
public partial class Form1 : Form
{
private IntPtr com;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
PrintImpact d = new PrintImpact();
com = d.OpenPrint("LPT1", 9600, 8, PrintImpact.Impact_COM_ONESTOPBIT, PrintImpact.Impact_COM_NOPARITY, PrintImpact.Impact_COM_RTS_CTS);
if (com.ToInt32() <= 0)
{
MessageBox.Show("打开失败!");
}
else
{
MessageBox.Show("打开成功!");
}
}
private void button2_Click(object sender, EventArgs e)
{
PrintImpact d = new PrintImpact();
int status = d.ClosePrint(com, PrintImpact.Impact_COM_STATUS);
if (status == PrintImpact.Impact_SUCCESS)
{
MessageBox.Show("关闭成功!");
}
else
{
MessageBox.Show("关闭失败!");
}
}
private void button3_Click(object sender, EventArgs e)
{
int status = PrintImpact.Impact_SUCCESS;
//定义打印操作对象
PrintImpact d = new PrintImpact();
//设置字符右间距
status = d.SetRightSpacing(com, PrintImpact.Impact_COM_STATUS, 0);
if (status == PrintImpact.Impact_FAIL)
{
MessageBox.Show("打印机设置字符右间距执行失败!");
return;
}
if (status == PrintImpact.Impact_ERROR_INVALID_HANDLE)
{
MessageBox.Show("打印机设置字符右间距端口或文件的句柄无效!");
return;
}
if (status == PrintImpact.Impact_ERROR_INVALID_PARAMETER)
{
MessageBox.Show("打印机设置字符右间距参数无效!");
return;
}
//设置字符行高
status = d.SetLineSpacing(com, PrintImpact.Impact_COM_STATUS, 24);
if (status == PrintImpact.Impact_FAIL)
{
MessageBox.Show("打印机设置字符行高失败!");
return;
}
if (status == PrintImpact.Impact_ERROR_INVALID_HANDLE)
{
MessageBox.Show("打印机设置字符行高端口或文件的句柄无效!");
return;
}
if (status == PrintImpact.Impact_ERROR_INVALID_PARAMETER)
{
MessageBox.Show("打印机设置字符行高参数无效!");
return;
}
string text = tbPrintText.Text;
//打印文本
status = d.TextOut(com, PrintImpact.Impact_COM_STATUS, text, 1, 1, PrintImpact.Impact_FONT_TYPE_CHINESE, PrintImpact.Impact_FONT_STYLE_NORMAL);
if (status == PrintImpact.Impact_FAIL)
{
MessageBox.Show("打印机打印文本失败!");
return;
}
if (status == PrintImpact.Impact_ERROR_INVALID_HANDLE)
{
MessageBox.Show("打印机打印文本端口或文件的句柄无效!");
return;
}
if (status == PrintImpact.Impact_ERROR_INVALID_PARAMETER)
{
MessageBox.Show("打印机打印文本参数无效!");
return;
}
MessageBox.Show("打印完成!");
}
private void button4_Click(object sender, EventArgs e)
{
int status = PrintImpact.Impact_SUCCESS;
//定义打印操作对象
PrintImpact d = new PrintImpact();
//向前走纸
status = d.FeedLine(com, PrintImpact.Impact_COM_STATUS);
if (status == PrintImpact.Impact_FAIL)
{
MessageBox.Show("向前走纸失败!");
return;
}
if (status == PrintImpact.Impact_ERROR_INVALID_HANDLE)
{
MessageBox.Show("向前走纸端口或文件的句柄无效!");
return;
}
if (status == PrintImpact.Impact_ERROR_INVALID_PARAMETER)
{
MessageBox.Show("向前走纸参数无效!");
return;
}
}
private void button5_Click(object sender, EventArgs e)
{
int status = PrintImpact.Impact_SUCCESS;
//定义打印操作对象
PrintImpact d = new PrintImpact();
//切纸
status = d.CutPaper(com, PrintImpact.Impact_COM_STATUS, PrintImpact.Impact_CUT_MODE_FULL, 0);
if (status == PrintImpact.Impact_FAIL)
{
MessageBox.Show("打印机切纸失败!");
return;
}
if (status == PrintImpact.Impact_ERROR_INVALID_HANDLE)
{
MessageBox.Show("打印机切纸端口或文件的句柄无效!");
return;
}
if (status == PrintImpact.Impact_ERROR_INVALID_PARAMETER)
{
MessageBox.Show("打印机切纸参数无效!");
return;
}
}
private void button6_Click(object sender, EventArgs e)
{
int status = PrintImpact.Impact_SUCCESS;
//定义打印操作对象
PrintImpact d = new PrintImpact();
//状态返回
byte pszStatus = 0x00;
//返回打印机状态
status = d.RTQueryStatus(com, PrintImpact.Impact_COM_STATUS, ref pszStatus);
if (status == PrintImpact.Impact_FAIL)
{
MessageBox.Show("查询打印机状态失败!");
return;
}
if (status == PrintImpact.Impact_ERROR_INVALID_HANDLE)
{
MessageBox.Show("打印机打印机状态的句柄无效!");
return;
}
if (status == PrintImpact.Impact_ERROR_INVALID_PARAMETER)
{
MessageBox.Show("打印机状态参数无效!");
return;
}
int[] iBits = new int[8];
for (int i = 0; i < iBits.Length; i++)
{
iBits[i] = Convert.ToInt32((pszStatus >> i) & 0x01);
}
if (pszStatus == 1)
{
MessageBox.Show("打印机正常");
}
else
{
if (iBits[0] == 0)
{
MessageBox.Show("有钱箱打开");
}
if (iBits[1] == 1)
{
MessageBox.Show("打印机脱机");
}
if (iBits[2] == 1)
{
MessageBox.Show("上盖打开");
}
if (iBits[3] =