Repository navigation
Expand file tree
/
Copy pathSerialPacket.cs
More file actions
172 lines (150 loc) · 7.36 KB
/
Copy pathSerialPacket.cs
File metadata and controls
172 lines (150 loc) · 7.36 KB
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
using System;
using System.Diagnostics;
using System.IO.Ports;
using System.Linq;
namespace ModemAPI
{
public class SerialPacket : ISerialPacket
{
SerialNetworkInterface io;
private readonly TimeSpan BetweenByteReceiveTimeout = TimeSpan.FromSeconds(1);
private readonly object _lock = new object();
public event Action<L1DropBytesReasons, int> OnDataDropped = delegate { };
public Stream UnderlyingStream { get { return io.Port.BaseStream; } }
public SerialPacket(SerialPort io)
{
try
{
this.io = new SerialNetworkInterface(io);
}
catch (InvalidOperationException)
{
this.io = NetworkInterfaceLibrary.NetworkInterfaces.GetValueOrDefault(io.PortName) ?? throw new NullReferenceException("error while SerialPacket was init");
}
}
public void TransmitBuffer(byte[] buffer)
{
if (buffer.Length > 0xFFFFFF)
{
throw new InvalidDataException("buffer length mustn't exceed 16777215 bytes");
}
// uint24 length (big-endian)
byte len0 = (byte)((buffer.Length >> 16) & 0xFF);
byte len1 = (byte)((buffer.Length >> 8) & 0xFF);
byte len2 = (byte)(buffer.Length & 0xFF);
List<byte> toTransmit = new()
{
0x55,
len0,
len1,
len2
};
toTransmit.AddRange(buffer);
//var t0 = Stopwatch.GetTimestamp();
io.Write(toTransmit.ToArray());
ModemAPIDebugger.OutputDebugMessage("trmit");
ModemAPIDebugger.PrintByteArray([.. toTransmit]);
//Console.WriteLine($"sent packet in {Stopwatch.GetElapsedTime(t0).TotalMilliseconds}ms");
//ModemAPIDebugger.PrintByteArray(buffer);
}
public void OnBufferReceived(Action<byte[], Action> onReceive)
{
List<byte> incomingBuffer = [];
int incomingBufferLastSize = 0;
Stopwatch receiveTimer = new();
ModemAPIDebugger.OutputDebugMessage("Subscribed to the serial buffer");
io.OnDataReceived(delegate (byte[] data)
{
lock (_lock)
{
incomingBuffer.AddRange(data);
// the timeout thing
// we got new data, so timeout resets
ModemAPIDebugger.OutputDebugMessage($"RECEIVED {data.Length} BYTES OF DATA");
bool killerBool = false;
void killer()
{
killerBool = true;
}
ModemAPIDebugger.OutputDebugMessage($"1: INCOMING BUFFER COUNTS {incomingBuffer.Count} BYTES");
if (killerBool)
{
return;
}
//var t0 = Stopwatch.GetTimestamp();
while (true)
{
//Console.WriteLine($"incoming buffer size: {incomingBuffer.Count}, growth: {incomingBuffer.Count - incomingBufferLastSize}");
incomingBufferLastSize = incomingBuffer.Count;
// atleast 4 bytes (sync and length)
if (incomingBuffer.Count < 4)
{
break;
}
receiveTimer.Restart();
if (incomingBuffer.Count > 0 &&
receiveTimer.Elapsed > BetweenByteReceiveTimeout)
{
ModemAPIDebugger.OutputDebugMessage(
$"Receive timeout ({BetweenByteReceiveTimeout.TotalSeconds}s, elapsed {receiveTimer.Elapsed}). Dropping {incomingBuffer.Count} buffered bytes.");
Task.Run(() => OnDataDropped(L1DropBytesReasons.DataTimedOut, incomingBuffer.Count));
incomingBuffer.Clear();
}
int syncIndex = incomingBuffer.IndexOf(0x55);
if (syncIndex == -1)
{
// no sync byte, drop everything cause it's shit
ModemAPIDebugger.OutputDebugMessage($"No sync byte, clearing {incomingBuffer.Count} bytes");
Task.Run(() => OnDataDropped(L1DropBytesReasons.NoHeader, incomingBuffer.Count));
incomingBuffer.Clear();
break;
}
if (syncIndex > 0)
{
// remove everything before sync
incomingBuffer.RemoveRange(0, syncIndex);
Task.Run(() => OnDataDropped(L1DropBytesReasons.FindingHeaders, syncIndex));
}
int targetDataLength =
(incomingBuffer[1] << 16) |
(incomingBuffer[2] << 8) |
incomingBuffer[3];
ModemAPIDebugger.OutputDebugMessage($"SerialPacket.cs received {incomingBuffer.Count} of {targetDataLength} (Length bytes: {incomingBuffer[1]}, {incomingBuffer[2]}, {incomingBuffer[3]})");
ModemAPIDebugger.PrintByteArray([.. incomingBuffer]);
// check if everything is received
if (incomingBuffer.Count < targetDataLength + 4)
{
break;
}
byte[] packet = incomingBuffer
.GetRange(4, targetDataLength)
.ToArray();
incomingBuffer.RemoveRange(0, targetDataLength + 4);
//incomingBuffer.Clear();
ModemAPIDebugger.OutputDebugMessage($"2: INCOMING BUFFER COUNTS {incomingBuffer.Count} BYTES");
receiveTimer.Reset();
onReceive(packet, killer);
ModemAPIDebugger.OutputDebugMessage("recv");
//ModemAPIDebugger.PrintByteArray(incomingBuffer.ToArray());
}
//Console.WriteLine($"process packets in {Stopwatch.GetElapsedTime(t0).TotalMilliseconds}ms");
}
});
}
private int CalculateTimeout(int packetSize, int baudRate)
{
// B/s speed
double bytesPerSecond = baudRate / 8.0;
// time
double transferTime = packetSize / bytesPerSecond;
// +10% extra
double timeoutSeconds = transferTime * 1.1;
// add 100ms to prevent mistimeouts
timeoutSeconds += 0.1;
// convert to ms
int timeoutMs = (int)Math.Ceiling(timeoutSeconds * 1000);
ModemAPIDebugger.OutputDebugMessage($"Timeout for {packetSize} bytes at {baudRate} bps: {timeoutMs}ms");
return timeoutMs;
}
}
}