-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathTcpPort.cs
138 lines (124 loc) · 4.67 KB
/
TcpPort.cs
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
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace UniversaLIS
{
partial class TcpPort : IPortAdapter
{
private const int BUFFER_SIZE = 64000;
// Please note that UniversaLIS currently supports only one TCP connection per port.
readonly TcpListener server;
Socket? client;
readonly byte[] readBuffer = new byte[BUFFER_SIZE];
readonly StringBuilder incomingData = new StringBuilder();
private readonly string portName;
public TcpPort(Tcp tcpSettings)
{
int port = tcpSettings.Socket;
IPAddress localAddr = IPAddress.Parse("127.0.0.1");
server = new TcpListener(localAddr, port);
portName = ((IPEndPoint)server.LocalEndpoint).Port.ToString();
}
string IPortAdapter.PortName
{
get => portName;
}
public event EventHandler? PortDataReceived;
private readonly System.Timers.Timer portTimer = new System.Timers.Timer();
/* This procedure may or may not evolve into something useful. */
protected void CheckDataReceived()
{
bool timedOut = false;
if (!(client is null) && client.Connected)
{
while (!timedOut)
{
int bytesReceived = 0;
try
{
bytesReceived = client.Receive(readBuffer);
incomingData.Append(Encoding.UTF8.GetString(readBuffer, 0, bytesReceived));
}
catch (SocketException)
{
// Most likely a timeout. Ignore it.
timedOut = true;
}
if (bytesReceived == 0)
{
timedOut = true;
}
}
if (incomingData.Length > 0)
{
EventHandler? handler = this.PortDataReceived;
EventArgs eventArgs = new EventArgs();
handler?.Invoke(this, eventArgs);
}
}
}
private void CheckDataReceived(Object source, System.Timers.ElapsedEventArgs e)
{
portTimer.Stop();
CheckDataReceived();
portTimer.Start();
}
void IPortAdapter.Close()
{
if (!(client is null))
{
client.Close();
}
server.Stop();
}
void IPortAdapter.Open()
{
server.Start(1); // Only one instrument connection per port, for simplicity.
Console.WriteLine("Waiting for a connection...");
client = server.AcceptSocket();
AppendToLog("Connected!");
incomingData.Clear();
client.ReceiveTimeout = 100;
portTimer.Interval = 1000;
portTimer.Elapsed += CheckDataReceived;
portTimer.AutoReset = true;
portTimer.Start();
}
string IPortAdapter.ReadChars()
{
string buffer = incomingData.ToString();
incomingData.Clear();
return buffer;
}
string IPortAdapter.PortType()
{
return "tcp";
}
public void AppendToLog(string txt)
{
string? publicFolder = Environment.GetEnvironmentVariable("AllUsersProfile");
var date = DateTime.Now;
string txtFile = $"{publicFolder}\\UniversaLIS\\Tcp_Logs\\TcpLog-{portName}_{date.Year}-{date.Month}-{date.Day}.txt";
if (!Directory.Exists($"{publicFolder}\\UniversaLIS\\Tcp_Logs\\"))
{
Directory.CreateDirectory($"{publicFolder}\\UniversaLIS\\Tcp_Logs\\");
}
string txtWrite = $"{date.ToLocalTime()} \t{txt}\r\n";
File.AppendAllText(txtFile, txtWrite);
}
void IPortAdapter.Send(string messageText)
{
if (!(client is null))
{
byte[] sendBytes = Encoding.ASCII.GetBytes(messageText);
client.Send(sendBytes);
}
else
{
throw new ArgumentNullException(nameof(messageText));
}
}
}
}