⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 formmain.cs

📁 PDA程序PDA for windows CE take a look
💻 CS
📖 第 1 页 / 共 3 页
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.Reflection;
using System.Data.SqlServerCe;
using System.Xml;
using XC2800FunctionLibrary; // 引用动态链接库文件XC2800DLL.dll中的命名空间

namespace GasPDA
{
    public partial class FormMain : Form
    {
        static public XC2800Reader RFIDReader = new XC2800Reader("COM1:", 19200, 0, 8, 1);
        private bool IsOpened = false;     // 端口是否打开
        private byte CloseDelay = 8;       // 关机延时,默认8分钟

        static public bool IsFree = false;
        static public int iCount = 0;      // 保证监听线程始终处于死循环
        static private int IsReceived = -1;// 判断PDA是否被动收到了主动上传指令,以及指令类型 0:读标签  1:条形码

        private static string CurrDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName);

        private static string DBPath =  CurrDir + @"\DB.sdf" ;
        private static string ConnString = "Data Source = " + DBPath + ";Password = ";

        private SqlCeConnection Conn = new SqlCeConnection(ConnString);
        
        static private FileMutex fMutex = new FileMutex();

        // 监听线程
        static public ThreadStart entryPoint = new ThreadStart(Watching);
        static public Thread WatchingThread = new Thread(entryPoint);

        // 监视读写器按键指令
        static void Watching()
        {
            while (iCount > -1)
            {
                if (IsFree)
                {
                    RFIDReader.WatchingPort(ref IsReceived);
                    if (IsReceived != -1)
                    {
                        IsFree = false;
                    }
                }
                Thread.Sleep(10);
            }
        }

        public FormMain()
        {            
            if (!fMutex.Open())
            {
                fMutex.Dispose();
                MessageBox.Show("请“单击”程序图标,以避免运行多个程序!", "提示");
                Application.Exit();
                return;
            }
            else
            {
                InitializeComponent();
                cmb_CloseDelaySet.SelectedIndex = 7;
                tc_Reader.SelectedIndex = 0;
                tb_Info.Text = "";

                if (File.Exists(DBPath))
                  Conn.Open();   // 打开数据库连接 共用此连接 减少打开关闭次数 可极大的减少查询时间

                WatchingThread.Start();
                WatchingThread.IsBackground = true;
            }
        }

        #region 主窗体

        // 切换页面
        private void tc_Reader_SelectedIndexChanged(object sender, EventArgs e)
        {
            IsFree = false;
            timerDo.Enabled = false;

            switch (tc_Reader.SelectedIndex)
            {
                case 0:                        // 读写器配置
                    if (IsOpened)
                    {
                        QueryPower();          // 开始查询电量
                    }
                    break;
                case 1:                        // 读使用证
                    if (IsOpened)
                    {
                        IsFree = true;
                        timerDo.Enabled = true;

                        btn_ReadTag.Enabled = true;
                    }
                    else
                    {
                        btn_ReadTag.Enabled = false;
                    }               
                    break;
                case 2:                        // 读条形码                    
                    if (IsOpened)
                    {
                        IsFree = true;
                        timerDo.Enabled = true;

                        btn_ReadBarCode.Enabled = true;                     
                    }
                    else
                    {
                        btn_ReadBarCode.Enabled = false;
                    }
                    break;
                default:
                    break;
            }
        }
        
