-
Notifications
You must be signed in to change notification settings - Fork 13
/
CommPort.cs
116 lines (106 loc) · 3.92 KB
/
CommPort.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
using System;
using System.IO;
using System.IO.Ports;
using System.Threading;
namespace UniversaLIS
{
public class CommPort : IPortAdapter
{
private readonly SerialPort serialPort = new SerialPort();
private readonly CommFacilitator facilitator;
public CommPort(Serial serial, CommFacilitator facilitator)
{
serialPort.PortName = serial.Portname!;
serialPort.BaudRate = serial.Baud;
serialPort.DataBits = serial.Databits;
serialPort.Parity = serial.Parity;
serialPort.StopBits = serial.Stopbits;
serialPort.Handshake = serial.Handshake;
serialPort.DataReceived += OnSerialDataReceived;
serialPort.ReadTimeout = 50;
this.facilitator = facilitator;
}
public void Send(string messageText)
{
AppendToLog($"Out: \t{messageText}");
byte[] bytes = serialPort.Encoding.GetBytes(messageText);
for (int i = 0; i < bytes.Length; i++)
{
serialPort.Write(bytes, i, 1);
}
}
public static readonly EventWaitHandle logOpen = new EventWaitHandle(true, EventResetMode.AutoReset);
string IPortAdapter.PortName
{
get
{
return serialPort.PortName;
}
}
public void AppendToLog(string txt)
{
string? publicFolder = Environment.GetEnvironmentVariable("AllUsersProfile");
var date = DateTime.Now;
string txtFile = $"{publicFolder}\\UniversaLIS\\Serial_Logs\\SerialLog-{serialPort.PortName}_{date.Year}-{date.Month}-{date.Day}.txt";
if (!Directory.Exists($"{publicFolder}\\UniversaLIS\\Serial_Logs\\"))
{
Directory.CreateDirectory($"{publicFolder}\\UniversaLIS\\Serial_Logs\\");
}
string txtWrite = $"{date.ToLocalTime()} \t{txt}\r\n";
_ = logOpen.WaitOne();
File.AppendAllText(txtFile, txtWrite);
_ = logOpen.Set();
}
public event EventHandler? PortDataReceived;
protected void OnSerialDataReceived(object sender, SerialDataReceivedEventArgs eventArgs)
{
EventHandler? handler = PortDataReceived;
handler?.Invoke(this, eventArgs);
}
public void Open()
{
serialPort.Open();
}
public void Close()
{
serialPort.Close();
}
string GetCharString()
{
char readChar = (char)serialPort.ReadChar();
return $"{readChar}";
}
string IPortAdapter.ReadChars()
{
System.Text.StringBuilder buffer = new System.Text.StringBuilder();
bool timedOut = false;
try
{
/* There are a few messages that won't end in a NewLine,
* so we have to read one character at a time until we run out of them.
*/
do
{ // Read one char at a time until the ReadChar times out.
try
{
buffer.Append(GetCharString());
}
catch (Exception)
{
timedOut = true;
}
} while (!timedOut);
}
catch (Exception ex)
{
facilitator.service.HandleEx(ex);
throw;
}
return buffer.ToString();
}
string IPortAdapter.PortType()
{
return "serial";
}
}
}