Skip to content

Commit f9f277f

Browse files
committed
Add project
1 parent 200330a commit f9f277f

15 files changed

+1584
-0
lines changed

GalaevNetwork3.sln

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GalaevNetwork3", "GalaevNetwork3\GalaevNetwork3.csproj", "{9A5E3483-E0F3-4B7A-81EB-C2FF8E08C274}"
4+
EndProject
5+
Global
6+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
7+
Debug|Any CPU = Debug|Any CPU
8+
Release|Any CPU = Release|Any CPU
9+
EndGlobalSection
10+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
11+
{9A5E3483-E0F3-4B7A-81EB-C2FF8E08C274}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
12+
{9A5E3483-E0F3-4B7A-81EB-C2FF8E08C274}.Debug|Any CPU.Build.0 = Debug|Any CPU
13+
{9A5E3483-E0F3-4B7A-81EB-C2FF8E08C274}.Release|Any CPU.ActiveCfg = Release|Any CPU
14+
{9A5E3483-E0F3-4B7A-81EB-C2FF8E08C274}.Release|Any CPU.Build.0 = Release|Any CPU
15+
EndGlobalSection
16+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace GalaevNetwork3.BitArrayRoutine
6+
{
7+
public static class BitArrayExtensions
8+
{
9+
// Based on https://stackoverflow.com/a/31954694
10+
public static ushort CRC16(this byte[] data)
11+
{
12+
ushort crc = 0;
13+
for (int i = 0; i < data.Length; i++)
14+
{
15+
crc ^= (ushort)(data[i] << 8);
16+
for (int j = 0; j < 8; j++)
17+
{
18+
if ((crc & 0x8000) != 0)
19+
crc = (ushort)((crc << 1) ^ 0x1021);
20+
else
21+
crc <<= 1;
22+
}
23+
}
24+
25+
return crc;
26+
}
27+
28+
public static string ToBinString(this BitArray bitArray)
29+
{
30+
StringBuilder builder = new StringBuilder(bitArray.Length);
31+
int pos = 1;
32+
for (var i = 0; i < bitArray.Count; i++, pos++)
33+
{
34+
builder.Append(bitArray[i] ? 1 : 0);
35+
if (pos % 8 == 0)
36+
{
37+
builder.Append(" ");
38+
}
39+
}
40+
41+
return builder.ToString();
42+
}
43+
44+
public static bool IsSameNoCopy(this BitArray srcArray, BitArray cmpArray, int srcStartIndex, int cmpStartIndex, int length)
45+
{
46+
for (var i = 0; i < length; i++)
47+
{
48+
if (srcArray[srcStartIndex + i] != cmpArray[cmpStartIndex + i])
49+
{
50+
return false;
51+
}
52+
}
53+
54+
return true;
55+
}
56+
57+
public static int FindFlag(this BitArray bitArray, int offset = 0)
58+
{
59+
int i = offset;
60+
while (i <= bitArray.Length - C.FlagSize)
61+
{
62+
if (bitArray.IsSameNoCopy(Frame.Flag, i, 0, C.FlagSize))
63+
{
64+
// We hit the flag
65+
return i;
66+
}
67+
68+
i++;
69+
}
70+
71+
return -1;
72+
}
73+
74+
public static byte[] ToByteArray(this BitArray data)
75+
{
76+
byte[] array = new byte[data.Length / 8 + (data.Length % 8 > 0 ? 1 : 0)];
77+
data.CopyTo(array, 0);
78+
return array;
79+
}
80+
81+
public static BitArray BitStaff(this BitArray data)
82+
{
83+
int ones = 0;
84+
int extraBits = 0;
85+
for (var i = 0; i < data.Count; i++)
86+
{
87+
if (data[i])
88+
{
89+
ones++;
90+
}
91+
92+
if (ones == 5)
93+
{
94+
extraBits++;
95+
ones = 0;
96+
}
97+
}
98+
99+
ones = 0;
100+
if (extraBits > 0)
101+
{
102+
BitArray result = new BitArray(data.Length + extraBits);
103+
int position = 0;
104+
for (var i = 0; i < data.Length; i++)
105+
{
106+
if (data[i])
107+
{
108+
ones++;
109+
}
110+
else
111+
{
112+
ones = 0;
113+
}
114+
115+
if (ones == 5)
116+
{
117+
result[position++] = false;
118+
ones = 0;
119+
}
120+
121+
result[position++] = data[i];
122+
}
123+
124+
return result;
125+
}
126+
127+
return data;
128+
}
129+
130+
public static BitArray DeBitStaff(this BitArray data)
131+
{
132+
int ones = 0;
133+
int extraBits = 0;
134+
for (var i = 0; i < data.Count; i++)
135+
{
136+
if (data[i])
137+
{
138+
ones++;
139+
}
140+
else
141+
{
142+
ones = 0;
143+
}
144+
145+
if (ones == 5)
146+
{
147+
extraBits++;
148+
ones = 0;
149+
}
150+
}
151+
152+
ones = 0;
153+
if (extraBits > 0)
154+
{
155+
BitArray result = new BitArray(data.Length - extraBits);
156+
157+
int position = 0;
158+
for (var i = 0; i < data.Length; i++)
159+
{
160+
if (data[i])
161+
{
162+
ones++;
163+
}
164+
165+
if (ones == 5)
166+
{
167+
position++;
168+
ones = 0;
169+
}
170+
171+
result[position++] = data[i];
172+
}
173+
174+
return result;
175+
}
176+
177+
return data;
178+
}
179+
180+
public static List<BitArray> Split(this BitArray array, int maxSize)
181+
{
182+
List<BitArray> parts = new();
183+
184+
BitArrayReader reader = new BitArrayReader(array);
185+
186+
while (reader.Position + maxSize <= array.Length)
187+
{
188+
parts.Add(reader.Read(maxSize));
189+
}
190+
191+
if (reader.Position < array.Length)
192+
{
193+
parts.Add(reader.Read(array.Length - reader.Position));
194+
}
195+
196+
return parts;
197+
}
198+
}
199+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Collections;
3+
4+
namespace GalaevNetwork3.BitArrayRoutine
5+
{
6+
public class BitArrayReader
7+
{
8+
public BitArray Array { get; }
9+
public int Position { get; private set; }
10+
11+
public BitArrayReader(BitArray array, int position = 0)
12+
{
13+
Array = array;
14+
Position = position;
15+
}
16+
17+
public void Adjust(int shift)
18+
{
19+
Position += shift;
20+
}
21+
22+
public BitArray Read(int size)
23+
{
24+
if (Position + size > Array.Length)
25+
{
26+
throw new ArgumentException($"Attempt to read {size}, when only {Array.Length - Position} available");
27+
}
28+
29+
BitArray result = new BitArray(size);
30+
31+
for (int i = 0; i < size; i++)
32+
{
33+
result[i] = Array[Position++];
34+
}
35+
36+
return result;
37+
}
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System.Collections;
2+
3+
namespace GalaevNetwork3.BitArrayRoutine
4+
{
5+
public class BitArrayWriter
6+
{
7+
public BitArray Array { get; }
8+
9+
public int Position { get; private set; }
10+
11+
public BitArrayWriter(BitArray array, int position = 0)
12+
{
13+
Array = array;
14+
Position = position;
15+
}
16+
17+
public void Write(BitArray data)
18+
{
19+
for (var i = 0; i < data.Length; i++)
20+
{
21+
Array[Position++] = data[i];
22+
}
23+
}
24+
}
25+
}

GalaevNetwork3/C.cs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace GalaevNetwork3
2+
{
3+
public static class C
4+
{
5+
public const int ChecksumSize = 16;
6+
public const int FlagSize = 8;
7+
public const int ControlSize = 16;
8+
9+
public const int ReceiverBufferSize = 5; // N
10+
11+
public const int MaxFrameDataSize = 255;
12+
}
13+
}

GalaevNetwork3/ConsoleHelper.cs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Collections;
3+
4+
namespace GalaevNetwork3
5+
{
6+
public static class ConsoleHelper
7+
{
8+
public static object LockObject = new Object();
9+
10+
public static void WriteToConsole(string info, string write)
11+
{
12+
lock (LockObject)
13+
{
14+
Console.WriteLine(info + " : " + write);
15+
}
16+
}
17+
18+
public static void WriteToConsoleArray(string info, BitArray array)
19+
{
20+
lock (LockObject)
21+
{
22+
Console.Write(info + " : ");
23+
for (int i = 0; i < array.Length; i++)
24+
{
25+
if (array[i] == true)
26+
Console.Write("1");
27+
else
28+
Console.Write("0");
29+
}
30+
31+
Console.WriteLine();
32+
}
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)