当前位置:编程学习 > C#/ASP.NET >>

定义一个SerialPort类 但就是DataRecevied事件触发不了 求解 想确认一下RtsEnable属性必须要打开吗?

using System;
using System.Collections.Generic;

using System.IO.Ports;
using System.Threading;

namespace ZW.IJPS.Domino
{
    public class InjectPrinter : APAL.InkjectPrinter
    {
        private Log.ActiveLog log = new Log.ActiveLog(Environment.CurrentDirectory + @"\DominoLog");       //记录事件

        private Queue<Model.PrintTaskInfo> printTaskQueue = new Queue<Model.PrintTaskInfo>();                 // 待打印任务队列
        private Queue<Model.PrintTaskInfo> printerBufQueue = new Queue<Model.PrintTaskInfo>();                // 打印机缓存队列

        private Dictionary<Model.Position, Model.StyleConfigInfo> configs = new Dictionary<Model.Position, Model.StyleConfigInfo>();      // 打印配置信息

        private CmdBuilder builder = new CmdBuilder();                                                      // 打印命令生成器
        private SerialPort port = null;
        private bool tryToClosePort = false;

        private Thread printThread = null;                                                          // 打印线程

        public InjectPrinter(Model.InjectPrinterInfo printerInfo)
            : base(printerInfo)
        {
            this.port = new SerialPort(base.PrinterInfo.PortName,base.PrinterInfo.BaudRate,Parity.None,8,StopBits.One);
            //this.port.Handshake = Handshake.None;
            //this.port.RtsEnable = true;

            this.port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceivedEventHandler);
            this.port.ReceivedBytesThreshold = 100;
        }

        /// <summary>当接受到的打印机返回的时候</summary>
        /// <param name="sender">发送的内容</param>
        /// <param name="e">Com口接受到数据时的参数</param>
        void port_DataReceivedEventHandler(object sender, SerialDataReceivedEventArgs e)
        {
            if (tryToClosePort == true)
            {
                this.port.ReadExisting();
                return;
            }
            int readOne = this.port.ReadByte();
            string returnString = Convert.ToString(readOne, 16).ToUpper();

            log.Write(String.Format("从端口:{0}接受到的字符为:{1}", this.port.PortName, returnString));

            switch (returnString)
            {
                case "20":
                case "31":
                case "32":
                case "33":
                case "34":
                case "3":
                    {
                        if (printerBufQueue.Count > 0)
                            printerBufQueue.Dequeue();
                        break;
                    }
                default:
                    {
                        break;
                    }
            }
        }

        public override void Start()
        {
            tryToClosePort = false;
            port.Open();
            printThread = new Thread(new ThreadStart(Print));
            printThread.Priority = ThreadPriority.AboveNormal;
            printThread.Start();

        }

        public override void LoadConfig(IList<Model.StyleConfigInfo> configs)
        {
            this.configs.Clear();
            foreach (var config in configs)
            {
                AddConfig(config);
            }
        }

        public override void AddConfig(Model.StyleConfigInfo config)
        {
            if (!this.configs.ContainsKey(config.Pos))
            {
                this.configs.Add(config.Pos, config);
            }
            else
            {
                this.configs[config.Pos] = config;
            }
        }

        /// <summary>发送任务到打印机</summary>
        /// <param name="tasks">任务列表</param>
        public override void RevTask(Model.PrintTaskInfo task)
        {
            printTaskQueue.Enqueue(task);
        }

        public override void Stop()
        {
            printThread.Abort();
            printThread.Join();

            tryToClosePort = true;
            this.port.Dispose();
            this.port.Close();
        }

        private void Print()
        {
            while (true)
            {
                if (printTaskQueue.Count != 0 && printerBufQueue.Count < 1)
                {
                    Model.PrintTaskInfo task = printTaskQueue.Dequeue();
                    Model.StyleConfigInfo config = this.configs[task.Pos];
                    string cmd = this.builder.BuildCmd(config, task);
                    SendToPrinter(cmd);
                    printerBufQueue.Enqueue(task);
                }
                Thread.Sleep(5);
            }
        }

        /// <summary>发送任务到打印机</summary>
        private void SendToPrinter(string cmd)
        {
            log.Write(string.Format("发送到端口:{0}的内容为:\r\n{1}", this.port.PortName, cmd));
            byte[] printValue = GetPrintCmd(cmd);
            this.port.Write(printValue, 0, printValue.Length);         // 发送信息到Printer
        }

        /* 转换输入为2进制代码,以空格作为分割 */
        private byte[] GetPrintCmd(string cmd)
        {
            string[] arrValue = cmd.Split(' ');
            byte[] printCmd = new byte[arrValue.Length];

            for (int i = 0; i < arrValue.Length; i++)
            {
                if (arrValue[i] != null && arrValue[i].Length != 0)
                {
                    string temp = arrValue[i].Trim('h');
                    printCmd[i] = Convert.ToByte(temp, 16);
                }
            }
            return printCmd;
        }
    }
}
--------------------编程问答-------------------- 回答问题 --------------------编程问答-------------------- 请帮忙看一下代码,有没有错误的地方
public InjectPrinter(Model.InjectPrinterInfo printerInfo)
  : base(printerInfo)
  {
  this.port = new SerialPort(base.PrinterInfo.PortName,base.PrinterInfo.BaudRate,Parity.None,8,StopBits.One);
  //this.port.Handshake = Handshake.None;
  //this.port.RtsEnable = true;

  this.port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceivedEventHandler);
  this.port.ReceivedBytesThreshold = 100;
  }
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,