C#下顾显显示的类

//顾显显示的类Led8N.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Ports;
using System.Windows.Forms;
using System.Data.OleDb;

namespace rent_li
{
    public class Led8N
    {
        public static void ShowBalance(double balance,int n)
        {
            string comselect;
            string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;";
            strConnection += @"Data Source=D:/db/rent.mdb";            
            OleDbConnection conn = new OleDbConnection(strConnection);
            try
            {
                conn.Open();
                OleDbCommand cmd = conn.CreateCommand();
                cmd.CommandText = "select marker from marks";
                OleDbDataReader dr = cmd.ExecuteReader();
                if (dr5.Read())
                {
                    comselect = dr["marker"].ToString();

                    dr.Close();
                    byte[] rec = Encoding.UTF8.GetBytes(balance.ToString());
                    //PortData pt = new PortData("COM1", 2400, Parity.Even);
                    PortData pt = new PortData(comselect, 2400, Parity.Even);

                    //发送ESC  Q  A  数字  CR
                    //byte[] rec = Encoding.UTF8.GetBytes("300.00");
                    byte[] buf = new byte[4 + rec.Length];
                    buf[0] = 0x1B;
                    buf[1] = 0x51;
                    buf[2] = 0x41;
                    for (int i = 0; i < rec.Length; i++)
                    {
                        buf[i + 3] = rec[i];
                    }
                    buf[buf.Length - 1] = 0x0D;
                    //MessageBox.Show(rec.ToString());


                    //发送C L R
                    byte[] chs = new byte[3];
                    chs[0] = 0x1B;
                    chs[1] = 0x73;
                    chs[2] = 0x30;


                    //发送状态
                    byte[] statu = new byte[3];
                    statu[0] = 0x1B;
                    statu[1] = 0x73;
                    if (n == 1)
                    {
                        statu[2] = 0x31;
                    }
                    else if (n == 2)
                    {
                        statu[2] = 0x32;
                    }
                    else if (n == 3)
                    {
                        statu[2] = 0x33;
                    }
                    else if (n == 4)
                    {
                        statu[2] = 0x34;
                    }
                    else
                    {
                        statu[2] = 0x30;
                    }


                    try
                    {
                        pt.Open();
                        pt.SendData(chs);
                        pt.SendData(buf);
                        pt.SendData(statu);
                    }
                    catch
                    {
                    }
                    finally
                    {
                        pt.Close();
                    }
                }
                else
                { dr.Close(); }
            }
            catch
            {
                MessageBox.Show("数据库连接失败");
               
            }
            finally
            { conn5.Close(); }
               
           
        }

 

        /// <summary>
        /// 引用方式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        //private void button1_Click(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        double num = Convert.ToDouble(textBox1.Text);
        //        int sw = Convert.ToInt16(textBox2.Text);
        //        Led8N.ShowBalance(num, sw);
        //    }
        //    catch
        //    {
        //        MessageBox.Show("收费显示屏输出出错");
        //    }
        //    finally
        //    {
        //    }
        //}
    }

 

}

 

 

//需要引用的PortData.cs

//顾显通过com口来连接

using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Ports;

namespace rent_li
{
    public class PortData
    {
        public event PortDataReceivedEventHandle Received;
        public event SerialErrorReceivedEventHandler Error;
        public SerialPort port;
        public bool ReceiveEventFlag = false;  //接收事件是否有效 false表示有效

        public PortData(string sPortName, int baudrate, Parity parity)
        {
            port = new SerialPort(sPortName, baudrate, parity, 8, StopBits.One);
            port.RtsEnable = true;
            port.ReadTimeout = 3000;
            port.DataReceived += new SerialDataReceivedEventHandler(DataReceived);
            port.ErrorReceived += new SerialErrorReceivedEventHandler(ErrorEvent);
        }


        ~PortData()
        {
            Close();
        }

        public void Open()
        {
            if (!port.IsOpen)
            {
                port.Open();
            }
        }

        public void Close()
        {
            if (port.IsOpen)
            {
                port.Close();
            }
        }


        //数据发送
        public void SendData(byte[] data)
        {
            if (port.IsOpen)
            {
                port.Write(data, 0, data.Length);
            }
        }


        public void SendData(byte[] data, int offset, int count)
        {
            if (port.IsOpen)
            {
                port.Write(data, offset, count);
            }
        }


        //发送命令
        public int SendCommand(byte[] SendData, ref  byte[] ReceiveData, int Overtime)
        {

            if (port.IsOpen)
            {
                ReceiveEventFlag = true;        //关闭接收事件
                port.DiscardInBuffer();         //清空接收缓冲区                
                port.Write(SendData, 0, SendData.Length);
                int num = 0, ret = 0;
                while (num++ < Overtime)
                {
                    if (port.BytesToRead >= ReceiveData.Length) break;
                    System.Threading.Thread.Sleep(1);
                }
                if (port.BytesToRead >= ReceiveData.Length)
                    ret = port.Read(ReceiveData, 0, ReceiveData.Length);
                ReceiveEventFlag = false;       //打开事件
                return ret;
            }
            return -1;
        }

        public void ErrorEvent(object sender, SerialErrorReceivedEventArgs e)
        {
            if (Error != null) Error(sender, e);
        }


        //数据接收
        public void DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            //禁止接收事件时直接退出
            if (ReceiveEventFlag) return;

            byte[] data = new byte[port.BytesToRead];
            port.Read(data, 0, data.Length);
            if (Received != null) Received(sender, new PortDataReciveEventArgs(data));
        }

        public bool IsOpen()
        {
            return port.IsOpen;
        }
    }


    public delegate void PortDataReceivedEventHandle(object sender, PortDataReciveEventArgs e);
    public class PortDataReciveEventArgs : EventArgs
    {
        public PortDataReciveEventArgs()
        {
            this.data = null;
        }

        public PortDataReciveEventArgs(byte[] data)
        {
            this.data = data;
        }

        private byte[] data;

        public byte[] Data
        {
            get { return data; }
            set { data = value; }
        }
    }

 


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值