SendWireManager.cs 9.9 KB

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

namespace URSoldering.DeviceLibrary
{
    /// <summary>
    /// 送丝管理类
    /// </summary>
    public class SendWireManager
    {
        public static readonly ILog LOGGER = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        public static bool IsRun = false;
        public static string serialPort = "";

        private static int bautRate = 19200;//波特率
        private static Parity parity = Parity.None;//校验位
        private static int dataBits = 8;//数据位
        private static StopBits stopBits = StopBits.One; //停止位
        private static AcSerialBean sb = null;

        private static string CMD_WriteMode = "0010WMOD00002";
        private static string CMD_SetSpeed = "0010WSPD";
        private static string CMD_SetSendLength = "0010WLEN";
        private static string CMD_GetSendLength = "0010RLEN";
        private static string CMD_GetSpeed = "0010RSPD";
        private static string CMD_GetStatus = "0010RECV";
        private static string CMD_StartForwardSend = "0010WSFD00001";
        private static string CMD_StartBackSend = "0010WSFD00000";
        private static string CMD_StopSend = "0010WSSD00000";
        private static string CMD_Reset = "0010WECV00001";

        private static string LogName = "";
        public static bool Init(string port)
        {
            LogName = "送丝器[" + port + "]";
            if (sb == null)
            {
                serialPort = port;
                sb = new AcSerialBean(serialPort, bautRate, parity, dataBits, stopBits);
            }
            try
            {
                if (sb.openPort())
                {
                    bool isOk = true;
                    //parseCommand("WRSP", out isOk);
                    parseCommand(CMD_WriteMode, out isOk);
                    if (isOk)
                    {
                        IsRun = true;
                        return true;
                    }
                    else
                    {
                        LogUtil.error(LogName+ "初始化失败!");
                        Release();
                        return false;
                    }
                }
                else
                {
                    LogUtil.error(LOGGER, LogName + "串口打开失败!");
                    IsRun = false;
                    return false;
                }
            }
            catch (Exception ex)
            {
                IsRun = false;
            }
            return true;
        }
        /// <summary>
        /// 释放资源
        /// </summary>
        public static void Release()
        {
            bool isOk = false;
            if (sb != null)
            {
                parseCommand(CMD_StopSend, out isOk);
                sb.closePort();
            }
            IsRun = false;
        }
        public static void Reset()
        {
            bool isOk = false;
            parseCommand(CMD_Reset, out isOk);
        }
        public static double querySpeed()
        {
            bool isOk = false;
            byte[] reviceData = parseCommand(CMD_GetSpeed, out isOk);
            double data = (double)getReviceData(reviceData)/10;
            return data ;
        }

        public static double queryLength()
        {
            bool isOk = false;
            byte[] reviceData = parseCommand(CMD_GetSendLength, out isOk);
            double data = (double)getReviceData(reviceData)/10;
            return data;
        }
        public static int getReviceData(byte[] dataArray)
        {
            string strData = "";
            try
            {
                if (dataArray == null)
                {
                    return 0;
                }
                if (dataArray.Length >= 14)
                {
                    for (int i = 9; i <= 13; i++)
                    {
                        strData += (char)dataArray[i] + "";
                    }
                }
            }
            catch (Exception ex)
            {
                LOGGER.Info(LogName + "转换出错:" + ex.ToString());
            }
             
            int data = 0;
            try
            {
                data = Convert.ToInt32(strData);
            }
            catch (Exception ex)
            {
                LogUtil.debug(LOGGER, LogName + "转换出错:" + strData);
            }
            return data;
        }

