唐召明
authored
|
1
|
using HHECS.BllModel;
|
唐召明
authored
|
2
3
4
|
using System.Net.Sockets;
using System.Text;
|
唐召明
authored
|
5
|
namespace HHECS.DAQClient.Communications
|
唐召明
authored
|
6
7
8
|
{
public class TcpClientCommunication : ICommunication
{
|
唐召明
authored
|
9
|
public int CommunicationId { get; set; }
|
唐召明
authored
|
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
public string IpAddress => _ipAddress;
protected TcpClient tcpClient = new();
protected string _ipAddress = null!;
protected int _port;
protected byte[] _data = new byte[1024 * 8];
protected TcpClientCommunication() { }
public TcpClientCommunication(int communicationId, string ip, int port) : this()
{
CommunicationId = communicationId;
_ipAddress = ip;
_port = port;
}
|
唐召明
authored
|
30
|
public BllResult ConnectClose()
|
唐召明
authored
|
31
32
33
34
|
{
try
{
tcpClient.Close();
|
唐召明
authored
|
35
|
return BllResultFactory.Success();
|
唐召明
authored
|
36
37
38
|
}
catch (Exception ex)
{
|
唐召明
authored
|
39
|
return BllResultFactory.Error(ex.Message);
|
唐召明
authored
|
40
41
42
|
}
}
|
唐召明
authored
|
43
|
public BllResult ConnectServer()
|
唐召明
authored
|
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
{
try
{
tcpClient = new TcpClient();
tcpClient.Connect(_ipAddress, _port);
Task.Run(async () =>
{
try
{
while (tcpClient.Connected)
{
var stream = tcpClient.GetStream();
var buffer = new byte[_data.Length];
stream.Read(buffer);
var bufferString = Encoding.Default.GetString(buffer).TrimEnd('\0');
if (!string.IsNullOrWhiteSpace(bufferString))
{
_data = buffer;
}
await Task.Delay(100);
}
}
catch (Exception) { }
});
|
唐召明
authored
|
68
|
return BllResultFactory.Success();
|
唐召明
authored
|
69
70
71
|
}
catch (Exception ex)
{
|
唐召明
authored
|
72
|
return BllResultFactory.Error(ex.Message);
|
唐召明
authored
|
73
74
75
|
}
}
|
唐召明
authored
|
76
|
public virtual BllResult Read(IEnumerable<DataItem> dataItems)
|
唐召明
authored
|
77
78
79
80
|
{
throw new NotImplementedException();
}
|
唐召明
authored
|
81
|
public BllResult Read(DataItem dataItem)
|
唐召明
authored
|
82
|
{
|
唐召明
authored
|
83
|
return Read([dataItem]);
|
唐召明
authored
|
84
85
|
}
|
唐召明
authored
|
86
|
public virtual BllResult Write(IEnumerable<DataItem> dataItems)
|
唐召明
authored
|
87
88
89
90
|
{
throw new NotImplementedException();
}
|
唐召明
authored
|
91
|
public BllResult Write(DataItem dataItem)
|
唐召明
authored
|
92
|
{
|
唐召明
authored
|
93
|
return Write([dataItem]);
|
唐召明
authored
|
94
95
96
|
}
}
}
|