电脑通(过串口服务器的ip地址)将打印命令-->串口服务器-->串口服务器通过串口将电脑发送的ESC/POS指令传给终端pos打印机,执行打印命令。已经实现了直接通过端口发送指令,现在的问题是怎样通过网络发送指令给串口服务器,求各位大牛帮忙!代码如下:

C# code ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
namespace Print
{
    /// <summary>
    /// 串口打印类
    /// </summary>
    public class CommControl
    {
        public CommControl()
        { }
         ///打印位置
         /// Left 居左打印
         /// Center 居中打印
         /// Right 居右打印
        
        public enum HorPos 
        {
            Left, Center, Right
        }
        //
        //private int ColWidth = 32;
        //定义串行端口资源
        private SerialPort serialPort;
         
         ///构造函数
         /// <param name="PortName">打印机所在的串口</param>
        public CommControl(string PortName)
        {
            try
            {
                Console.WriteLine("开始设置打印机初始化状态");
                serialPort = new SerialPort();
                 
                serialPort.PortName = PortName;
                serialPort.BaudRate = 9600;
                serialPort.DataBits = 8;
                serialPort.StopBits = StopBits.One;
                serialPort.Parity = Parity.None;
                serialPort.Open();
                //设置打印机初始化状态
                SetNormalFont();
                Console.WriteLine("打印机初始化设置成功!");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        ///
        /// 析构函数,不调用此函数,不能释放串口
        /// <returns>是否释放成功</returns>
        public bool Dispose()
        {
            Console.WriteLine("开始判断串口状态!");
            bool result = true;
            if (serialPort != null)
            {
                if (serialPort.IsOpen)
                {
                    try
                    {
                        Console.WriteLine("");
                        serialPort.Close();
                        serialPort.Dispose();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        return false;
                    }
                }
            }
            return result;
        }
         
         /// 打印机所在串口是否打开
         /// <returns>true 串口打开成功;false串口打开失败</returns>
          
        public bool IsOpen()
        {
            bool result = false;
            if (serialPort != null && serialPort.IsOpen)
            {
                result = true;
            }
            return result;
        }
         
         }

/// 向打印机发送byte类型的数据
         /// <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 (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }
        }

/// 向打印机发送字符串类型的数据
         /// <param name="Data">要打印的数据</param>
         /// <returns>打印是否成功</returns>
         
        public bool Write(string Data)
        {
            try
            {
                if (IsOpen())
                {
                    byte[] bData = Encoding.Default.GetBytes(Data);
                    Write(bData);
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }
        }

/// 发送数据到打印机,打印完成后,自动跳到下一行
         /// <param name="Data">要打印的数据</param>
         /// <returns>是否打印成功</returns>
        
        public bool WriteLine(string Data)
        {
            bool result = Write(Data);
            if (result)
            {
                //打印头移动到下一行
                result = NewRow();
            }
            return result;
        }

/// 发送数据到打印机,打印完成后,自动跳到下一行,并可指定打印位置
         /// <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);
            //}
            byte[] temp;
            switch (horPos)
            {
                case HorPos.Center:
                    {
                        temp = new byte[] { 0x1B, 0x61, 0x31};
                        serialPort.Write(temp, 0, temp.Length);
                        //Data = Data.PadLeft(length + (ColWidth - length) / 2 - (length - Data.Length), '*');
                        break;
                    }
                case HorPos.Right:
                    {
                        //Data = Data.PadLeft(ColWidth - (length - Data.Length), '*');
                        temp = new byte[] { 0x1B, 0x61, 0x32 };
                        serialPort.Write(temp, 0, temp.Length);
                        break;
                    }
                case HorPos.Left:
                    {
                        //1B 61 n选择字体默认居左显示(n对应ascll码'0')
                        temp = new byte[] { 0x1B, 0x61, 0x30 };
                        serialPort.Write(temp, 0, temp.Length);
                        break;
                    }
                default:
                    {
                        break;
                    }
            }
            return WriteLine(Data);
        }
/// 打印一行分割线====
         /// <returns>是否打印成功</returns>
         
        public bool PrintLine()
        {
            byte[] temp = new byte[] { 0x1B, 0x61, 0x30 };
            serialPort.Write(temp, 0, temp.Length);
            return WriteLine("--------------------------------");
        }

/*
         * 打印日期 格式:2011-06-21 00:00:00
         * <returns>是否打印成功</returns>
         */
        public bool PrintDate()
        {
            return WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
        }

/*
         * 打印头移动到下一行
         * <returns>是否移动成功</returns>
         */
        public bool NewRow()
        {
            byte[] temp = new byte[] { 0x0A };
            return Write(temp);
        }

/*
         * 打印头移动多行
         * <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;
        }
        /*
         * 设置打印机初始化状态
         */
        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[] { 0x1B, 0x21, 0x00 };
                serialPort.Write(temp, 0, temp.Length);
                
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }
        }

/*
         * 以大一倍的字体打印数据
         * <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[] { 0x1B, 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 (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }
            result = SetNormalFont();
            if (result)
            {
                result = NewRow();
            }
            return result;
        }
/*
         * 以大一倍的字体打印数据,打印完成换行
         * <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, 0x01 };
                serialPort.Write(temp, 0, temp.Length);
                //1D 21 选择字体大小,纵向放大1倍
                temp = new byte[] { 0x1B, 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 (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }
            result = SetNormalFont();
            if (result)
            {
                result = NewRow();
            }
            return result;
        }

/*
         * 设置带下划线的行
         * <returns>是否设置成功</returns>
         */
        public bool SetUnderLine()
        {
            bool result = false;

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 (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }
            result = SetNormalFont();
            return result;
        }
    }

