SolderingManager.cs 8.5 KB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using URSoldering.Common;
using System.IO.Ports;
using log4net;
using System.Reflection;

namespace URSoldering.DeviceLibrary
{
    /// <summary>
    /// TODO:烙铁管理类
    /// </summary>
    public class SolderingManager
    {
        public static readonly ILog LOGGER = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
      
        public static bool IsRun = false;
  
        private static int bautRate = 19200;//波特率
        private static Parity parity = Parity.None;//校验位
        private static int dataBits = 8;//数据位
        private static StopBits stopBits = StopBits.One; //停止位
        public static string serialPort = "";
        public static string equipmentPort = "";
        private static AcSerialBean serialBean = null;
       
        /// <summary>
        /// 初始化
        /// </summary>
        /// <returns></returns>
        public static bool Init(string sPort,string ePort)
        {
            serialPort = sPort;
            equipmentPort = ePort;
            if (serialBean == null)
            {
                serialBean = new AcSerialBean(serialPort, bautRate, parity, dataBits, stopBits);
            }
           
            try
            {
                if (IsRun)
                {
                    return true ;
                }
                if (serialBean.openPort())
                {
                    bool isOk = true;
                    parseCommand("WPS" + equipmentPort + "00000", out isOk);
                    if (isOk)
                    {
                        IsRun = true;
                        return true;
                    }
                    else
                    {
                        LogUtil.error("烙铁初始化失败!"); 
                        Release();
                        return false;
                    }
                }
                else
                {
                    LogUtil.error(LOGGER, "烙铁串口打开失败!");
                    IsRun = false;
                    return false;
                }
            }
            catch (Exception ex)
            {
                IsRun = false;
            }
            return true;
        }
        /// <summary>
        /// 释放资源
        /// </summary>
        public static void Release()
        {
            if (serialBean == null)
            {
                return;
            }
            bool isOk = false;
            parseCommand("WPS" + equipmentPort + "00002",out isOk);
            serialBean.closePort();
            IsRun = false;
        }

        /// <summary>
        /// 查询设置温度
        /// </summary>
        /// <param name="commandText"></param>
        /// <returns></returns>
        public static int querySettingTemp( )
        {
            bool isOk = false;
            byte[] settingTemp = parseCommand("RST" + equipmentPort,out isOk);
           
            string temp = "";
            for (int i = 5; i <= 9; i++)
            {
                temp += (char)settingTemp[i] + "";
            }

            int tem = 0;
            try
            {
                tem = Convert.ToInt32(temp);
            }
            catch (Exception ex)
            {
                LogUtil.debug(LOGGER, "转换温度出错:" + temp);
            }
            return tem;
        }

        /// <summary>
        /// 查询实际温度
        /// </summary>
        /// <param name="commandText"></param>
        /// <returns></returns>
        public static int queryActualTemp()
        {
            if (IsRun)
            {
                bool isOk = false;
                byte[] actualTemp = parseCommand("RTT" + equipmentPort, out isOk);
                return getReviceTemp(actualTemp);
            }
            else
            {
                LogUtil.debug("烙铁未初始化,调用了queryActualTemp直接返回0");
                return 0;
            }
           
        }
        public static int getReviceTemp(byte[] tempArray)
        {
            string temp = "";
            try
            {
                if (tempArray == null)
                {
                    return 0;
                }

                for (int i = 5; i <= 9; i++)
                {
                    temp += (char)tempArray[i] + "";
                }
            }
            catch (Exception ex)
            {
                LOGGER.Info("转换出错:" + ex.ToString());
            }
            int tem = 0;
            try
            {
                tem = Convert.ToInt32(temp);
            }
            catch (Exception ex)
            {
                LogUtil.debug(LOGGER, "转换温度出错:" + temp);
            }
            return tem;
        }
        /// <summary>
        /// 设置温度
        /// </summary>
        /// <param name="commandText"></param>
        public static bool  setTemp(string temp)
        {
            if (IsRun)
            {
                bool isOk = false;
                parseCommand("WST" + equipmentPort + temp.PadLeft(5, '0'), out isOk);
                return isOk;
            }
            else
            {
                LogUtil.info("烙铁未初始化,调用了setTemp直接返回false");
                return false;
            }
        }
        public static int ReadMaxTemp()
        {
            bool isOk = false;
            byte[] maxTemp = parseCommand("RMAT" , out isOk); 
            return getReviceTemp(maxTemp);
        }
        public static int ReadMinTemp()
        {
            bool isOk = false;
            byte[] minTemp = parseCommand("RMIT", out isOk);
            return getReviceTemp(minTemp); 
        }

        public static int ReadPortError()
        {
            if (IsRun)
            {
                bool isOk = false;
                byte[] minTemp = parseCommand("RPE" + equipmentPort, out isOk);
                return getReviceTemp(minTemp);
            }
            else
            {
                return 0;
            }
        }
        private static byte[] parseCommand(string commandText,out bool isOk)
        {
            byte[] message = new byte[commandText.Length + 2];
            message[0] = (byte)2;
            for (int i = 1; i < commandText.Length + 1; i++)
            {
                message[i] = (byte)commandText[i - 1];
            }
            message[message.Length - 1] = (byte)3;
            ushort bcc = 0;
            SerialBean.CalculateBCC(message, message.Length, out bcc);

            /**
             * 读命令返回值包含数据域
             * 写命令返回值不包含数据域 
             */
            byte[] data = null;
            byte[] messageAll = new byte[message.Length + 1];
            message.CopyTo(messageAll, 0);
            messageAll[messageAll.Length - 1] = (byte)bcc;
            if ("R".Equals(commandText.Substring(0, 1)))
            {
                data = new byte[messageAll.Length + 5];
            }
            else
            {
                data = new byte[messageAll.Length - 5];
            }
          
            serialBean.SendCommand(messageAll, ref data, 100, out  isOk);
            return data;
        }



        public static string GetErrorStr(int error)
        {
            // 00000 OK
            //00001 Short-circuit
            //00002 Short-circuit non-recoverable, equipment should be restarted
            //00003 Open circuit
            //00004 No tool
            //00005 No tool accepted
            //00006 Tool detection
            //00007 Stop due to maximums powers (Not implemented)
            //00008 Stop due to overload (MOS)
            string errMsg="";
            switch(error){
                case 1:
                    errMsg="Short-circuit";
                    break;
                case 2:
                    errMsg="Short-circuit non-recoverable, equipment should be restarted";
                    break;
                case 3:
                    errMsg="Open circuit";
                    break;
                case 4:
                    errMsg="No tool";
                    break;
                case 5:
                    errMsg="No tool accepted";
                    break;
                case 6:
                    errMsg="Tool detection";
                    break;
                case 7:
                    errMsg="Stop due to maximums powers (Not implemented)";
                    break;
                case 8:
                    errMsg="Stop due to overload (MOS)";
                    break;
            }
            return errMsg;
 
        }
    }
}