private void BUT_savesettings_Click(object sender, EventArgs e)
{
ICommsSerial comPort = new SerialPort();
try
{
if (MainV2.comPort.BaseStream.IsOpen)
{
getTelemPortWithRadio(ref comPort);
}
else
{
comPort.PortName = MainV2.comPort.BaseStream.PortName;
comPort.BaudRate = MainV2.comPort.BaseStream.BaudRate;
}
comPort.ReadTimeout = 4000;
comPort.Open();
}
catch
{
CustomMessageBox.Show("Invalid ComPort or in use");
return;
}
lbl_status.Text = "Connecting";
if (doConnect(comPort))
{
// cleanup
doCommand(comPort, "AT&T");
comPort.DiscardInBuffer();
lbl_status.Text = "Doing Command";
if (RTI.Text != "")
{
// remote
var answer = doCommand(comPort, "RTI5", true);
var items = answer.Split(new[] {'\n'}, StringSplitOptions.RemoveEmptyEntries);
foreach (var item in items)
{
if (item.StartsWith("S"))
{
var values = item.Split(':', '=');
if (values.Length == 3)
{
values[1] = values[1].Replace("/", "_");
var controls = groupBoxRemote.Controls.Find("R" + values[1].Trim(), true);
if (controls.Length > 0)
{
if (controls[0].GetType() == typeof (CheckBox))
{
var value = ((CheckBox) controls[0]).Checked ? "1" : "0";
if (value != values[2].Trim())
{
var cmdanswer = doCommand(comPort,
"RT" + values[0].Trim() + "=" + value + "\r");
if (cmdanswer.Contains("OK"))
{
}
else
{
if (values[1] == "ENCRYPTION_LEVEL")
{
// set this on the local radio as well.
doCommand(comPort, "AT" + values[0].Trim() + "=" + value + "\r");
// both radios should now be using the default key
}
else
{
CustomMessageBox.Show("Set Command error");
}
}
}
}
else if (controls[0] is TextBox)
{
}
else if (controls[0].Name.Contains("MAVLINK")) //
{
if (((ComboBox) controls[0]).SelectedValue.ToString() != values[2].Trim())
{
var cmdanswer = doCommand(comPort,
"RT" + values[0].Trim() + "=" + ((ComboBox) controls[0]).SelectedValue +
"\r");
if (cmdanswer.Contains("OK"))
{
}
else
{
CustomMessageBox.Show("Set Command error");
}
}
}
else if (controls[0] is ComboBox)
{
if (controls[0].Text != values[2].Trim())
{
var cmdanswer = doCommand(comPort,
"RT" + values[0].Trim() + "=" + controls[0].Text + "\r");
if (cmdanswer.Contains("OK"))
{
}
else
{
CustomMessageBox.Show("Set Command error");
}
}
}
}
}
}
}
Sleep(100);
}
comPort.DiscardInBuffer();
{
//local
var answer = doCommand(comPort, "ATI5", true);
var items = answer.Split(new[] {'\n'}, StringSplitOptions.RemoveEmptyEntries);
foreach (var item in items)
{
if (item.StartsWith("S"))
{
var values = item.Split(':', '=');
if (values.Length == 3)
{
values[1] = values[1].Replace("/", "_");
var controls = groupBoxLocal.Controls.Find(values[1].Trim(), true);
if (controls.Length > 0)
{
if (controls[0].GetType() == typeof (CheckBox))
{
var value = ((CheckBox) controls[0]).Checked ? "1" : "0";
if (value != values[2].Trim())
{
var cmdanswer = doCommand(comPort,
"AT" + values[0].Trim() + "=" + value + "\r");
if (cmdanswer.Contains("OK"))
{
}
else
{
CustomMessageBox.Show("Set Command error");
}
}
}
else if (controls[0] is TextBox)
{
}
else if (controls[0].Name.Contains("MAVLINK")) //
{
if (((ComboBox) controls[0]).SelectedValue.ToString() != values[2].Trim())
{
var cmdanswer = doCommand(comPort,
"AT" + values[0].Trim() + "=" + ((ComboBox) controls[0]).SelectedValue +
"\r");
if (cmdanswer.Contains("OK"))
{
}
else
{
CustomMessageBox.Show("Set Command error");
}
}
}
else if (controls[0] is ComboBox)
{
if (controls[0].Text != values[2].Trim())
{
var cmdanswer = doCommand(comPort,
"AT" + values[0].Trim() + "=" + controls[0].Text + "\r");
if (cmdanswer.Contains("OK"))
{
}
else
{
CustomMessageBox.Show("Set Command error");
}
}
}
}
}
}
}
// set encryption keys at the same time, so if we are enabled we dont lose comms.
// we have set encryption to on for both radios, they will be using the default key atm
if (RENCRYPTION_LEVEL.Checked)
{
doCommand(comPort, "RT&E=" + txt_Raeskey.Text.PadRight(32, '0'), true);
}
if (ENCRYPTION_LEVEL.Checked)
{
doCommand(comPort, "AT&E=" + txt_aeskey.Text.PadRight(32, '0'), true);
}
if (RTI.Text != "")
{
// write it
doCommand(comPort, "RT&W");
// return to normal mode
doCommand(comPort, "RTZ");
}
// write it
doCommand(comPort, "AT&W");
// return to normal mode
doCommand(comPort, "ATZ");
}
lbl_status.Text = "Done";
}
else
{
// return to normal mode
doCommand(comPort, "ATZ");
lbl_status.Text = "Fail";
CustomMessageBox.Show("Failed to enter command mode");
}
comPort.Close();
}