Newer
Older
IRIS_REFACTOR_ZJ / irisDoor / DoorSystem.cs
yxw on 14 Aug 2020 5 KB first commit
using System;
using System.Linq;
using System.IO.Ports;
using System.Threading;
using System.Reflection;
using System.Windows.Forms;
using ImageFormatter;
using irisHelper;

namespace IrisDoor
{
    public class DoorSystem
    {
        private string[] serialPorts;
        private static SerialPort _serialPort;
        private static int thCount = 1;

        public DoorSystem()
        {
            if (ConfigHelper.GetAppConfig("doorSystemEnable") == "true")
            {
                //连接串口
                string serialPort = ConfigHelper.GetAppConfig("serialPort");
                if (connectSerialPort(serialPort) == -1)
                {
                    MessageBox.Show("Connect to serial port failed!");
                    Application.Exit();//连接失败,关闭程序
                }

                Thread th = new Thread(initZhaJi);
                th.Start();
            }
        }

        /// <summary>
        /// 按名称连接串口
        /// </summary>
        /// <param name="sPort"></param>
        public int connectSerialPort(string sPort)
        {
            try
            {
                serialPorts = SerialPort.GetPortNames();
                if (serialPorts.Count() > 0)
                {
                    for (int i = 0; i < serialPorts.Count(); i++)
                    {
                        if (serialPorts[i] == sPort)
                        {
                            _serialPort = new SerialPort();
                            //设置串口参数 
                            _serialPort.PortName = serialPorts[i];
                            _serialPort.BaudRate = 9600;
                            _serialPort.Parity = Parity.None;
                            _serialPort.DataBits = 8;
                            _serialPort.StopBits = StopBits.One;
                            _serialPort.Handshake = Handshake.None;
                            _serialPort.ReadTimeout = 500;
                            _serialPort.WriteTimeout = 500;

                            //打开串口
                            _serialPort.Open();

                            i = 9999;
                            return 0;
                        }
                    }
                    return -1;
                }
                else
                {
                    MessageBox.Show("no serial,closeing!");
                    return -1;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("serial connection failed:" + ex.Message);
                return -1;
            }
        }

        //给门发送开门信号
        public static void open()
        {
            string cardIDstr = "01 05 00 00 FF 00 8C 3A";
            byte[] cardID = BitmapFactory.StrToHexBytes(cardIDstr);
            _serialPort.Write(cardID, 0, cardID.Length);

            int sleepTime = Convert.ToInt32(ConfigHelper.GetAppConfig("sleepTime").ToString());
            Thread.Sleep(sleepTime);

            cardIDstr = "01 05 00 00 00 00 CD CA ";
            cardID = BitmapFactory.StrToHexBytes(cardIDstr);
            _serialPort.Write(cardID, 0, cardID.Length);
            _serialPort.DiscardOutBuffer();
        }

        public static void initZhaJi()
        {
            if (_serialPort == null || !_serialPort.IsOpen)
            {
                LogHelper.WriteLog(MethodBase.GetCurrentMethod().DeclaringType, "initZhaJi+闸机串口连接失败");
                return;
            }
            try
            {
                lock (typeof(DoorSystem))
                {
                    if (thCount == 1)
                    {
                        string cardIDstr = "AT+OUT2+1=OFF\r\n";
                        byte[] cardID = System.Text.Encoding.Default.GetBytes(cardIDstr);
                        _serialPort.Write(cardID, 0, cardID.Length);
                        _serialPort.DiscardOutBuffer();
                    }
                    thCount--;
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(MethodBase.GetCurrentMethod().DeclaringType, ex.Message);
            }
        }

        public static void payCard()
        {
            if (_serialPort == null || !_serialPort.IsOpen)
            {
                LogHelper.WriteLog(MethodBase.GetCurrentMethod().DeclaringType, "parCard+闸机串口连接失败");
                return;
            }
            try
            {
                lock (typeof(DoorSystem))
                {
                    thCount++;

                    string cardIDstr = "AT+OUT2+1=ON\r\n";
                    byte[] cardID = System.Text.Encoding.Default.GetBytes(cardIDstr);
                    _serialPort.Write(cardID, 0, cardID.Length);
                    _serialPort.DiscardOutBuffer();
                }
                Thread.Sleep(12000);
                initZhaJi();
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(MethodBase.GetCurrentMethod().DeclaringType, ex.Message);
            }
        }

    }
}