SendWireManager.cs 9.1 KB

using log4net;
using URSoldering.Common;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

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 = ConfigAppSettings.GetValue(Setting_Init.ShuoKe_Control_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 SerialBean sb = new SerialBean(serialPort, bautRate, parity, dataBits, stopBits);

        private static string CMD_WriteMode = "WMOD00002";
        private static string CMD_SetSpeed = "WSPD";
        private static string CMD_SetSendLength = "WLEN";
        private static string CMD_GetSpeed = "RLEN";
        private static string CMD_GetSendLength = "RSPD";
        private static string CMD_GetStatus = "RECV";
        private static string CMD_StartForwardSend = "WSFD00001";
        private static string CMD_StartBackSend = "WSFD00000";
        private static string CMD_StopSend = "WSSD00000";
        private static string CMD_Reset = "WCTP00000";
        //private static string CMD_WriteMode = "02 57 4D 4F 44 30 30 30 30 32 03 22";
        //private static string CMD_SetSpeed = "02 57 53 50 44 30 30 31 31 30 03 21";
        //private static string CMD_SetSendLength = "02 57 4C 45 4E 30 31 31 30 30 03 21";
        //private static string CMD_GetSpeed = "02 52 53 50 44 03 14";
        //private static string CMD_GetSendLength = "02 52 4C 45 4E 03 14";
        //private static string CMD_GetStatus = "02 52 54 45 53 03 11";
        //private static string CMD_StartForwardSend = "02 57 53 46 44 30 30 30 30 31 03 36";
        //private static string CMD_StartBackSend = "02 57 53 46 44 30 30 30 30 30 03 37";
        /// <summary>
        /// 初始化
        /// </summary>
        /// <returns></returns>
        public static bool Init()
        {
            try
            {
                if (sb.openPort())
                {
                    bool isOk = true;
                    parseCommand(CMD_WriteMode, 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()
        {
            bool isOk = false;
            parseCommand(CMD_StopSend, out isOk);
            sb.closePort();
            IsRun = false;
        }
        public static void Reset()
        {
            bool isOk = false;
            parseCommand(CMD_Reset, out isOk);
        }
        public static int querySpeed()
        {
            bool isOk = false;
            byte[] reviceData = parseCommand(CMD_GetSpeed, out isOk);
            return getReviceData(reviceData);
        }

        public static int queryLength()
        {
            bool isOk = false;
            byte[] reviceData = parseCommand(CMD_GetSendLength, out isOk);
            return getReviceData(reviceData);
        }
        public static int getReviceData(byte[] dataArray)
        {
            string temp = "";
            try
            {
                if (dataArray == null)
                {
                    return 0;
                }

                for (int i = 5; i <= 9; i++)
                {
                    temp += (char)dataArray[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;
        }

        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("RECV", 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;
        }
        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];
            }

            sb.SendCommand(messageAll, ref data, 2, 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;
        }

    }
}