-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConnector.cs
142 lines (121 loc) · 4.02 KB
/
Connector.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
using System;
using System.Collections.Generic;
using System.IO.Ports;
namespace SerialToNetDotnet
{
public enum TerminalType
{
raw,
telnet,
unknown
}
public class NetServerConfig
{
public TerminalType terminalType { get; set; }
public EchoType echoType { get; set; }
public List<char> skipChars { get; set; }
public int tcpPort { get; set; }
}
class Connector
{
public class Config
{
public NetServerConfig netCfg { get; set; }
public int baudRate { get; set; }
public string portName { get; set; }
public int databits { get; set; }
public int stopbits { get; set; }
public Parity parity { get; set; }
}
public readonly Config m_config;
private readonly Server m_server;
private readonly SerialPort m_serial;
public bool IsStarted { get; private set; }
public Connector(Config config)
{
m_config = config;
m_server = new Server(m_config.netCfg, m_config.portName);
m_server.DataReceived += NetDataReceivedHandler;
m_server.OnFirstClientConnected += FirstClientConnectedHandler;
m_server.OnLastClientDisconnected += LastClientDisconnectedHandler;
m_serial = new SerialPort(m_config.portName,
m_config.baudRate,
m_config.parity,
m_config.databits,
stopBitsIntToEnum(m_config.stopbits));
IsStarted = false;
m_serial.Handshake = Handshake.None;
m_serial.DataReceived += SerialDataReceivedHandler;
}
public void Start()
{
try
{
m_server.Start();
}
catch
{
throw new Exception("Server cannot be started");
}
IsStarted = true;
}
private void NetDataReceivedHandler(byte[] buffer, int numBytes)
{
m_serial.Write(buffer, 0, numBytes);
}
private void SerialDataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
int numRx = sp.BytesToRead;
byte[] rxBuf = new byte[numRx];
sp.Read(rxBuf, 0, numRx);
m_server.SendBytesToAll(rxBuf);
}
private void FirstClientConnectedHandler()
{
try
{
m_serial.Open();
}
catch (Exception e)
{
Console.WriteLine("Failed to open port: {0}, {1}", m_config.portName, e.ToString());
throw;
}
Console.WriteLine("Serial port " + m_config.portName + " opened");
}
private void LastClientDisconnectedHandler()
{
if (m_serial.IsOpen)
{
m_serial.Close();
Console.WriteLine("Serial port " + m_config.portName + " closed");
}
}
public override string ToString()
{
string res = string.Format(
"Serial {0}, is opened: {1}, type: {2}. Server on port {3}, clients:\r\n",
m_config.portName,
m_serial.IsOpen,
m_config.netCfg.terminalType.ToString(),
m_config.netCfg.tcpPort);
res += m_server.getClientsString();
return res;
}
internal static StopBits stopBitsIntToEnum(int num)
{
switch (num)
{
case 0:
return StopBits.None;
case 1:
return StopBits.One;
case 2:
return StopBits.Two;
default:
throw new ArgumentException(String.Format("Cannot convert {0} to enum StopBits", num));
}
}
}
}