GalvanometerManager_Partial.cs
5.0 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
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;
}
}