forked from ArduPilot/MissionPlanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommsSerialScan.cs
167 lines (135 loc) · 5.35 KB
/
CommsSerialScan.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
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Threading;
namespace MissionPlanner.Comms
{
public class CommsSerialScan
{
static public bool foundport = false;
public static ConcurrentBag<ICommsSerial> portinterface;
static object runlock = new object();
public static int run = 0;
public static int running = 0;
static bool connect = false;
static int[] bauds = new int[] {0, 115200, 57600, 38400, 19200, 9600};
static public void Scan(bool connect = false)
{
foundport = false;
portinterface = new ConcurrentBag<ICommsSerial>();
lock (runlock)
{
run = 1;
running = 0;
}
CommsSerialScan.connect = connect;
string[] portlist = SerialPort.GetPortNames();
foreach (var portname in portlist)
{
new Thread(o => { doread(portname.Clone()); }).Start();
}
}
static void doread(object o)
{
lock (runlock)
{
running++;
}
var portname = (string) o;
Console.WriteLine("Scanning {0}", portname);
try
{
ICommsSerial port = new SerialPort();
{
port.PortName = portname;
foreach (var baud in bauds)
{
// try default baud
if(baud != 0)
port.BaudRate = baud;
port.Open();
port.DiscardInBuffer();
// let data flow in
Thread.Sleep(2000);
lock (runlock)
{
if (run == 0)
{
port.Close();
return;
}
}
int available = port.BytesToRead;
var buffer = new byte[available];
int read = port.Read(buffer, 0, available);
Console.WriteLine("{0} {1}", portname, read);
if (read > 0)
{
using (MemoryStream ms = new MemoryStream(buffer, 0, read))
{
MAVLink.MavlinkParse mav = new MAVLink.MavlinkParse();
try
{
again:
var packet = mav.ReadPacket(ms);
if (packet != null && packet.Length > 0)
{
port.Close();
Console.WriteLine("Found Mavlink on port {0} at {1}", port.PortName,
port.BaudRate);
foundport = true;
portinterface.Add(port);
if (connect)
{
lock (runlock)
{
running--;
if (running == 0)
run = 0;
}
doConnect?.Invoke(port);
}
break;
}
Console.WriteLine(portname + " crc: " + mav.badCRC);
goto again;
}
catch (Exception ex)
{
Console.WriteLine(portname + " " + ex.ToString());
}
}
}
try
{
port.Close();
}
catch ( Exception ex)
{
Console.WriteLine(ex.ToString());
}
if (foundport)
break;
}
}
}
catch (Exception ex)
{
Console.WriteLine(portname + " " + ex.ToString());
}
finally
{
lock (runlock)
{
running--;
if (running == 0)
run = 0;
}
}
Console.WriteLine("Scan port {0} Finished!!", portname);
}
public static event doconnect doConnect;
public delegate void doconnect(ICommsSerial port);
}
}