本文整理汇总了C#中SerialPort.DiscardInBuffer方法的典型用法代码示例。如果您正苦于以下问题:C# SerialPort.DiscardInBuffer方法的具体用法?C# SerialPort.DiscardInBuffer怎么用?C# SerialPort.DiscardInBuffer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SerialPort
的用法示例。
在下文中一共展示了SerialPort.DiscardInBuffer方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ExecCommand
public string ExecCommand(SerialPort port, string command, int responseTimeout, string errorMessage)
{
try
{
port.DiscardOutBuffer();
port.DiscardInBuffer();
port.Write(command + "\r");
return "Success";
}
catch (Exception ex)
{
throw ex;
}
}
示例2: BUT_getcurrent_Click
private void BUT_getcurrent_Click(object sender, EventArgs e)
{
ArdupilotMega.Comms.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))
{
comPort.DiscardInBuffer();
lbl_status.Text = "Doing Command ATI & RTI";
ATI.Text = doCommand(comPort, "ATI1").Trim();
RTI.Text = doCommand(comPort, "RTI1").Trim();
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(), false);
if (controls.Length > 0)
{
if (controls[0].GetType() == typeof(CheckBox))
{
((CheckBox)controls[0]).Checked = values[2].Trim() == "1";
}
else
{
controls[0].Text = values[2].Trim();
}
}
}
}
}
// remote
foreach (Control ctl in this.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(), false);
if (controls.Length == 0)
continue;
if (controls[0].GetType() == typeof(CheckBox))
{
((CheckBox)controls[0]).Checked = values[2].Trim() == "1";
}
else if (controls[0].GetType() == typeof(TextBox))
{
((TextBox)controls[0]).Text = values[2].Trim();
}
else if (controls[0].GetType() == typeof(ComboBox))
{
((ComboBox)controls[0]).SelectedText = values[2].Trim();
//.........这里部分代码省略.........
示例3: BUT_savesettings_Click
private void BUT_savesettings_Click(object sender, EventArgs e)
{
ArdupilotMega.Comms.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))
{
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(), false);
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].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");
}
}
}
}
}
}
}
// write it
doCommand(comPort, "RT&W");
// return to normal mode
doCommand(comPort, "RTZ");
Sleep(100);
}
comPort.DiscardInBuffer();
{
//local
string answer = doCommand(comPort, "ATI5");
string[] items = answer.Split('\n');
foreach (string item in items)
//.........这里部分代码省略.........
示例4: request_DoWork
public void request_DoWork(object sender, DoWorkEventArgs e)
{
var worker = (BackgroundWorker)sender;
worker.ReportProgress(0);
ClsComSettingMain clsComSettingMain = (ClsComSettingMain)e.Argument;
string comPort = clsComSettingMain.comport;
int baudRate = clsComSettingMain.baudRate;
if (comPort != null && baudRate != 0)
{
SerialPort serialPort = new SerialPort(comPort, baudRate);
serialPort.DataReceived += new SerialDataReceivedEventHandler(process_DoWorkHandler);
serialPort.Open();
while (true)
{
if (worker.CancellationPending)
{
worker.ReportProgress(100);
e.Cancel = true;
break;
}
else
{
serialPort.DiscardInBuffer(); // Eingangspuffer leeren
serialPort.Write(adv_request, 0, 3); // Write byte array to serial port, with no offset, all 3 bytes
}
}
}
}
示例5: BUT_resettodefault_Click
private void BUT_resettodefault_Click(object sender, EventArgs e)
{
ArdupilotMega.Comms.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();
}
示例6: BUT_getcurrent_Click
private void BUT_getcurrent_Click(object sender, EventArgs e)
{
ArdupilotMega.Comms.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.Code freq = (uploader.Uploader.Code)Enum.Parse(typeof(uploader.Uploader.Code), doCommand(comPort, "ATI3"));
uploader.Uploader.Code board = (uploader.Uploader.Code)Enum.Parse(typeof(uploader.Uploader.Code), doCommand(comPort, "ATI2"));
ATI3.Text = freq.ToString();
// 8 and 9
if (freq == uploader.Uploader.Code.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.Code.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.Code.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.Code.DEVICE_ID_RFD900 || board == uploader.Uploader.Code.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].GetType() == typeof(CheckBox))
{
((CheckBox)controls[0]).Checked = values[2].Trim() == "1";
}
else
{
controls[0].Text = values[2].Trim();
}
}
}
}
}
//.........这里部分代码省略.........
示例7: checkOpen
/// <summary>
/// Verify if the serial port is opened and opens it if necessary
/// </summary>
/// <returns><c>true</c>, if port is opened, <c>false</c> otherwise.</returns>
/// <param name="portSpeed">Port speed.</param>
public static bool checkOpen (int portSpeed = 9600)
{
if (s_serial == null) {
//string portName = GetPortName ();
if (portName == "") {
print ("Error: Couldn't find serial port.");
return false;
} else {
if (s_debug)
print("Opening serial port: " + portName);
}
s_serial = new SerialPort (portName, portSpeed);
try {
s_serial.Open ();
} catch(System.Exception e) {
print ("Failed to open");
return false;
}
//print ("default ReadTimeout: " + s_serial.ReadTimeout);
//s_serial.ReadTimeout = 10;
// clear input buffer from previous garbage
s_serial.DiscardInBuffer ();
}
print ("Successfully opened " + s_serial.PortName);
return s_serial.IsOpen;
}
示例8: checkOpen
/// <summary>
/// Verify if the serial port is opened and opens it if necessary
/// </summary>
/// <returns><c>true</c>, if port is opened, <c>false</c> otherwise.</returns>
/// <param name="portSpeed">Port speed.</param>
public static bool checkOpen(int portSpeed = 9600)
{
if (s_serial == null) {
string portName = GetPortName ();
if (portName == "") {
print ("Error: Couldn't find serial port.");
return false;
} else {
if (s_debug)
print("Opening serial port: " + portName);
}
s_serial = new SerialPort (portName, portSpeed);
s_serial.Open ();
//print ("default ReadTimeout: " + s_serial.ReadTimeout);
//s_serial.ReadTimeout = 10;
// clear input buffer from previous garbage
s_serial.DiscardInBuffer ();
}
return s_serial.IsOpen;
}