        // 退出程序
        private void FormMain_Closing(object sender, CancelEventArgs e)
        {
            if (MessageBox.Show("确定退出本系统吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.OK)
            {
                fMutex.Dispose();
                RFIDReader.Dispose();
                Application.Exit();
            }
            else
            {
                e.Cancel = true;
            }
        }
        
        // Timer事件
        private void timerDo_Tick(object sender, EventArgs e)
        {
            if (IsReceived == 0) // 读标签
            {
                if (tc_Reader.SelectedIndex == 1)
                {
                    Cursor.Current = Cursors.WaitCursor;
                    btn_ReadTag_Click(sender, e);
                    Cursor.Current = Cursors.Default;
                }
                else
                {
                    IsReceived = -1;
                    IsFree = true;
                }
                Thread.Sleep(50);
            }
            if (IsReceived == 1) // 读条形码
            {
                if (tc_Reader.SelectedIndex == 2)
                {
                    Cursor.Current = Cursors.WaitCursor;
                    btn_ReadBarCode_Click(sender, e);
                    Cursor.Current = Cursors.Default;
                }
                else
                {
                    IsReceived = -1;
                    IsFree = true;
                }
                Thread.Sleep(50);
            }
        }

        // 以下为菜单点击事件 用于切换页面
        private void menuItem2_Click(object sender, EventArgs e)
        {
            tc_Reader.SelectedIndex = 0;
        }

        private void menuItem3_Click(object sender, EventArgs e)
        {
            tc_Reader.SelectedIndex = 1;
        }

        private void menuItem4_Click(object sender, EventArgs e)
        {
            tc_Reader.SelectedIndex = 2;
        }

        private void menuItem5_Click(object sender, EventArgs e)
        {
            tc_Reader.SelectedIndex = 3;
        }

        private void menuItem6_Click(object sender, EventArgs e)
        {
            tc_Reader.SelectedIndex = 4;
        }

        private void menuItem7_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定退出本系统吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.OK)
            {
                fMutex.Dispose();
                RFIDReader.Dispose();
                Application.Exit();
            }
        }

        #endregion 主窗体

        #region 读写器配置

        // 查询读写器电量
        private void QueryPower()
        {
            if (IsOpened == true)
            {
                byte PowerLevel = 0;
                byte CellStatus = 0;

                int Result = RFIDReader.PowerQuery(ref PowerLevel, ref CellStatus);

                if (Result == 1)
                {
                    lbl_PowerLevel.Text = PowerLevel.ToString() + "%";
                    if (PowerLevel > 30)
                    {
                        lbl_PowerLevel.ForeColor = Color.Blue;
                    }
                    else
                    {
                        lbl_PowerLevel.ForeColor = Color.Red;
                    }
                    pBar_PowerLevel.Value = PowerLevel;                   

                    switch (CellStatus)
                    {
                        case 0:                            
                            lbl_CellStatus.Text = "正在放电中";
                           
                            lbl_CellStatus.ForeColor = Color.Red;
                            break;
                        case 1:                         
                            lbl_CellStatus.Text = "正在充电中";
                            
                            lbl_CellStatus.ForeColor = Color.Green;
                            break;
                        case 2:
                            lbl_CellStatus.Text = "充电已满";
                            
                            lbl_CellStatus.ForeColor = Color.Blue;
                            break;
                        default:
                            lbl_CellStatus.Text = "电池状态不明";
                            break;
                    }
                }
            }
            else
            {
                lbl_CellStatus.Text = "";
                lbl_PowerLevel.Text = "";
                pBar_PowerLevel.Value = 0;
            }
        }

        // 添加时间到操作记录并显示
        private void AddTime2Record(string newtxt)
        {
            string strTime = DateTime.Now.ToString();
            int startIndex = strTime.IndexOf(" ", 0);
            startIndex++;
            char[] arrTime = new char[strTime.Length - startIndex];
            strTime.CopyTo(startIndex, arrTime, 0, arrTime.Length);

            string Time = "";
            for (int i = 0; i < arrTime.Length; i++)
            {
                Time = Time + arrTime[i].ToString();
            }
            newtxt = Time + " " + newtxt;

            if (tb_Info.Text != "")
            {
                tb_Info.Text = newtxt + "\r\n" + tb_Info.Text;
            }
            else
            {
                tb_Info.Text = newtxt;
            }
        }

        // 建立连接
        private void btn_OpenPort_Click(object sender, EventArgs e)
        {
            string info = "";

            Cursor.Current = Cursors.WaitCursor;

            if (!IsOpened)
            {
                IsOpened = RFIDReader.OpenPort();
            }

            if (IsOpened)
            {
                int IfOpen = RFIDReader.Communicate();

                if (IfOpen != 1)
                {
                    IsOpened = false;

                    //播放失败声音
                    Sound.PlayWAV(@"\Windows\Alarm2.wav");

                    info = "与读写器建立连接失败!" + RFIDReader.ErrorMatch(IfOpen);
                }
                else
                {
                    btn_OpenPort.Enabled = false;
                    btn_ClosePort.Enabled = true;
                    btn_CloseReader.Enabled = true;
                    cmb_CloseDelaySet.Enabled = true;

                    QueryPower();

                    Sound.PlayWAV(@"\Windows\Infend.wav");

                    info = "连接成功!";
                }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -