本文整理汇总了C#中MissionPlanner.Comms.SerialPort类的典型用法代码示例。如果您正苦于以下问题:C# SerialPort类的具体用法?C# SerialPort怎么用?C# SerialPort使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SerialPort类属于MissionPlanner.Comms命名空间,在下文中一共展示了SerialPort类的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AP_GPS_COMNAV
Stream port = null;// File.Open(@"C:\Users\hog\Desktop\gps data\asterx-m", FileMode.Open);
public AP_GPS_COMNAV()
{
var sport = new SerialPort("COM13", 115200);
sport.Open();
port = sport.BaseStream;
}
示例2: BUT_savesettings_Click
private void BUT_savesettings_Click(object sender, EventArgs e)
{
ICommsSerial comPort = new SerialPort();
try
{
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
string answer = doCommand(comPort, "RTI5");
string[] items = answer.Split('\n');
foreach (string item in items)
{
if (item.StartsWith("S"))
{
string[] values = item.Split(':', '=');
if (values.Length == 3)
{
Control[] controls = this.Controls.Find("R" + values[0].Trim(), true);
if (controls.Length > 0)
{
if (controls[0].GetType() == typeof(CheckBox))
{
string value = ((CheckBox)controls[0]).Checked ? "1" : "0";
if (value != values[2].Trim())
{
string cmdanswer = doCommand(comPort, "RT" + values[0].Trim() + "=" + value + "\r");
if (cmdanswer.Contains("OK"))
{
}
else
{
CustomMessageBox.Show("Set Command error");
}
}
}
else
{
if (controls[0] is TextBox)
{
}
else
{
if (((ComboBox)controls[0]).SelectedValue != null)
{
if (((ComboBox)controls[0]).SelectedValue.ToString() != values[2].Trim())
{
string 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].Text != values[2].Trim() && controls[0].Text != "")
{
string cmdanswer = doCommand(comPort, "RT" + values[0].Trim() + "=" + controls[0].Text + "\r");
if (cmdanswer.Contains("OK"))
{
}
else
{
CustomMessageBox.Show("Set Command error");
//.........这里部分代码省略.........
示例3: BUT_resettodefault_Click
private void BUT_resettodefault_Click(object sender, EventArgs e)
{
ICommsSerial comPort = new SerialPort();
try
{
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 ATI & AT&F";
doCommand(comPort, "AT&F");
doCommand(comPort, "AT&W");
lbl_status.Text = "Reset";
doCommand(comPort, "ATZ");
comPort.Close();
BUT_getcurrent_Click(sender, e);
}
else
{
// off hook
doCommand(comPort, "ATO");
lbl_status.Text = "Fail";
CustomMessageBox.Show("Failed to enter command mode");
}
if (comPort.IsOpen)
comPort.Close();
}
示例4: BUT_getcurrent_Click
private void BUT_getcurrent_Click(object sender, EventArgs e)
{
ICommsSerial comPort = new SerialPort();
try
{
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 ATI & RTI";
ATI.Text = doCommand(comPort, "ATI");
RTI.Text = doCommand(comPort, "RTI");
uploader.Uploader.Frequency freq = (uploader.Uploader.Frequency)Enum.Parse(typeof(uploader.Uploader.Frequency), doCommand(comPort, "ATI3"));
uploader.Uploader.Board board = (uploader.Uploader.Board)Enum.Parse(typeof(uploader.Uploader.Board), doCommand(comPort, "ATI2"));
ATI3.Text = freq.ToString();
ATI2.Text = board.ToString();
try
{
RTI2.Text = ((uploader.Uploader.Board)Enum.Parse(typeof(uploader.Uploader.Board), doCommand(comPort, "RTI2"))).ToString();
}
catch { }
// 8 and 9
if (freq == uploader.Uploader.Frequency.FREQ_915)
{
S8.DataSource = Range(895000, 1000, 935000);
RS8.DataSource = Range(895000, 1000, 935000);
S9.DataSource = Range(895000, 1000, 935000);
RS9.DataSource = Range(895000, 1000, 935000);
}
else if (freq == uploader.Uploader.Frequency.FREQ_433)
{
S8.DataSource = Range(414000, 100, 454000);
RS8.DataSource = Range(414000, 100, 454000);
S9.DataSource = Range(414000, 100, 454000);
RS9.DataSource = Range(414000, 100, 454000);
}
else if (freq == uploader.Uploader.Frequency.FREQ_868)
{
S8.DataSource = Range(849000, 1000, 889000);
RS8.DataSource = Range(849000, 1000, 889000);
S9.DataSource = Range(849000, 1000, 889000);
RS9.DataSource = Range(849000, 1000, 889000);
}
if (board == uploader.Uploader.Board.DEVICE_ID_RFD900 || board == uploader.Uploader.Board.DEVICE_ID_RFD900A)
{
S4.DataSource = Range(1, 1, 30);
RS4.DataSource = Range(1, 1, 30);
}
RSSI.Text = doCommand(comPort, "ATI7").Trim();
lbl_status.Text = "Doing Command ATI5";
string answer = doCommand(comPort, "ATI5");
string[] items = answer.Split('\n');
foreach (string item in items)
{
if (item.StartsWith("S"))
{
string[] values = item.Split(':', '=');
if (values.Length == 3)
{
Control[] controls = this.Controls.Find(values[0].Trim(), true);
if (controls.Length > 0)
{
controls[0].Enabled = true;
if (controls[0] is CheckBox)
{
((CheckBox)controls[0]).Checked = values[2].Trim() == "1";
}
//.........这里部分代码省略.........
示例5: UploadFW
private void UploadFW(bool custom = false)
{
ICommsSerial comPort = new SerialPort();
uploader.Uploader uploader = new uploader.Uploader();
if (MainV2.comPort.BaseStream.IsOpen)
{
try
{
comPort = new MAVLinkSerialPort(MainV2.comPort, 0);//MAVLink.SERIAL_CONTROL_DEV.TELEM1);
uploader.PROG_MULTI_MAX = 64;
}
catch (Exception ex)
{
CustomMessageBox.Show("Error " + ex.ToString());
}
}
try
{
comPort.PortName = MainV2.comPort.BaseStream.PortName;
comPort.BaudRate = 115200;
comPort.Open();
}
catch { CustomMessageBox.Show("Invalid ComPort or in use"); return; }
// prep what we are going to upload
uploader.IHex iHex = new uploader.IHex();
iHex.LogEvent += new LogEventHandler(iHex_LogEvent);
iHex.ProgressEvent += new ProgressEventHandler(iHex_ProgressEvent);
bool bootloadermode = false;
// attempt bootloader mode
try
{
uploader_ProgressEvent(0);
uploader_LogEvent("Trying Bootloader Mode");
uploader.port = comPort;
uploader.connect_and_sync();
uploader.ProgressEvent += new ProgressEventHandler(uploader_ProgressEvent);
uploader.LogEvent += new LogEventHandler(uploader_LogEvent);
uploader_LogEvent("In Bootloader Mode");
bootloadermode = true;
}
catch
{
// cleanup bootloader mode fail, and try firmware mode
comPort.Close();
if (MainV2.comPort.BaseStream.IsOpen)
{
// default baud... guess
comPort.BaudRate = 57600;
} else {
comPort.BaudRate = MainV2.comPort.BaseStream.BaudRate;
}
try
{
comPort.Open();
}
catch { CustomMessageBox.Show("Error opening port", "Error"); return; }
uploader.ProgressEvent += new ProgressEventHandler(uploader_ProgressEvent);
uploader.LogEvent += new LogEventHandler(uploader_LogEvent);
uploader_LogEvent("Trying Firmware Mode");
bootloadermode = false;
}
// check for either already bootloadermode, or if we can do a ATI to ID the firmware
if (bootloadermode || doConnect(comPort))
{
// put into bootloader mode/udpate mode
if (!bootloadermode)
{
try
{
comPort.Write("AT&UPDATE\r\n");
string left = comPort.ReadExisting();
log.Info(left);
Sleep(700);
comPort.BaudRate = 115200;
}
catch { }
}
// force sync after changing baudrate
uploader.connect_and_sync();
global::uploader.Uploader.Board device = global::uploader.Uploader.Board.FAILED;
global::uploader.Uploader.Frequency freq = global::uploader.Uploader.Frequency.FAILED;
//.........这里部分代码省略.........
示例6: upload
/// <summary>
/// Upload the specified image_data.
/// </summary>
/// <param name='image_data'>
/// Image_data to be uploaded.
/// </param>
public void upload(SerialPort on_port, IHex image_data)
{
progress (0);
port = on_port;
try {
connect_and_sync ();
upload_and_verify (image_data);
cmdReboot ();
} catch {
if (port.IsOpen)
port.Close ();
throw;
}
}
示例7: readpacket
/// <summary>
///
/// </summary>
/// <param name="serialPort"></param>
/// <returns></returns>
static byte[] readpacket(SerialPort serialPort)
{
byte[] temp = new byte[4000];
byte[] mes = new byte[2] { 0x0, 0xC0 }; // fail
int a = 7;
int count = 0;
serialPort.ReadTimeout = 1000;
while (count < a)
{
//Console.WriteLine("count {0} a {1} mes leng {2}",count,a,mes.Length);
try
{
temp[count] = (byte)serialPort.ReadByte();
}
catch { break; }
//Console.Write("{1}", temp[0], (char)temp[0]);
if (temp[0] != 0x1b)
{
count = 0;
continue;
}
if (count == 3)
{
a = (temp[2] << 8) + temp[3];
mes = new byte[a];
a += 5;
}
if (count >= 5)
{
mes[count - 5] = temp[count];
}
count++;
}
//Console.WriteLine("read ck");
try
{
temp[count] = (byte)serialPort.ReadByte();
}
catch { }
count++;
Array.Resize<byte>(ref temp, count);
//Console.WriteLine(this.BytesToRead);
return mes;
}
示例8: genstkv2packet
/// <summary>
/// STK v2 generate packet
/// </summary>
/// <param name="serialPort"></param>
/// <param name="message"></param>
/// <returns></returns>
static byte[] genstkv2packet(SerialPort serialPort, byte[] message)
{
byte[] data = new byte[300];
byte ck = 0;
data[0] = 0x1b;
ck ^= data[0];
data[1] = 0x1;
ck ^= data[1];
data[2] = (byte)((message.Length >> 8) & 0xff);
ck ^= data[2];
data[3] = (byte)(message.Length & 0xff);
ck ^= data[3];
data[4] = 0xe;
ck ^= data[4];
int a = 5;
foreach (byte let in message)
{
data[a] = let;
ck ^= let;
a++;
}
data[a] = ck;
a++;
serialPort.Write(data, 0, a);
//Console.WriteLine("about to read packet");
byte[] ret = BoardDetect.readpacket(serialPort);
//if (ret[1] == 0x0)
{
//Console.WriteLine("received OK");
}
return ret;
}
示例9: DetectBoard
/// <summary>
/// Detects APM board version
/// </summary>
/// <param name="port"></param>
/// <returns> (1280/2560/2560-2/px4/px4v2)</returns>
public static boards DetectBoard(string port)
{
SerialPort serialPort = new SerialPort();
serialPort.PortName = port;
if (!MainV2.MONO)
{
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_SerialPort"); // Win32_USBControllerDevice
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
foreach (ManagementObject obj2 in searcher.Get())
{
Console.WriteLine("PNPID: " + obj2.Properties["PNPDeviceID"].Value.ToString());
// check vid and pid
if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_2341&PID_0010"))
{
// check port name as well
if (obj2.Properties["Name"].Value.ToString().ToUpper().Contains(serialPort.PortName.ToUpper()))
{
log.Info("is a 2560-2");
return boards.b2560v2;
}
}
if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0010"))
{
// check port name as well
//if (obj2.Properties["Name"].Value.ToString().ToUpper().Contains(serialPort.PortName.ToUpper()))
{
log.Info("is a px4");
return boards.px4;
}
}
if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0011"))
{
log.Info("is a px4v2");
return boards.px4v2;
}
if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0001"))
{
log.Info("is a px4v2 bootloader");
return boards.px4v2;
}
if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0016"))
{
log.Info("is a px4v2 bootloader");
CustomMessageBox.Show("You appear to have a bootloader with a bad PID value, please update your bootloader.");
return boards.px4v2;
}
//|| obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0012") || obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0013") || obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0014") || obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0015") || obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0016")
}
}
else
{
// if its mono
if (DialogResult.Yes == CustomMessageBox.Show("Is this a APM 2+?", "APM 2+", MessageBoxButtons.YesNo))
{
return boards.b2560v2;
}
else
{
if (DialogResult.Yes == CustomMessageBox.Show("Is this a PX4/PIXHAWK?", "PX4/PIXHAWK", MessageBoxButtons.YesNo))
{
if (DialogResult.Yes == CustomMessageBox.Show("Is this a PIXHAWK?", "PIXHAWK", MessageBoxButtons.YesNo))
{
return boards.px4v2;
}
return boards.px4;
}
else
{
return boards.b2560;
}
}
}
if (serialPort.IsOpen)
serialPort.Close();
serialPort.DtrEnable = true;
serialPort.BaudRate = 57600;
serialPort.Open();
Thread.Sleep(100);
int a = 0;
while (a < 20) // 20 * 50 = 1 sec
{
//Console.WriteLine("write " + DateTime.Now.Millisecond);
//.........这里部分代码省略.........
示例10: BUT_getcurrent_Click
private void BUT_getcurrent_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";
try
{
if (doConnect(comPort))
{
// cleanup
doCommand(comPort, "AT&T");
comPort.DiscardInBuffer();
lbl_status.Text = "Doing Command ATI & RTI";
ATI.Text = doCommand(comPort, "ATI");
var freq =
(Uploader.Frequency)
Enum.Parse(typeof (Uploader.Frequency), doCommand(comPort, "ATI3"));
var board =
(Uploader.Board)
Enum.Parse(typeof (Uploader.Board), doCommand(comPort, "ATI2"));
ATI3.Text = freq.ToString();
ATI2.Text = board.ToString();
// 8 and 9
if (freq == Uploader.Frequency.FREQ_915)
{
MIN_FREQ.DataSource = Range(895000, 1000, 935000);
RMIN_FREQ.DataSource = Range(895000, 1000, 935000);
MAX_FREQ.DataSource = Range(895000, 1000, 935000);
RMAX_FREQ.DataSource = Range(895000, 1000, 935000);
}
else if (freq == Uploader.Frequency.FREQ_433)
{
MIN_FREQ.DataSource = Range(414000, 50, 460000);
RMIN_FREQ.DataSource = Range(414000, 50, 460000);
MAX_FREQ.DataSource = Range(414000, 50, 460000);
RMAX_FREQ.DataSource = Range(414000, 50, 460000);
}
else if (freq == Uploader.Frequency.FREQ_868)
{
MIN_FREQ.DataSource = Range(849000, 1000, 889000);
RMIN_FREQ.DataSource = Range(849000, 1000, 889000);
MAX_FREQ.DataSource = Range(849000, 1000, 889000);
RMAX_FREQ.DataSource = Range(849000, 1000, 889000);
}
if (board == Uploader.Board.DEVICE_ID_RFD900 ||
board == Uploader.Board.DEVICE_ID_RFD900A
|| board == Uploader.Board.DEVICE_ID_RFD900P ||
board == Uploader.Board.DEVICE_ID_RFD900U ||
board == Uploader.Board.DEVICE_ID_RFD900Plus)
{
TXPOWER.DataSource = Range(1, 1, 30);
RTXPOWER.DataSource = Range(1, 1, 30);
}
txt_aeskey.Text = doCommand(comPort, "AT&E?").Trim();
RSSI.Text = doCommand(comPort, "ATI7").Trim();
lbl_status.Text = "Doing Command ATI5";
var answer = doCommand(comPort, "ATI5", true);
var items = answer.Split('\n');
foreach (var item in items)
{
//.........这里部分代码省略.........
示例11: BUT_savesettings_Click
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";
// set encryption keys at the same time, so if we are enabled we dont lose comms.
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 != "")
{
// 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
{
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"))
{
}
//.........这里部分代码省略.........
示例12: DetectBoard
/// <summary>
/// Detects APM board version
/// </summary>
/// <param name="port"></param>
/// <returns> (1280/2560/2560-2/px4/px4v2)</returns>
public static boards DetectBoard(string port)
{
SerialPort serialPort = new SerialPort();
serialPort.PortName = port;
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_SerialPort"); // Win32_USBControllerDevice
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
foreach (ManagementObject obj2 in searcher.Get())
{
Console.WriteLine("PNPID: " + obj2.Properties["PNPDeviceID"].Value.ToString());
if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0002"))
{
log.Info("is a playuav osd bootloader");
return boards.playuavosd;
}
}
return boards.none;
}
示例13: DetectVersion
/// <summary>
/// detects STK version 1 or 2
/// </summary>
/// <param name="port">comportname</param>
/// <returns>string either (1280/2560) or "" for none</returns>
public static string DetectVersion(string port)
{
SerialPort serialPort = new SerialPort();
serialPort.PortName = port;
if (serialPort.IsOpen)
serialPort.Close();
serialPort.DtrEnable = true;
serialPort.BaudRate = 57600;
serialPort.Open();
serialPort.toggleDTR();
Thread.Sleep(100);
int a = 0;
while (a < 20) // 20 * 50 = 1 sec
{
//Console.WriteLine("write " + DateTime.Now.Millisecond);
serialPort.DiscardInBuffer();
serialPort.Write(new byte[] { (byte)'0', (byte)' ' }, 0, 2);
a++;
Thread.Sleep(50);
//Console.WriteLine("btr {0}", serialPort.BytesToRead);
if (serialPort.BytesToRead >= 2)
{
byte b1 = (byte)serialPort.ReadByte();
byte b2 = (byte)serialPort.ReadByte();
if (b1 == 0x14 && b2 == 0x10)
{
serialPort.Close();
log.Info("is a 1280");
return "1280";
}
}
}
serialPort.Close();
log.Warn("Not a 1280");
Thread.Sleep(500);
serialPort.DtrEnable = true;
serialPort.BaudRate = 115200;
serialPort.Open();
serialPort.toggleDTR();
Thread.Sleep(100);
a = 0;
while (a < 4)
{
byte[] temp = new byte[] { 0x6, 0, 0, 0, 0 };
temp = ArduinoDetect.genstkv2packet(serialPort, temp);
a++;
Thread.Sleep(50);
try
{
if (temp[0] == 6 && temp[1] == 0 && temp.Length == 2)
{
serialPort.Close();
log.Info("is a 2560");
return "2560";
}
}
catch
{
}
}
serialPort.Close();
log.Warn("Not a 2560");
return "";
}
示例14: BUT_savesettings_Click
private void BUT_savesettings_Click(object sender, EventArgs e)
{
ICommsSerial comPort = new SerialPort();
try
{
if (MainV2.comPort.BaseStream.IsOpen)
{
comPort = new MAVLinkSerialPort(MainV2.comPort, 0); //MAVLink.SERIAL_CONTROL_DEV.TELEM1);
comPort.BaudRate = 57600;
}
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)
{
var controls = Controls.Find("R" + values[0].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
{
CustomMessageBox.Show("Set Command error");
}
}
}
else if (controls[0] is TextBox)
{
}
else if (controls[0].Name.Contains("S6")) //
{
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())
{
//.........这里部分代码省略.........