C#串口通信 实现打印功能

   做个打印的小例子,实现串口通信,打印、关闭串口、打印功能。

.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] =

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值