Skip to content

Commit

Permalink
TCPSerial Fixes ArduPilot#3307
Browse files Browse the repository at this point in the history
Add ConfigRef in TCPSerial to save port and host to Settings with custom name
After port is closed, we do not autoReconnect
  • Loading branch information
Godeffroy authored and meee1 committed Mar 20, 2024
1 parent c4b2e1a commit 2e282c1
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Controls/MovingBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private void BUT_connect_Click(object sender, EventArgs e)
BUT_connect.Text = Strings.Stop;
break;
case "TCP Client":
comPort = new TcpSerial() { retrys = 999999, autoReconnect = true };
comPort = new TcpSerial() { retrys = 999999, autoReconnect = true, ConfigRef = "MovingBaseTCP" };
CMB_baudrate.SelectedIndex = 0;
break;
case "UDP Host - 14551":
Expand Down
2 changes: 1 addition & 1 deletion Controls/SerialOutputCoT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private void BUT_connect_Click(object sender, EventArgs e)
BUT_connect.Text = Strings.Stop;
break;
case "TCP Client":
CoTStream = new TcpSerial() { retrys = 999999, autoReconnect = true };
CoTStream = new TcpSerial() { retrys = 999999, autoReconnect = true, ConfigRef = "SerialOutputCoTTCP" };
CMB_baudrate.SelectedIndex = 0;
break;
case "UDP Host - 14551":
Expand Down
2 changes: 1 addition & 1 deletion Controls/SerialOutputNMEA.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private void BUT_connect_Click(object sender, EventArgs e)
BUT_connect.Text = Strings.Stop;
break;
case "TCP Client":
NmeaStream = new TcpSerial() { retrys = 999999, autoReconnect = true };
NmeaStream = new TcpSerial() { retrys = 999999, autoReconnect = true, ConfigRef = "SerialOutputNMEATCP" };
CMB_baudrate.SelectedIndex = 0;
break;
case "UDP Host - 14551":
Expand Down
4 changes: 2 additions & 2 deletions Controls/SerialOutputPass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private void BUT_connect_Click(object sender, EventArgs e)

case "TCP Client":

MainV2.comPort.MirrorStream = new TcpSerial() { retrys = 999999, autoReconnect = true };
MainV2.comPort.MirrorStream = new TcpSerial() { retrys = 999999, autoReconnect = true, ConfigRef = "SerialOutputPassTCP" };
CMB_baudrate.SelectedIndex = 0;
break;
case "UDP Host - 14550":
Expand Down Expand Up @@ -208,7 +208,7 @@ private void myDataGridView1_CellContentClick(object sender, DataGridViewCellEve
}
else if (direction == "Outbound")
{
MainV2.comPort.MirrorStream = new TcpSerial() { retrys = 999999, autoReconnect = true, Host = extra, Port = port };
MainV2.comPort.MirrorStream = new TcpSerial() { retrys = 999999, autoReconnect = true, Host = extra, Port = port, ConfigRef = "SerialOutputPassTCP" };
CMB_baudrate.SelectedIndex = 0;
MainV2.comPort.MirrorStream.Open();
}
Expand Down
29 changes: 17 additions & 12 deletions ExtLibs/Comms/CommsTCPSerial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ public class TcpSerial : CommsBase, ICommsSerial, IDisposable
public bool autoReconnect;
public TcpClient client = new TcpClient();
private bool inOpen;
private bool closed;
private DateTime lastReconnectTime = DateTime.MinValue;

private bool reconnectnoprompt;
private IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);

public int retrys = 3;

public string ConfigRef { get; set; } = "";

