GalvanometerManager_Partial.cs 5.0 KB
 
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using TSA_V.Common;

namespace TSA_V.DeviceLibrary
{

    partial class GalvanometerManager
    {
        private static Dictionary<string, GalvanometerPoint> LastPointMap = new Dictionary<string, GalvanometerPoint>();
        private static string mapObj = "";
        public static GalvanometerPoint GetLastPoint( )
        {
            return GetLastPoint(TSAVBean.Gal_Port, TSAVBean.Gal_Addr);
        }
            public static GalvanometerPoint GetLastPoint(string portName, int slvAddr)
        {
            GalvanometerPoint point = new GalvanometerPoint( );
            try
            {
                string name = portName + "_" + slvAddr;
                LastPointMap.TryGetValue(name, out point);
                if (point == null)
                {
                    point = new GalvanometerPoint( );
                }
            }
            catch (Exception ex)
            {
                LogUtil.error("GetLastPoint出错:" + ex.ToString());
            }
            return point;
        }

        public static bool isOpen(string gal_Port)
        {
            AcSerialBean sear = GetSerialBean(gal_Port);
            return sear != null;
        }

        private static void UpdateLastPoint(string portName, int slvAddr, GalvanometerPoint point)
        {
            try
            {
                lock (mapObj)
                {
                    string name = portName + "_" + slvAddr;
                    if (LastPointMap.ContainsKey(name))
                    {
                        LastPointMap.Remove(name);
                    }
                    LastPointMap.Add(name, point);
                }
            }
            catch (Exception ex)
            {
                LogUtil.error("UpdateLastPoint出错:" + ex.ToString());
            }
        }

        private static Dictionary<string, AcSerialBean> serialBeanMap = new Dictionary<string, AcSerialBean>();

        private static AcSerialBean GetSerialBean(string portName)
        {
            if (serialBeanMap.ContainsKey(portName))
            {
                return serialBeanMap[portName];
            }
            return null;
        }

        public static byte[] SendCommand(string portName, byte[] data, int reviceLength = 0, int outTime = 80)
        {
            if (outTime > 80)
            {
                outTime = 80;
            }
            byte[] returnData = null;

            try
            {
                if (data == null)
                {
                    return returnData;
                }
                string strSend = "";
                for (int i = 0; i < data.Length; i++)
                {
                    strSend += string.Format("{0:X2} ", data[i]);
                }
                if (strSend.Equals(""))
                {
                    return returnData;
                }
                if (IsShowMsg)
                {
                    LogUtil.info("串口" + portName + " 写入数据:" + strSend + "");
                }
                AcSerialBean bean = GetSerialBean(portName);
                if (bean == null)
                {
                    LogUtil.debug("振镜 SendCommand  串口未打开【" + portName + "】数据:" + strSend + "。");
                }
                else
                {
                    bean.SendCommand(data, ref returnData, outTime, reviceLength);
                    System.Threading.Thread.Sleep(2);
                    if (returnData != null && returnData.Length > 0)
                    {
                        string reviceData = "";
                        for (int i = 0; i < returnData.Length; i++)
                        {
                            reviceData += string.Format("{0:X2} ", returnData[i]);
                        }
                        if (IsShowMsg)
                        {
                            LogUtil.info("串口" + portName + " 收到数据:" + reviceData + "");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogUtil.info(ex.ToString());
            }
            return returnData;
        }

    }

    public class GalvanometerPoint
    {
        public GalvanometerPoint()
        {

        }
        public GalvanometerPoint( int x, int y, int areax= 4000, int areay= 4000, int pointType = 1, int pointLenth = 1, int pointLight = 10)
        {
            this.AreaX = areax;
            this.AreaY = areay;
            this.X = x;
            this.Y = y;
            this.PointType = pointType;
            this.PointLength = pointLenth;
            this.PointLight = pointLight;
        }
        public int AreaX = 4000;
        public int AreaY = 4000;

        public int X = -1;

        public int Y = -1;

        public int PointType = 1;

        public int PointLength = 10;

        public int PointLight = 10;
    }
}