        public static bool IsAlarm()
        {
            int error = ReadPortError();
            if (error > 0)
            {
                return true;
            }
            return false;
        }
        public static void SendWireBack(double time, double speed)
        {
            setSpeed(speed);
            double length = (time * speed);
            setLength(length);
            StartBSend();
        }
        public static void SendWire(double time, double speed)
        {
            setSpeed(speed);
            double length = (time * speed);
            setLength(length);
            StartFSend();
        }
        public static bool setSpeed(double speed)
        {
            bool isOk = false;
            int data = (int)(speed * 10);
            parseCommand(CMD_SetSpeed + data.ToString().PadLeft(5, '0'), out isOk);
            return isOk;
        }
        public static bool setLength(double length)
        {
            bool isOk = false;
            int data = (int)(length * 10);
            parseCommand(CMD_SetSendLength + data.ToString().PadLeft(5, '0'), out isOk);
            return isOk;
        }

        public static bool StartFSend()
        {
            bool isOk = false;
            parseCommand(CMD_StartForwardSend, out isOk);
            return isOk;
        }
        public static bool StartBSend()
        {
            bool isOk = false;
            parseCommand(CMD_StartBackSend, out isOk);
            return isOk;
        }
        public static int ReadPortError()
        {
            bool isOk = false;
            byte[] reviceData = parseCommand(CMD_GetStatus, out isOk);
            return getReviceData(reviceData);
        }

        public static bool StopSend()
        {
            bool isOk = false;
            parseCommand(CMD_StopSend, out isOk);
            return isOk;
        }

        public static int ReadCounter()
        {
            bool isOk = false;
            byte[] reviceData = parseCommand("RCTP", out isOk);
            int value = getReviceData(reviceData);
            return value;
        }
        public static byte[] parseCommand(string commandText, out bool isOk)
        {
            if (sb == null)
            {
                isOk = false;
                return null;
            }
            byte[] message = new byte[commandText.Length + 2];
            message[0] = (byte)0x02;
            for (int i = 1; i < commandText.Length + 1; i++)
            {
                message[i] = (byte)commandText[i - 1];
            }
            message[message.Length - 1] = (byte)0x03;
            ushort bcc = 0;
            AcSerialBean.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 (commandText.Length >= 5 && ("R".Equals(commandText.Substring(4, 1))))
            {
                data = new byte[messageAll.Length + 5];
            } 
            else if ("R".Equals(commandText.Substring(0, 1)))
            {
                data = new byte[messageAll.Length + 5];
            }
            else
            {
                data = new byte[messageAll.Length - 5];
            }
            string str = AcSerialBean.byteToHexStr(messageAll," ");
            LogUtil.debug(LogName + " JBC送丝器【" +commandText+"】 转换后【"+str+"】"); 
            sb.SendCommand(messageAll, ref data, 100, out  isOk); 
            return data;
        } 
        public static string GetErrorStr(int error)
        {
            string errMsg = "";
            //            00000 OK
            //00001 Tin feeding is not allowed
            switch (error)
            {
                case 0:
                    errMsg = "OK";
                    break;
                case 1:
                    errMsg = "Tin feeding is not allowed";
                    break;
            }
            return errMsg;
            //Number:00001 BCC error (校验时发生帧错误)     ASCII:0x30 0x30  0x30 0x30 0x31
            // 00002 Format error (通信格式不正确)    ASCII:0x30 0x30  0x30 0x30 0x32
            // 00003 Out of range (修正值超出极限)    ASCII:0x30 0x30  0x30 0x30 0x33
            // 00004 Control error (不接受控制码)     ASCII:0x30 0x30  0x30 0x30 0x34
            // 00005 Control mode (你必须控制设备,模式机器人)   ASCII:0x30 0x30  0x30 0x30 0x35
            // 00006 Station model error (未知状态)              ASCII:0x30 0x30  0x30 0x30 0x36
            // 99999 Undefined (未定义的错误)                    ASCII:0x39 0x39  0x39 0x39 0x39

            switch (error)
            {
                case 1:
                    errMsg = "BCC error";
                    break;
                case 2:
                    errMsg = "Format error";
                    break;
                case 3:
                    errMsg = "Out of range";
                    break;
                case 4:
                    errMsg = "Control error";
                    break;
                case 5:
                    errMsg = "Control mode";
                    break;
                case 6:
                    errMsg = "Station model error";
                    break;
            }
            return errMsg;
        }

       
    }
}