MaxSmallTransferBl.cs
4.44 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
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));
}
}
}
}