TheMachine.cs
14.8 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
using System;
using System.Collections.Generic;
using System.Threading;
using System.Windows.Forms;
using log4net;
using System.Reflection;
namespace MachineDll
{
public class TheMachine
{
//接受到的数据
private int _FirstBuff;
private int _SecondBuff;
private BuffType _ItsBuffType;
private int _ReadCount;
private MachineControllor _ItsControllor;
private Thread readThread;
public SerialPortSetting _ItsSerialPort;
private readonly List<IReceiveData> itsClietns = new List<IReceiveData>();
public static readonly ILog LOGGER = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
//防止该类被外部引用实例化
//唯一访问的入口点是单一实例,以防不测
internal TheMachine()
{
}
public SerialPortSetting ItsSerialPort
{
get
{
return _ItsSerialPort;
}
}
public List<IReceiveData> ItsClietns
{
get
{
return itsClietns;
}
}
public void AddClients(IReceiveData client)
{
itsClietns.Add(client);
}
public void RemoveClients(IReceiveData waitRemovedClient)
{
//LOGGER.Debug("11");
itsClietns.Remove(waitRemovedClient);
}
public string TestConnection()
{
_ItsSerialPort = new SerialPortSetting();
string portName = _ItsSerialPort.TestConnection();
return portName;
}
public bool StartConnection()
{
bool isConnection = false;
if (_ItsSerialPort == null)
{
//LOGGER.Debug("_ItsSerialPort is null");
try
{
_ItsSerialPort = new SerialPortSetting();
isConnection = _ItsSerialPort.StartConnection();
_ItsControllor = new MachineControllor(_ItsSerialPort);
}
catch (Exception ex)
{
//LOGGER.Warn(ex.StackTrace);
}
//LOGGER.Debug("new one _ItsSerialPort object");
}
else
{
if (!_ItsSerialPort.PortIsOpen())
{
isConnection = _ItsSerialPort.StartConnection();
}
else
{
isConnection = true;
}
}
return isConnection;
}
public void CloseConnection()
{
if (timer != null)
{
timer.Stop();
}
if (_ItsSerialPort != null)
{
_ItsSerialPort.CloseConnection();
}
}
public bool StartReading()
{
if (_ItsSerialPort == null)
{
MessageBox.Show("串口未连接,请先连接串口");
}
if (readThread == null || !readThread.IsAlive)
{
readThread = new Thread(StartReadFromBuffer);
readThread.IsBackground = true;
readThread.Start();
return true;
}
return false;
}
/// <summary>
/// 不要随意终止该读取的线程,当有2个以上客户端时,如果仅仅是本线程不需要读取,调用RemoveClients方法将自己移除
/// </summary>
public void StopReading()
{
if (readThread != null && readThread.IsAlive)
{
if (timer != null)
{
timer.Stop();
}
if (_ItsSerialPort != null && _ItsSerialPort.PortIsOpen())
{
_ItsSerialPort.ReadExisting();
}
readThread.Abort();
}
//LOGGER.Debug("9");
}
public MachineControllor GetControllor
{
get
{
if (_ItsControllor == null)
{
MessageBox.Show("请确认端口已经连接,否则无法对清洗机操作!");
}
return _ItsControllor;
}
}
private Byte[] reciveValue;
private void StartReadFromBuffer()
{
if (_ItsSerialPort != null && _ItsSerialPort.PortIsOpen())
{
while (true)
{
Byte[] byte1 = new Byte[1];
byte1[0] = 0x80;
_ItsSerialPort.SendByteNew(byte1);//BuffType.ADDRESS.ToString());
System.Threading.Thread.Sleep(200);
Byte[] flag = _ItsSerialPort.ReadByte();
if (flag.Length == 0)
{
return;
}
if ((int)flag[0] == 6)//握手成功
{
byte1 = new Byte[1];
byte1[0] = 0x0;
_ItsSerialPort.SendByteNew(byte1);//BuffType.COMMAND1.ToString());
System.Threading.Thread.Sleep(200);
reciveValue = _ItsSerialPort.ReadByte();
RealDataRead();
}
Thread.Sleep(600);
}
}
}
private void RealDataRead()
{
//_FirstBuff = _ItsSerialPort.ReadByte();
//_SecondBuff = _ItsSerialPort.ReadByte();
_ReadCount++;
TellClients();
}
private void TellClients()
{
foreach (IReceiveData data in itsClietns)
{
if (reciveValue.Length > 0)
{
data.NewReceiveData(_ReadCount, reciveValue);
}
}
}
public bool StartReadingIO()
{
//LOGGER.Debug("3");
if (_ItsSerialPort == null)
{
//LOGGER.Debug("4.1");
MessageBox.Show("串口未连接,请先连接串口");
}
if (readThread == null || !readThread.IsAlive)
{
//LOGGER.Debug("into start read io, new one thread,time is "+DateTime.Now);
//LOGGER.Debug("4.2");
readThread = new Thread(StartReadIOFromBuffer);
readThread.IsBackground = true;
readThread.Start();
return true;
}
//LOGGER.Debug("5");
return false;
}
System.Timers.Timer timer;
private void StartReadIOFromBuffer()
{
if (_ItsSerialPort != null && _ItsSerialPort.PortIsOpen())
{
//LOGGER.Debug("new a timer to read io data,time is "+DateTime.Now +" current thread ID is "+Thread.CurrentThread.ManagedThreadId);
timer = new System.Timers.Timer();
timer.Interval = 1000;
timer.AutoReset = true;
timer.Elapsed+=new System.Timers.ElapsedEventHandler(timer_Elapsed);
timer.Start();
}
}
private void timer_Elapsed(object sender, EventArgs e)
{
double receiveIOValue = this.GetControllor.SendAIString();
IOTellClients(receiveIOValue);
}
private void IOTellClients(double receiveIOValue)
{
foreach (IReceiveData data in itsClietns)
{
if (receiveIOValue >= 0)
{
data.NewReceiveIOData(receiveIOValue);
}
}
}
}
}