TheMachine.cs
5.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
using System;
using System.Collections.Generic;
using System.Threading;
using System.Windows.Forms;
namespace MachineDll
{
public class TheMachine
{
//接受到的数据
private int _FirstBuff;
private int _SecondBuff;
private BuffType _ItsBuffType;
private int _ReadCount;
private MachineControllor _ItsControllor;
private Thread readThread;
private SerialPortSetting _ItsSerialPort;
private readonly List<IReceiveData> itsClietns = new List<IReceiveData>();
//防止该类被外部引用实例化
//唯一访问的入口点是单一实例,以防不测
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)
{
itsClietns.Remove(waitRemovedClient);
}
public void TestConnection()
{
_ItsSerialPort = new SerialPortSetting();
_ItsSerialPort.TestConnection();
}
public bool StartConnection()
{
bool isConnection = false;
if (_ItsSerialPort == null)
{
_ItsSerialPort = new SerialPortSetting();
isConnection=_ItsSerialPort.StartConnection();
_ItsControllor = new MachineControllor(_ItsSerialPort);
}
else
{
if (!_ItsSerialPort.PortIsOpen())
{
isConnection = _ItsSerialPort.StartConnection();
}
else
{
isConnection = true;
}
}
return isConnection;
}
public void CloseConnection()
{
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 (_ItsSerialPort != null && _ItsSerialPort.PortIsOpen())
{
_ItsSerialPort.ReadExisting();
}
readThread.Abort();
}
}
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);
}
}
}
}
}