GalvanometerManager.cs 21.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
 
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
{
    /// <summary>
    /// 外部调用的接口放在此文件中
    /// </summary>
    public partial class GalvanometerManager
    {
        public static bool IsShowMsg = false;
        public static bool OpenPort(string portName)
        {
            try
            {
                if (serialBeanMap.ContainsKey(portName))
                {
                    return true;
                }
                int ACBaudRate = 115200;

                AcSerialBean bean = new AcSerialBean(portName, ACBaudRate, Parity.None, 8, StopBits.One);
                bool result = bean.openPort();
                if (!result)
                {
                    LogUtil.info("打开串口【" + portName + "】【" + ACBaudRate + "】失败");
                    return false;
                }
                LogUtil.info("打开串口【" + portName + "】【" + ACBaudRate + "】成功");

                //bean.DataReceived += DataReceived;

                if (serialBeanMap.ContainsKey(portName))
                {
                    serialBeanMap.Remove(portName);
                }
                serialBeanMap.Add(portName, bean);
                return true;
            }
            catch (Exception ex)
            {
                LogUtil.error("" + ex.ToString());
            }
            return false;
        }
        public static void ColsePort(string portName)
        {
            AcSerialBean bean = GetSerialBean(portName);
            if (bean == null)
            {
                LogUtil.info("串口【" + portName + "】未打开,不需要关闭");
                return;
            }
            //清理缓存
            bean.clearInBuffer();
            bean.clearOutBuffer();
            bean.closePort();

            if (serialBeanMap.ContainsKey(portName))
            {
                serialBeanMap.Remove(portName);
            }
        }

        public static bool Handshake(string portName)
        {
            // 1.RS - 485 转换小板握手信号
            //  发送:255,255,255,255,255,254
            //接收:23,34,45,56,67,78
            byte[] sendData = new byte[6] { 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe };
            byte[] reviceData = SendCommand(portName, sendData, 5);
            if (reviceData != null)
            {
                int r1 = Convert.ToInt32(reviceData[0].ToString("X2"), 16);
                int r2 = Convert.ToInt32(reviceData[1].ToString("X2"), 16);
                int r3 = Convert.ToInt32(reviceData[2].ToString("X2"), 16);
                int r4 = Convert.ToInt32(reviceData[3].ToString("X2"), 16);
                int r5 = Convert.ToInt32(reviceData[4].ToString("X2"), 16);
                int r6 = Convert.ToInt32(reviceData[5].ToString("X2"), 16);
                if (r1.Equals(23) && r2.Equals(34) && r3.Equals(45) && r4.Equals(56) && r5.Equals(67) && r6.Equals(78))
                {
                    return true;
                }
            }
            return false;
        }
        public static void ShowPoint(string port, int addr, GalvanometerPoint point)
        {
            //判断最后一个点是不是十字
            GalvanometerPoint lastPoint = GetLastPoint();
            //if (TSAVBean.IsInRunTest())
            //{
            //    if (lastPoint.X.Equals(2000) && lastPoint.Y.Equals(2000) && lastPoint.PointType.Equals(2) && lastPoint.PointLength.Equals(255))
            //    {
            //        return;
            //    }
            //    else
            //    {
            //        GalvanometerPoint p = new GalvanometerPoint(2000, 2000, 4000, 4000, 2, 255, 255);
            //        ShowPoint(port, addr, 2000, 2000, 4000, 4000, 2, 255, 255);
            //        UpdateLastPoint(port, addr, p);
            //    }
            //    return;
            //}
            UpdateLastPoint(port, addr, point);
            ShowPoint(port, addr, point.X, point.Y, point.AreaX, point.AreaY, point.PointType, point.PointLength, point.PointLight);
        }
        private static void ShowPoint(string port, int addr, int x, int y, int areax, int areay, int pointType = 1, int pointLenth = 1, int pointLight = 10)
        {
            string cmd = "FF50";
            byte[] paramData = new byte[20];
            for (int i = 0; i < paramData.Length; i++)
            {
                paramData[i] = 0x00;
            }
            //坐标范围
            byte[] xByte = AcSerialBean.StringToByte(areax, 2);
            byte[] yByte = AcSerialBean.StringToByte(areay, 2);
            Array.Copy(xByte, paramData, 2);
            Array.Copy(yByte, 0, paramData, 2, 2);
            //循环点数
            paramData[4] = 0x00;
            paramData[5] = 0x00;
            //段光
            paramData[6] = 0x01;
            //停留时间

            //点数量
            paramData[11] = 0x01;
            //坐标参数:
            // 坐标点形状 0xXX (范围 1~6; 1 点,2 十字,3 横,4 竖,5 正方形,6 圆;1 个字节)
            paramData[12] = (byte)pointType;
            //坐标点大小 0xXX (范围 5~255;1 个字节)
            paramData[13] = (byte)pointLenth;
            //坐标点亮度 0xXX (范围 0~255; 0 为灭光,255 为最亮;1 个字节)
            paramData[14] = (byte)pointLight;
            //是否填充 0xXX (0 为不填充, 1 为填充;1 个字节, 此值为预留没有用)
            paramData[15] = 0x00;
            //坐标点位置 0xXX 0xXX(X 轴坐标值参考坐标范围值)
            //0xXX 0xXX(Y 轴坐标值参考坐标范围值)
            xByte = AcSerialBean.StringToByte(x, 2);
            yByte = AcSerialBean.StringToByte(y, 2);
            Array.Copy(xByte, 0, paramData, 16, 2);
            Array.Copy(yByte, 0, paramData, 18, 2);

            byte[] sendData = BuildCmdData(cmd, addr, 0, paramData);
            SendCommand(port, sendData);
        }

        public static bool GetPointStatus(string port, int addr)
        {
            //            2.获得激光亮度状态
            //命令:0xFF 0x0A 返回数据:0xFE 0xXX
            //( 0xXX 范围:0x00 ~0xFF; 其中 0x00 为关,0xFF 为开)
            string cmd = "FF0A";
            byte[] sendData = BuildCmdData(cmd, 0);
            int length = sendData.Length;
            byte[] reviceData = SendCommand(port, sendData, length);
            try
            {
                byte data = reviceData[length - 1];
                if (data.Equals(0x00))
                {
                    return false;
                }
            } catch (Exception ex)
            {

            }
            return true;
        }
        public static void SetPointStatus(string port, int addr, bool isOpen)
        {
            //            1.激光亮度设置
            //命令:0xFE 0xXX ( 0xXX 范围:0x00 ~0xFF; 其中 0x00 为关,0xFF 为开) 
            string cmd = "FE00";
            if (isOpen)
            {
                cmd = "FEFF";
            }
            byte[] sendData = BuildCmdData(cmd, addr);
            SendCommand(port, sendData);
        }
        public static void ShowArea(string port, int addr)
        {
            //            2.显示扫描范围
            //命令:0xFF 0x01
            string cmd = "FF01";
            byte[] sendData = BuildCmdData(cmd, 0);
            SendCommand(port, sendData);
        }

        public static void SetArea(string port, int addr, int x, int y)
        {
            byte[] paramData = new byte[4];
            byte[] xByte = AcSerialBean.StringToByte(x, 2);
            byte[] yByte = AcSerialBean.StringToByte(y, 2);
            Array.Copy(xByte, paramData, 2);
            Array.Copy(yByte, 0, paramData, 2, 2);
            string cmd = "FF00";
            byte[] sendData = BuildCmdData(cmd, addr, 0, paramData);
            SendCommand(port, sendData);
        }
        public static Point GetArea(string port, int addr)
        {

            string cmd = "FF03";
            byte[] sendData = BuildCmdData(cmd, addr);
            int length = sendData.Length + 4;
            byte[] reviceData = SendCommand(port, sendData, length);

            byte[] xByte = new byte[2];
            byte[] yByte = new byte[2];
            Array.Copy(reviceData, sendData.Length, xByte, 0, 2);
            Array.Copy(reviceData, sendData.Length + 2, xByte, 0, 2);
            int x = ConverByteToInt(xByte);
            int y = ConverByteToInt(yByte);
            return new Point(x, y);
        }
        public static int Get485Addr(string port)
        {
            string cmd = "FF11";
            byte[] sendData = BuildCmdData(cmd, 0);
            int length = sendData.Length + 2;
            byte[] reviceData = SendCommand(port, sendData, length);
            byte[] databyte = new byte[2];
            Array.Copy(reviceData, sendData.Length, databyte, 0, 2);
            return ConverByteToInt(databyte);
        }
        public static int ConverByteToInt(byte[] bytearray)
        {
            string a = bytearray[0].ToString("X2") + bytearray[1].ToString("X2");
            try
            {
                int absValue = Convert.ToInt32(a, 16);
                return absValue;
            }
            catch (Exception ex)
            {

            }
            return 0;
        }
        public static bool Set485Addr(string port, int addr, int targetAddr)
        {
            string cmd = "FF10";
            byte[] targetAddrbyte = AcSerialBean.StringToByte(targetAddr, 2);
            byte[] sendData = BuildCmdData(cmd, addr, 0, targetAddrbyte);
            byte[] reviceData = SendCommand(port, sendData);
            return true;
        }
        public static bool ContinuePlay(string port, int addr)
        {
            string CMD_ContinuePlay = "FF0C";
            return SendCmd(port, addr, CMD_ContinuePlay);
        }
        public static bool StopPlay(string port, int addr)
        {
            string CMD_StopPlay = "FF0B";
            return SendCmd(port, addr, CMD_StopPlay);
        }
        private static bool SendCmd(string port, int addr, string Cmd)
        {
            byte[] sendData = BuildCmdData(Cmd, addr);
            byte[] reviceData = SendCommand(port, sendData);
            return true;
        }

        public static bool InPoint(string port, int addr, double x, double y)
        {
            GalvanometerPoint lastPoint = GetLastPoint(port, addr);
            if (lastPoint.X.Equals((int)x) && lastPoint.Y.Equals((int)y))
            {
                return true;
            } return false;
        }

        public static byte[] BuildCmdData(string cmd, int RS_485_Addr = 1, int PicNum = 0, byte[] paramData = null)
        {
            // C.RS - 485 地址控制命令
            //1.设置 RS - 485 地址
            // 命令:0xFF 0x10 数据:0xXX 0xXX (地址范围 0~60000;2 个字节
            int length = 2 + 2 + 2;
            if (paramData != null)
            { length = 2 + 2 + 2 + paramData.Length; }
            byte[] data = new byte[length + 6];
            //前导
            data[0] = 0xff;
            data[1] = 0xff;
            data[2] = 0xff;
            data[3] = 0xff;
            //命令数据长度
            byte[] lengthByte = AcSerialBean.StringToByte(length, 2);
            data[5] = lengthByte[1];
            data[4] = lengthByte[0];

            //RS-485地址
            byte[] addrByte = AcSerialBean.StringToByte(RS_485_Addr, 2);

            data[7] = addrByte[1];
            data[6] = addrByte[0];

            //图形编号
            byte[] numByte = AcSerialBean.StringToByte(PicNum, 2);
            data[8] = numByte[0];
            data[9] = numByte[1];
            //命令号
            byte[] cmbByte = AcSerialBean.StringToByte(cmd, 2);
            data[10] = cmbByte[0];
            data[11] = cmbByte[1];
            //命令数据
            if (paramData != null)
            {
                Array.Copy(paramData, 0, data, 12, paramData.Length);
            }
            return data;
        }


        //        F.图形控制命令
        //1. 显示图形
        //命令:0xFF 0x80
        //数据:
        //坐标范围:0xXX 0xXX (X 轴范围 1~4094;2 个字节)
        //0xXX 0xXX (Y 轴范围 1~4094;2 个字节)
        //图形循环次数:
        //0xXX 0xXX (范围 1~60000;0 为无限循环;2 个字节)
        //图形坐标点停留时间(us):
        //0xXX 0xXX 0xXX 0xXX (范围 1~4000000000;4 个字节)
        //图形数量:0xXX (范围 1~200;1 个字节)
        //单个图形参数:
        //图形类型 0xXX (范围 1~8;1 线,2 直线,3 椭圆,4 矩形,5 三角形,
        //6 多边形,7 五角星,8 文字;1 个字节)
        //是否填充 0xXX (0 为不填充, 1 为填充;1 个字节)
        //--------------------------------------以下为不填充图形数据格式------------------------------------------
        //单个图形坐标点数 0xXX 0xXX (范围 1~2000;2 个字节;包括坐标区域)
        //单个图形坐标区域 左上角+右上角+左下角+右下角 (占 4 个坐标点空间)
        //坐标参数数据 1 线 N 个连续坐标点 N 个坐标点
        //2 直线 起点+终点 2 个坐标点
        //3 椭圆 N 个连续坐标点 N 个坐标点
        //4 矩形 左上角+右上角+右下角+左下角 4 个坐标点
        //5 三角形 第 1 顶点+第 2 顶点+第 3 顶点 3 个坐标点
        //6 多边形 N 个连续顶点 N 个坐标点
        //7 五角星 10 个连续顺时针方向的顶点 10 个坐标点
        //8 文字 N 个连续坐标点,且含有激光断笔 N 个坐标点
        //(文字例子:坐标点 1+坐标点 2+坐标点 3+坐标点 3 断笔+坐标点 4 断笔
        //+坐标点 4+坐标点 5+坐标点 6+坐标点 6 断笔+坐标点 1 断笔)
        //单个图形坐标点参数格式 0xXX 0xXX (X 轴坐标值参考坐标范围值;2 个字节)
        //0xXX 0xXX (Y 轴坐标值参考坐标范围值;2 个字节)
        //0xXX (图形坐标点亮度 0~255; 0 为灭光即断笔,255 为最亮;
        //1 个字节)
        //------------------------------------以下为填充图形数据格式-------------------------------------------
        //图形 Y 轴起点 0xXX 0xXX
        //图形 Y 轴终点 0xXX 0xXX
        //单个图形坐标点数 0xXX 0xXX (范围 1~2000;2 个字节)
        //单个图形坐标区域 左上角+右上角+左下角+右下角 (占 4 个坐标点空间)
        //------------------以下为图形轮廓数据备份,类型 3 椭圆不备份------------------------
        //单个图形坐标点数 0xXX 0xXX (范围 1~2000;2 个字节;包括坐标区域)
        //单个图形坐标颜色值 0x00 0xXX(0xXX 为颜色值)
        //坐标参数数据 4 矩形 左上角+右上角+右下角+左下角 4 个坐标点
        //5 三角形 第 1 顶点+第 2 顶点+第 3 顶点 3 个坐标点
        //6 多边形 N 个连续顶点 N 个坐标点
        //7 五角星 10 个连续顶点 10 个坐标点
        //---------------------------------------------------------------------------
        //节填充直线坐标参数数据 3 椭圆 N 个连续填充直线 N 个坐标点
        //4 矩形 N 个连续填充直线 N 个坐标点
        //5 三角形 N 个连续填充直线 N 个坐标点
        //6 多边形 N 个连续填充直线 N 个坐标点
        //7 五角星 N 个连续填充直线 N 个坐标点
        //单个填充直线点参数格式 0xXX 0xXX (X 轴起点坐标值,参考坐标范围值;2 个字节)
        //0xXX 0xXX (X 轴终点坐标值,参考坐标范围值;2 个字节)
        //---------------------------------------------------------------------------------------------------------------------

        public static void ShowImage(string port, int addr, int areax, int areay, int imageType,  List<ImgPoint> areaList,List<ImgPoint> pointList)
        {
            string cmd = "FF80";      //命令:0xFF 0x80
            int pointCount = pointList.Count;
            int length = pointCount * 5 + 31;
            byte[] paramData = new byte[20];
            for (int i = 0; i < paramData.Length; i++)
            {
                paramData[i] = 0x00;
            }
            //坐标范围:0xXX 0xXX (X 轴范围 1~4094;2 个字节)    //0xXX 0xXX (Y 轴范围 1~4094;2 个字节)
            AddPoint(areax, areay, paramData, 0);
            //图形循环次数:       0xXX 0xXX (范围 1~60000;0 为无限循环;2 个字节)
            paramData[4] = 0x00;
            paramData[5] = 0x00;
            //图形坐标点停留时间(us): 0xXX 0xXX 0xXX 0xXX (范围 1~4000000000;4 个字节)

            //图形数量:0xXX (范围 1~200;1 个字节)
            paramData[10] = 0x01;
            //单个图形参数:

            //图形类型 0xXX (范围 1~8;1 线,2 直线,3 椭圆,4 矩形,5 三角形,6 多边形,7 五角星,8 文字;1 个字节)
            paramData[11] = 0x06;
            //是否填充 0xXX (0 为不填充, 1 为填充;1 个字节)
            paramData[12] = 0x00;
            //--------------------------------------以下为不填充图形数据格式------------------------------------------
            //单个图形坐标点数 0xXX 0xXX (范围 1~2000;2 个字节;包括坐标区域)

            //坐标参数数据 1 线 N 个连续坐标点 N 个坐标点
            //2 直线 起点+终点 2 个坐标点
            //3 椭圆 N 个连续坐标点 N 个坐标点
            //4 矩形 左上角+右上角+右下角+左下角 4 个坐标点
            //5 三角形 第 1 顶点+第 2 顶点+第 3 顶点 3 个坐标点
            //6 多边形 N 个连续顶点 N 个坐标点
            //7 五角星 10 个连续顺时针方向的顶点 10 个坐标点
            //8 文字 N 个连续坐标点,且含有激光断笔 N 个坐标点
            //(文字例子:坐标点 1+坐标点 2+坐标点 3+坐标点 3 断笔+坐标点 4 断笔  +坐标点 4+坐标点 5+坐标点 6+坐标点 6 断笔+坐标点 1 断笔)

            //单个图形坐标点数 0xXX 0xXX (范围 1~2000;2 个字节;包括坐标区域)

            byte[] pointByte = AcSerialBean.StringToByte(pointCount, 2);
            Array.Copy(pointByte, 0, paramData, 13, 2);
            //单个图形坐标区域 左上角+右上角+左下角+右下角 (占 4 个坐标点空间)
            AddPoint(200, 200, paramData, 15);
            AddPoint(3800, 200, paramData, 19);
            AddPoint(200, 3800, paramData, 23);
            AddPoint(3800, 3800, paramData, 27);
            //     单个图形坐标点参数格式 0xXX 0xXX (X 轴坐标值参考坐标范围值;2 个字节)      0xXX 0xXX (Y 轴坐标值参考坐标范围值;2 个字节)            
            //0xXX (图形坐标点亮度 0~255; 0 为灭光即断笔,255 为最亮;1 个字节)
            foreach(ImgPoint img in pointList)
            {
                AddPoint(img, paramData, 31);
            }

            byte[] sendData = BuildCmdData(cmd, addr, 0, paramData);
            SendCommand(port, sendData);
        }

        public static void ShowImageTest(string port, int addr)
        {
            List<ImgPoint> areaList = new List<ImgPoint>()
            {
                new ImgPoint(200,200),
                new ImgPoint(3800,200),
                new ImgPoint(200,3800),
                new ImgPoint (3800,3800)
            };
            List<ImgPoint> pointList = new List<ImgPoint>()
            {
                 new ImgPoint(200,200),
                new ImgPoint(3800,300),
                new ImgPoint(500,500),
                new ImgPoint(3800,500),
                new ImgPoint(600,600),
                new ImgPoint (3800,700),
                new ImgPoint(1000,1000),
                new ImgPoint (3800,1200),
                new ImgPoint (3800,300),
            };
            ShowImage(port, addr, 4000, 4000, 6, areaList, pointList);
        }
        private static void AddPoint(int x, int y, byte[] paramData, int startIndex)
        {
            byte[] xByte = AcSerialBean.StringToByte(x, 2);
            byte[] yByte = AcSerialBean.StringToByte(y, 2);
            Array.Copy(xByte, 0, paramData, startIndex, 2);
            Array.Copy(yByte, 0, paramData, startIndex + 2, 2);
        }
        private static void AddPoint(ImgPoint img, byte[] paramData, int startIndex)
        {
            byte[] xByte = AcSerialBean.StringToByte(img.X, 2);
            byte[] yByte = AcSerialBean.StringToByte(img.Y, 2);
           
            Array.Copy(xByte, 0, paramData, startIndex, 2);
            Array.Copy(yByte, 0, paramData, startIndex + 2, 2);
            paramData[startIndex + 4] = (byte)img.LightV ;
        }
    }
    public class ImgPoint
    {
        public ImgPoint(int x, int y, int light = 255)
        {
            this.X = x;
            this.Y = y;
            this.LightV = light;
        }
        public int X = 0;
        public int Y = 0;
        public int LightV = 255;
    }

}