关于pos打印机通过tcp/ip操作打印指令集相关推荐

  1. 《CCNP ROUTE 300-101学习指南》——1.4节路由和TCP/IP操作

    本节书摘来自异步社区<CCNP ROUTE 300-101学习指南>一书中的第1章,第1.4节路由和TCP/IP操作,作者 [美]戴安娜 蒂尔(Diane Teare) , 鲍勃 瓦尚(B ...

  2. ttlink无线打印服务器固件,TTLINK TT-180U1打印机服务器 TCP/IP添加打印机的教程

    使用TT-180U1 LPR添加方式,本教程以打印机为兄弟HL-2140激光打印机为实例, 系统为Windows 7 64位系统, 打印服务器IP固定为192.168.1.220 . 优点:不用安装软 ...

  3. ttlink无线打印服务器,TTLINK TT-180U1打印机服务器 TCP/IP添加打印机的教程

    使用TT-180U1 LPR添加方式,本教程以打印机为兄弟HL-2140激光打印机为实例, 系统为Windows 7 64位系统, 打印服务器IP固定为192.168.1.220 . 优点:不用安装软 ...

  4. c# 中崎_C#版OPOS打印(基于北洋OPOS SDK二次开发包,支持EPSON和北洋、佳博、商祺等支持标准ESC/POS指令的POS打印机)...

    C#版OPOS打印(基于北洋OPOS SDK二次开发包,支持EPSON和北洋.佳博.商祺等支持标准ESC/POS指令的POS打印机) 收藏 C#版OPOS打印 基于北洋OPOS SDK二次开发包,支持 ...

  5. TCP/IP协议端口大全

    TCP/IP协议端口大全 应用层网关服务     Internet 连接共享 (ICS)/Internet 连接防火墙 (ICF) 服务的这个子组件对允许网络协议通过防火墙并在 Internet 连接 ...

  6. TCP/IP 端口号大全

    端口:0 服务:Reserved 说明:通常用于分析操作系统.这一方法能够工作是因为在一些系统中"0"是无效端口,当你试图使用通常的闭合端口连接它时将产生不同的结果.一种典型的扫描 ...

  7. uIP 一个免费的TCP/IP栈

    uIP 一个免费的TCP/IP栈 原文:Adam Dunkels adam@dunkels.com 2002年2月15日 翻译:张伟林   2003年5月17日 okelinchang@163.com ...

  8. [心平气和读经典]The TCP/IP Guide(003)

    The TCP/IP Guide [Page 43, 44] Scope of The TCP/IP Guide | 本书的讨论范围 The first step to dealing with a ...

  9. C#版OPOS打印(基于北洋OPOS SDK二次开发包,支持EPSON和北洋、佳博、商祺等支持标准ESC/POS指令的POS打印机)

     C#版OPOS打印 基于北洋OPOS SDK二次开发包,支持EPSON和北洋.佳博.商祺等支持标准ESC/POS指令的POS打印机 支持并口,串口,网口,USB口,驱动方式等多种端口 支持开关钱箱 ...

最新文章

  1. python生成器应用中的一个要点
  2. 操作系统引发对未来道路的认知思考
  3. saas- -m ihrm 项目_Convertlab等企业入选腾讯SaaS加速器二期名单
  4. 虚拟机状态错误_学会这3招,分分钟迁移业务繁忙虚拟机!
  5. windows 远程连接debian_UOS统一操作系统远程协助软件TeamViewer
  6. 五笔输入法的学习记录
  7. html5提供类似“JQuery”中操作类名的方法
  8. 基于C++的Qt网络编程——聊天客户端
  9. 未来教育计算机书,未来教育计算机二级
  10. 感觉所有的方法都有人做了,NLPer怎么找创新点?
  11. Android onKeyDown事件 监听不到Home键 可以监听到back键
  12. Java网络编程并实现一对一聊天室功能
  13. triggered传递参数
  14. 经典音频MUTE电路分析
  15. 创业公司项目管理流程这样做才有效
  16. 共享店铺靠谱么?共享店铺哪家好?全方位测评企雀共享店铺,黑谷共享店铺!
  17. 插屏广告怎么玩?这些优化要点请get~
  18. [转]完美解决图片/链接虚线边框
  19. c#中sealed关键字的使用
  20. 安卓获取已创建的悬浮窗集合

热门文章

  1. 增加php的amqp扩展
  2. Kafka SASL/PLAIN 环境构建(Docker版)
  3. 唱给挚爱高妹(大头妹)的歌~~~
  4. 新一代的数据库备份解决方案--Oracle数据库
  5. word操作:如何修改字体(正确、规范、快捷)
  6. IP-Prefix List
  7. [Android]朝花夕拾之使用DexClassLoader动态加载广点通jar包
  8. 2023 目标,与君共勉
  9. 【JQuery】使用JQuery实现城市两级或三级联动(下拉菜单)
  10. oracle的dmp导出,oracle数据库dmp文件的导出和导入方法