public TcpSerial()
{
//System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-US");
Expand Down Expand Up @@ -80,7 +83,7 @@ public bool IsOpen
if (client == null) return false;
if (client.Client == null) return false;

if (autoReconnect && client.Client.Connected == false && !inOpen)
if (autoReconnect && client.Client.Connected == false && !inOpen && !closed)
doAutoReconnect();

return client.Client.Connected;
Expand All @@ -100,6 +103,7 @@ public void Open()
try
{
inOpen = true;
closed = false;

if (client.Client.Connected)
{
Expand All @@ -112,9 +116,9 @@ public void Open()

if (Host == "")
{
dest = OnSettings("TCP_port", dest);
dest = OnSettings("TCP_port" + ConfigRef, dest);

host = OnSettings("TCP_host", host);
host = OnSettings("TCP_host" + ConfigRef, host);

if (!reconnectnoprompt)
{
Expand All @@ -124,7 +128,7 @@ public void Open()
if (inputboxreturn.Cancel == OnInputBoxShow("remote Port", "Enter remote port", ref dest))
throw new Exception("Canceled by request");
}

Host = host;
}
else
{
Expand All @@ -133,12 +137,12 @@ public void Open()

Port = dest;

log.InfoFormat("TCP Open {0} {1}", host, Port);
log.InfoFormat("TCP Open {0} {1}", Host, Port);

OnSettings("TCP_port", Port, true);
OnSettings("TCP_host", host, true);
OnSettings("TCP_port" + ConfigRef, Port, true);
OnSettings("TCP_host" + ConfigRef, Host, true);

InitTCPClient(host, Port);
InitTCPClient(Host, Port);
}
catch
{
Expand Down Expand Up @@ -292,6 +296,7 @@ public string ReadLine()

public void Close()
{
log.InfoFormat("TCP Close {0} {1}", Host, Port);
try
{
if (client.Client != null && client.Client.Connected)
Expand All @@ -311,7 +316,7 @@ public void Close()
catch
{
}

closed = true;
client = new TcpClient();
Host = "";
}
Expand Down Expand Up @@ -340,8 +345,8 @@ private void doAutoReconnect()

client = new TcpClient();

var host = OnSettings("TCP_host", "");
var port = int.Parse(OnSettings("TCP_port", ""));
var host = OnSettings("TCP_host" + ConfigRef, "");
var port = int.Parse(OnSettings("TCP_port" + ConfigRef, ""));

log.InfoFormat("doAutoReconnect {0} {1}", host, port);

Expand Down Expand Up @@ -372,7 +377,7 @@ private void VerifyConnected()
{
log.Info("tcp reconnect");
client = new TcpClient();
client.Connect(OnSettings("TCP_host", ""), int.Parse(OnSettings("TCP_port", "")));
client.Connect(OnSettings("TCP_host" + ConfigRef, ""), int.Parse(OnSettings("TCP_port" + ConfigRef, "")));
retrys--;
}

Expand Down
2 changes: 1 addition & 1 deletion Plugins/OpenDroneID2/NMEA_GPS_Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ private void doGPSConnect()
BUT_connect.Text = Strings.Stop;
break;
case "TCP Client":
comPort = new TcpSerial() { retrys = 999999, autoReconnect = true };
comPort = new TcpSerial() { retrys = 999999, autoReconnect = true, ConfigRef = "OpenDroneIDTCP" };
CMB_baudrate.SelectedIndex = 0;
break;
case "UDP Host - 14551":
Expand Down
4 changes: 2 additions & 2 deletions plugins/Dowding/DowdingUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ private void BUT_connect_Click(object sender, EventArgs e)
BUT_connect.Text = Strings.Stop;
break;
case "TCP Client":
ATStream = new TcpSerial() {retrys = 999999, autoReconnect = true};
ATStream = new TcpSerial() {retrys = 999999, autoReconnect = true, ConfigRef = "DowdingATTCP" };
CMB_baudrate.SelectedIndex = 0;
break;
case "UDP Host - 14551":
Expand Down Expand Up @@ -471,7 +471,7 @@ private void but_cotstart_Click(object sender, EventArgs e)
listenerCoT.BeginAcceptTcpClient(new AsyncCallback(DoAcceptCoTTcpClientCallback), listenerCoT);
break;
case "TCP Client":
CoTStream = new TcpSerial() {retrys = 999999, autoReconnect = true};
CoTStream = new TcpSerial() {retrys = 999999, autoReconnect = true, ConfigRef = "DowdingCoTTCP" };
CMB_baudrate.SelectedIndex = 0;
break;
case "UDP Host - 14551":
Expand Down

0 comments on commit 2e282c1

Please sign in to comment.