MaxSmallTransferBl.cs 4.44 KB
using OS.Spin.Common;
using OS.Spin.Common.Machine;
using OS.Spin.Modle.BusinessLayer;
using System;
using System.Collections.Generic;
using System.IO.Ports;

namespace OS.Spin.BusinessLayer.SubBusiness
{
    public class MaxSmallTransferBl
    {
        private SerialTool _serial = null;
        private Dictionary<int, int> _sy2jxLables = new Dictionary<int, int>() { { 0, 0 }, { 1, 1 }, { 2, 2 }, { 3, 3 }, { 4, 4 }, { 5, 5 }, { 6, 6 },
            { 7, 7 }, { 8, 8 }, { 9, 9 }, { 10, 10 }, { 11, 11 } ,{ 12, 18 }, { 13, 28 }, { 14, 15 }, { 15, 47 }, { 16, 0 }, { 17, 17 }, { 18, 31}, { 19, 100 }, { 20, 0 }};

        /// <summary>
        /// 接收巨细数据处理
        /// </summary>
        /// <param name="type"></param>
        /// <param name="meter"></param>
        public delegate void MaxSmallRecived(int type, double meter);

        public MaxSmallRecived DoRecived;

        public MaxSmallTransferBl(MConfigMaxSmall config)
        {
            _serial = new SerialTool(string.Format("Com{0}", config.ComNum),
                config.BandRate, System.IO.Ports.Parity.None,
                config.ByteSize, System.IO.Ports.StopBits.One);

            // 绑定接收数据处理方法
            _serial.DataReceived += PortRecived;
        }

        private bool _sendFlag = true;
        /// <summary>
        /// 处理接收巨细发来的数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <param name="bits"></param>
        private void PortRecived(object sender, SerialDataReceivedEventArgs e, byte[] bits)
        {
            try
            {
                // Todo 接收处理
                if (null == bits || bits.Length <= 8 || !(bits[0] == 0xFF && bits[1] == 0xFE && bits[bits.Length - 1] == 0xAA))
                {
                    return;
                }

                // 切换花纹
                if (bits[3] == 0x33)
                {
                    //  切换花纹
                    //OS.Spin.Running.Cache.GetInstance().FigureId = bits[4];
                    return;
                }

                // 过滤非法数据
                if (bits[3] != 0x11)
                {
                    return;
                }

                // 计算当前米数
                double current_meter = bits[4] * 256 + bits[5] + bits[6] / 100.0;

                // 修改当前米数
                OS.Spin.Running.Cache.GetInstance().Speed = bits[7];

                if (current_meter <= 2)
                {
                    //  清空上次数据
                    DoRecived?.Invoke(0, current_meter);
                    return;
                }

                if (bits[3] == 0x11)
                {
                    _sendFlag = true;
                }

                if (bits[3] == 0x22)
                {
                    _sendFlag = false;
                }

                // 软触发相机
                DoRecived?.Invoke(1, current_meter);
            }
            catch (Exception ex)
            {
                LogisTrac.WriteLog(string.Format("PortRecived:{0}", ex.Message));
            }

        }

        /// <summary>
        /// 向巨细发送瑕疵信息
        /// </summary>
        /// <param name="results">瑕疵结果</param>
        public void PortSend(List<MDetectionResult> results)
        {
            try
            {
                if (!_sendFlag || null == results || results.Count <= 0)
                {
                    return;
                }

                byte[] bits = new byte[50];
                int send_cnt = 0;
                bits[send_cnt++] = 0xFF;
                bits[send_cnt++] = 0xFD;
                bits[send_cnt++] = (byte)results.Count;

                for (int i = 0; i < results.Count; i++)
                {
                    if (!results[i].IsRepeated)
                    {
                        // 对接巨细平台瑕疵代码
                        bits[send_cnt++] = (byte)_sy2jxLables[results[i].FlawNo];
                    }
                }

                for (int i = send_cnt; i < 5; i++)
                    bits[send_cnt++] = 0;

                bits[send_cnt++] = 0xAA;

                // 发送瑕疵信息
                _serial.writeData(OS.Spin.Common.Machine.SerialTool.ByteToString(bits));
            }
            catch (Exception ex)
            {
                LogisTrac.WriteLog(string.Format("PortSend:{0}", ex.Message));
            }
        }
    }
}