本文整理汇总了C#中MissionPlanner.Comms.SerialPort.Close方法的典型用法代码示例。如果您正苦于以下问题:C# SerialPort.Close方法的具体用法?C# SerialPort.Close怎么用?C# SerialPort.Close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MissionPlanner.Comms.SerialPort
的用法示例。
在下文中一共展示了SerialPort.Close方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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();
}
示例2: BUT_savesettings_Click
//.........这里部分代码省略.........
//local
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)
{
if (controls[0].GetType() == typeof(CheckBox))
{
string value = ((CheckBox)controls[0]).Checked ? "1" : "0";
if (value != values[2].Trim())
{
string 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 (((ComboBox)controls[0]).SelectedValue != null)
{
if (((ComboBox)controls[0]).SelectedValue.ToString() != values[2].Trim())
{
string 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].Text != values[2].Trim() && controls[0].Text != "")
{
string cmdanswer = doCommand(comPort, "AT" + values[0].Trim() + "=" + controls[0].Text + "\r");
if (cmdanswer.Contains("OK"))
{
}
else
{
CustomMessageBox.Show("Set Command error");
}
}
}
}
}
}
}
}
// 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();
}
示例3: 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;
}
}
示例4: BUT_getcurrent_Click
//.........这里部分代码省略.........
var ans = Enum.Parse(typeof(mavlink_option), values[2].Trim());
((ComboBox)controls[0]).Text = ans.ToString();
}
else if (controls[0] is ComboBox)
{
((ComboBox)controls[0]).Text = values[2].Trim();
}
}
}
}
}
// remote
foreach (Control ctl in groupBox2.Controls)
{
if (ctl.Name.StartsWith("RS") && ctl.Name != "RSSI")
ctl.ResetText();
}
comPort.DiscardInBuffer();
lbl_status.Text = "Doing Command RTI5";
answer = doCommand(comPort, "RTI5");
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)
continue;
controls[0].Enabled = true;
if (controls[0] is CheckBox)
{
((CheckBox)controls[0]).Checked = values[2].Trim() == "1";
}
else if (controls[0] is TextBox)
{
((TextBox)controls[0]).Text = values[2].Trim();
}
else if (controls[0].Name.Contains("S6")) //
{
var ans = Enum.Parse(typeof(mavlink_option), values[2].Trim());
((ComboBox)controls[0]).Text = ans.ToString();
}
else if (controls[0] is ComboBox)
{
((ComboBox)controls[0]).Text = values[2].Trim();
}
}
else
{
/*
if (item.Contains("S15"))
{
answer = doCommand(comPort, "RTS15?");
int rts15 = 0;
if (int.TryParse(answer, out rts15))
{
RS15.Enabled = true;
RS15.Text = rts15.ToString();
}
}
*/
log.Info("Odd config line :" + item);
}
}
}
// off hook
doCommand(comPort, "ATO");
lbl_status.Text = "Done";
}
else
{
// off hook
doCommand(comPort, "ATO");
lbl_status.Text = "Fail";
CustomMessageBox.Show("Failed to enter command mode");
}
comPort.Close();
BUT_Syncoptions.Enabled = true;
BUT_savesettings.Enabled = true;
}
示例5: 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);
//.........这里部分代码省略.........
示例6: BUT_getcurrent_Click
//.........这里部分代码省略.........
catch
{
}
txt_Raeskey.Text = doCommand(comPort, "RT&E?").Trim();
lbl_status.Text = "Doing Command RTI5";
answer = doCommand(comPort, "RTI5", true);
items = answer.Split('\n');
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)
continue;
controls[0].Enabled = true;
if (controls[0] is CheckBox)
{
((CheckBox) controls[0]).Checked = values[2].Trim() == "1";
}
else if (controls[0] is TextBox)
{
((TextBox) controls[0]).Text = values[2].Trim();
}
else if (controls[0].Name.Contains("MAVLINK")) //
{
var ans = Enum.Parse(typeof (mavlink_option), values[2].Trim());
((ComboBox) controls[0]).Text = ans.ToString();
}
else if (controls[0] is ComboBox)
{
((ComboBox) controls[0]).Text = values[2].Trim();
}
}
else
{
/*
if (item.Contains("S15"))
{
answer = doCommand(comPort, "RTS15?");
int rts15 = 0;
if (int.TryParse(answer, out rts15))
{
RS15.Enabled = true;
RS15.Text = rts15.ToString();
}
}
*/
log.Info("Odd config line :" + item);
}
}
}
// off hook
doCommand(comPort, "ATO");
lbl_status.Text = "Done";
}
else
{
// off hook
doCommand(comPort, "ATO");
lbl_status.Text = "Fail";
CustomMessageBox.Show("Failed to enter command mode");
}
comPort.Close();
BUT_Syncoptions.Enabled = true;
BUT_savesettings.Enabled = true;
}
catch (Exception ex)
{
try
{
if (comPort != null)
comPort.Close();
}
catch
{
}
lbl_status.Text = "Error";
CustomMessageBox.Show("Error during read " + ex);
}
}
示例7: BUT_savesettings_Click
//.........这里部分代码省略.........
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");
}
}
}
}
}
}
}
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();
}
示例8: UploadFW
private void UploadFW(bool custom = false)
{
ICommsSerial comPort = new SerialPort();
var uploader = new Uploader();
if (MainV2.comPort.BaseStream.IsOpen)
{
try
{
getTelemPortWithRadio(ref comPort);
uploader.PROG_MULTI_MAX = 64;
}
catch (Exception ex)
{
CustomMessageBox.Show("Error " + ex);
}
}
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
var iHex = new IHex();
iHex.LogEvent += iHex_LogEvent;
iHex.ProgressEvent += iHex_ProgressEvent;
var bootloadermode = false;
// attempt bootloader mode
try
{
if (upload_xmodem(comPort))
{
comPort.Close();
return;
}
comPort.BaudRate = 115200;
uploader_ProgressEvent(0);
uploader_LogEvent("Trying Bootloader Mode");
uploader.port = comPort;
uploader.connect_and_sync();
uploader.ProgressEvent += uploader_ProgressEvent;
uploader.LogEvent += uploader_LogEvent;
uploader_LogEvent("In Bootloader Mode");
bootloadermode = true;
}
catch (Exception ex1)
{
log.Error(ex1);
// 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 += uploader_ProgressEvent;
uploader.LogEvent += 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))
{
//.........这里部分代码省略.........
示例9: 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 "";
}