1 Star 0 Fork 0

mbdong/MissionPlanner

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
SerialOutputPass.cs 4.46 KB
一键复制 编辑 原始数据 按行查看 历史
Michael Oborne 提交于 2015-11-14 13:55 . Formatting
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;
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/mbdong/MissionPlanner.git
git@gitee.com:mbdong/MissionPlanner.git
mbdong
MissionPlanner
MissionPlanner
master

搜索帮助