forked from ArduPilot/MissionPlanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerialOutputPass.cs
110 lines (98 loc) · 4.24 KB
/
SerialOutputPass.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MissionPlanner.Comms;
using System.Net.Sockets;
using System.Threading;
namespace MissionPlanner
{
public partial class SerialOutputPass : Form
{
static TcpListener listener;
// Thread signal.
public static ManualResetEvent tcpClientConnected = new ManualResetEvent(false);
public SerialOutputPass()
{
InitializeComponent();
chk_write.Checked = MainV2.comPort.MirrorStreamWrite;
CMB_serialport.Items.AddRange(SerialPort.GetPortNames());
CMB_serialport.Items.Add("TCP Host - 14550");
CMB_serialport.Items.Add("TCP Client");
CMB_serialport.Items.Add("UDP Host - 14550");
CMB_serialport.Items.Add("UDP Client");
if (MainV2.comPort.MirrorStream != null && MainV2.comPort.MirrorStream.IsOpen || listener != null)
{
BUT_connect.Text = Strings.Stop;
}
MissionPlanner.Utilities.Tracking.AddPage(this.GetType().ToString(), this.Text);
}
private void BUT_connect_Click(object sender, EventArgs e)
{
if (MainV2.comPort.MirrorStream != null && MainV2.comPort.MirrorStream.IsOpen || listener != null)
{
MainV2.comPort.MirrorStream.Close();
BUT_connect.Text = Strings.Connect;
}
else
{
try
{
switch (CMB_serialport.Text)
{
case "TCP Host - 14550":
case "TCP Host":
MainV2.comPort.MirrorStream = new TcpSerial();
listener = new TcpListener(System.Net.IPAddress.Any,14550);
listener.Start(0);
listener.BeginAcceptTcpClient(new AsyncCallback(DoAcceptTcpClientCallback), listener);
BUT_connect.Text = Strings.Stop;
return;
case "TCP Client":
MainV2.comPort.MirrorStream = new TcpSerial();
break;
case "UDP Host - 14550":
MainV2.comPort.MirrorStream = new UdpSerial();
break;
case "UDP Client":
MainV2.comPort.MirrorStream = new UdpSerialConnect();
break;
default:
MainV2.comPort.MirrorStream = new SerialPort();
MainV2.comPort.MirrorStream.PortName = CMB_serialport.Text;
break;
}
}
catch { CustomMessageBox.Show(Strings.InvalidPortName); return; }
try
{
MainV2.comPort.MirrorStream.BaudRate = int.Parse(CMB_baudrate.Text);
}
catch { CustomMessageBox.Show(Strings.InvalidBaudRate); return; }
try
{
MainV2.comPort.MirrorStream.Open();
}
catch { CustomMessageBox.Show("Error Connecting\nif using com0com please rename the ports to COM??"); return; }
}
}
void DoAcceptTcpClientCallback(IAsyncResult ar)
{
// Get the listener that handles the client request.
TcpListener listener = (TcpListener)ar.AsyncState;
// End the operation and display the received data on
// the console.
TcpClient client = listener.EndAcceptTcpClient(ar);
((TcpSerial)MainV2.comPort.MirrorStream).client = client;
listener.BeginAcceptTcpClient(new AsyncCallback(DoAcceptTcpClientCallback), listener);
}
private void chk_write_CheckedChanged(object sender, EventArgs e)
{
MainV2.comPort.MirrorStreamWrite = chk_write.Checked;
}
}
}