forked from HAMGZZ/DEATHRAY-ControlProgram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Comms.cs
224 lines (200 loc) · 7.34 KB
/
Comms.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
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
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Text;
using System.Threading;
namespace ControllProgram
{
class Comms
{
private static SerialPort _SerialPort;
private Logger logger;
private string commName;
public bool writing = true;
public Comms(string commName, int baud, Parity parity = Parity.None, int dataBits = 8, StopBits stopBits = StopBits.One, Handshake handshake = Handshake.None)
{
this.commName = commName;
logger = new Logger("COMMUNICATION", Logger.Level.INFO);
Thread readThread = new Thread(ReceiveLoop);
// Create a new SerialPort object with default settings.
_SerialPort = new SerialPort();
// Allow the user to set the appropriate properties.
_SerialPort.PortName = SetPortName(_SerialPort.PortName, commName);
_SerialPort.BaudRate = baud;
_SerialPort.Parity = parity;
_SerialPort.DataBits = dataBits;
_SerialPort.StopBits = stopBits;
_SerialPort.Handshake = handshake;
// Set the read/write timeouts
_SerialPort.ReadTimeout = 500;
_SerialPort.WriteTimeout = 500;
_SerialPort.Open();
logger.log(Logger.Level.INFO, "Communication stream \"" + commName + "\" started");
readThread.Start();
logger.log(Logger.Level.INFO, "Communication reader \"" + commName + "\" started");
}
public void EndComms()
{
_SerialPort.Close();
logger.log(Logger.Level.INFO, "Communication stream \"" + commName + "\" ended");
}
public void ClearBuffer()
{
logger.log(Logger.Level.DEBUG, "Communication stream \"" + commName + "\" cleared");
_SerialPort.DiscardInBuffer();
}
public int DataAvailable()
{
return _SerialPort.BytesToRead;
}
//send data to port in form of string;
public void Send(string data)
{
writing = true;
try
{
byte[] dataHex = Encoding.Default.GetBytes(data);
logger.log(Logger.Level.DEBUG, "Sending data : " + BitConverter.ToString(dataHex).Replace("-", ","));
_SerialPort.Write(data);
}
catch (Exception ex)
{
logger.log(Logger.Level.WARNING, "Send data failed, exception : " + ex.ToString());
}
writing = false;
}
public void SendBytes(byte[] bytes, int count)
{
writing = true;
try
{
logger.log(Logger.Level.DEBUG, "Sending char : " + BitConverter.ToString(bytes).Replace("-", ","));
_SerialPort.Write(bytes, 0, count);
}
catch (Exception ex)
{
logger.log(Logger.Level.WARNING, "Send bytes : '" + BitConverter.ToString(bytes).Replace("-", ",") + "' failed, exception : " + ex.ToString());
}
writing = false;
}
public void SendChar(char data)
{
byte[] dataHex = { Convert.ToByte(data) };
writing = true;
try
{
logger.log(Logger.Level.DEBUG, "Sending char : " + BitConverter.ToString(dataHex).Replace("-", ","));
_SerialPort.Write(data.ToString());
}
catch (Exception ex)
{
logger.log(Logger.Level.WARNING, "Send char : '" + BitConverter.ToString(dataHex).Replace("-", ",") + "' failed, exception : " + ex.ToString());
}
writing = false;
}
public string ReadLine()
{
if (_SerialPort.IsOpen)
{
try
{
logger.log(Logger.Level.DEBUG, "Reading line...");
return _SerialPort.ReadLine();
}
catch (Exception ex)
{
logger.log(Logger.Level.WARNING, "ReadLine failed, exception : " + ex.ToString());
return "";
}
}
else
return "";
}
public string ReadUntil(string charecter)
{
if (_SerialPort.IsOpen)
{
try
{
logger.log(Logger.Level.DEBUG, "Reading until received '" + charecter + "'");
return _SerialPort.ReadTo(charecter);
}
catch (Exception ex)
{
logger.log(Logger.Level.WARNING, "ReadUntil failed, exception : " + ex.ToString());
return null;
}
}
else
return null;
}
//reads x number of bytes then clears buffer.
public int[] ReadBytes(int numberOfBytesToRead)
{
int[] toRead = new int[numberOfBytesToRead];
if (_SerialPort.IsOpen && _SerialPort.BytesToRead > 0)
{
for (int i = 0; i < numberOfBytesToRead; i++)
{
try
{
logger.log(Logger.Level.DEBUG, "Reading " + numberOfBytesToRead + " bytes received");
toRead[i] = _SerialPort.ReadChar();
}
catch (Exception ex)
{
logger.log(Logger.Level.WARNING, "ReadBytes failed, exception : " + ex.ToString());
}
}
}
return toRead;
}
public char ReadChar()
{
if (_SerialPort.IsOpen)
{
try
{
var c = Convert.ToChar(_SerialPort.ReadChar());
logger.log(Logger.Level.DEBUG, "Reading char : " + Convert.ToByte(c).ToString("x2"));
return c;
}
catch (Exception ex)
{
logger.log(Logger.Level.WARNING, "ReadChar failed, exception : " + ex.ToString());
return '\0';
}
}
else
return '\0';
}
public event EventHandler SerialIn;
protected virtual void OnSerialIn(EventArgs e)
{
EventHandler handler = SerialIn;
handler?.Invoke(this, e);
}
//Thread to check that data is coming in
private void ReceiveLoop()
{
_SerialPort.DataReceived += _SerialPort_DataReceived;
}
//Through public SerialIn event;
private void _SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
OnSerialIn(e);
}
// Display Port values and prompt user to enter a port.
private static string SetPortName(string defaultPortName, string commName)
{
string portName;
Console.Write("Enter COM port value for " + commName + " (Default: {0}): ", defaultPortName);
portName = Console.ReadLine();
if (portName == "" || !(portName.ToLower()).StartsWith("com"))
{
portName = defaultPortName;
}
return portName;
}
}
}