本文整理汇总了C#中ICommsSerial.DiscardInBuffer方法的典型用法代码示例。如果您正苦于以下问题:C# ICommsSerial.DiscardInBuffer方法的具体用法?C# ICommsSerial.DiscardInBuffer怎么用?C# ICommsSerial.DiscardInBuffer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ICommsSerial
的用法示例。
在下文中一共展示了ICommsSerial.DiscardInBuffer方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: doCommand
public string doCommand(ICommsSerial comPort, string cmd, int level = 0)
{
if (!comPort.IsOpen)
return "";
comPort.ReadTimeout = 1000;
// setup to known state
comPort.Write("\r\n");
// alow some time to gather thoughts
Sleep(100);
// ignore all existing data
comPort.DiscardInBuffer();
lbl_status.Text = "Doing Command " + cmd;
log.Info("Doing Command " + cmd);
// write command
comPort.Write(cmd + "\r\n");
// read echoed line or existing data
string temp;
try
{
temp = Serial_ReadLine(comPort);
}
catch { temp = comPort.ReadExisting(); }
log.Info("cmd " + cmd + " echo " + temp);
// delay for command
Sleep(500);
// get responce
string ans = "";
while (comPort.BytesToRead > 0)
{
try
{
ans = ans + Serial_ReadLine(comPort) + "\n";
}
catch { ans = ans + comPort.ReadExisting() + "\n"; }
Sleep(50);
if (ans.Length > 500)
{
break;
}
}
log.Info("responce " + level + " " + ans.Replace('\0', ' '));
Regex pattern = new Regex(@"^\[([0-9+])\]\s+",RegexOptions.Multiline);
if (pattern.IsMatch(ans))
{
Match mat = pattern.Match(ans);
ans = pattern.Replace(ans,"");
}
// try again
if (ans == "" && level == 0)
return doCommand(comPort, cmd, 1);
return ans;
}
示例2: doConnect
public bool doConnect(ICommsSerial comPort)
{
try
{
// clear buffer
comPort.DiscardInBuffer();
// setup a known enviroment
comPort.Write("\r\n");
// wait
Sleep(1100);
// send config string
comPort.Write("+++");
// wait
Sleep(1100);
// check for config responce "OK"
log.Info("Connect btr " + comPort.BytesToRead + " baud " + comPort.BaudRate);
string conn = comPort.ReadExisting();
log.Info("Connect first responce " + conn.Replace('\0', ' ') + " " + conn.Length);
if (conn.Contains("OK"))
{
//return true;
}
else
{
// cleanup incase we are already in cmd mode
comPort.Write("\r\n");
}
doCommand(comPort, "AT&T");
string version = doCommand(comPort, "ATI");
log.Info("Connect Version: " + version.Trim() + "\n");
Regex regex = new Regex(@"SiK\s+(.*)\s+on\s+(.*)");
if (regex.IsMatch(version))
{
return true;
}
return false;
}
catch { return false; }
}
示例3: doConnect
public bool doConnect(ICommsSerial comPort)
{
try
{
Console.WriteLine("doConnect");
// setup a known enviroment
comPort.Write("ATO\r\n");
// wait
Sleep(1100, comPort);
comPort.DiscardInBuffer();
// send config string
comPort.Write("+++");
Sleep(1100,comPort);
// check for config response "OK"
log.Info("Connect btr " + comPort.BytesToRead + " baud " + comPort.BaudRate);
// allow time for data/response
byte[] buffer = new byte[20];
int len = comPort.Read(buffer, 0, buffer.Length);
string conn = ASCIIEncoding.ASCII.GetString(buffer, 0, len);
log.Info("Connect first response " + conn.Replace('\0', ' ') + " " + conn.Length);
if (conn.Contains("OK"))
{
//return true;
}
else
{
// cleanup incase we are already in cmd mode
comPort.Write("\r\n");
}
doCommand(comPort, "AT&T");
string version = doCommand(comPort, "ATI");
log.Info("Connect Version: " + version.Trim() + "\n");
Regex regex = new Regex(@"SiK\s+(.*)\s+on\s+(.*)");
if (regex.IsMatch(version))
{
return true;
}
return false;
}
catch { return false; }
}
示例4: doCommand
public string doCommand(ICommsSerial comPort, string cmd, int level = 0)
{
if (!comPort.IsOpen)
return "";
comPort.ReadTimeout = 1000;
comPort.DiscardInBuffer();
// setup to known state
comPort.Write("\r\n");
try
{
var temp1 = Serial_ReadLine(comPort);
}
catch
{
try
{
comPort.ReadExisting();
}
catch { return ""; }
}
Sleep(100);
comPort.DiscardInBuffer();
lbl_status.Text = "Doing Command " + cmd;
log.Info("Doing Command " + cmd);
// write command
comPort.Write(cmd + "\r\n");
// read echoed line or existing data
string temp;
try
{
temp = Serial_ReadLine(comPort);
}
catch { temp = comPort.ReadExisting(); }
log.Info("cmd " + cmd + " echo " + temp);
// get response
string ans = "";
DateTime deadline = DateTime.Now.AddMilliseconds(200);
while (comPort.BytesToRead > 0 || DateTime.Now < deadline)
{
try
{
ans = ans + Serial_ReadLine(comPort) + "\n";
}
catch { ans = ans + comPort.ReadExisting() + "\n"; }
Sleep(50);
if (ans.Length > 1024)
{
break;
}
}
log.Info("response " + level + " " + ans.Replace('\0', ' '));
Regex pattern = new Regex(@"^\[([0-9+])\]\s+", RegexOptions.Multiline);
if (pattern.IsMatch(ans))
{
Match mat = pattern.Match(ans);
ans = pattern.Replace(ans, "");
}
// try again
if (ans == "" && level == 0)
return doCommand(comPort, cmd, 1);
return ans;
}
示例5: Terminal_Load
private void Terminal_Load(object sender, EventArgs e)
{
try
{
MainV2.comPort.giveComport = true;
comPort = MainV2.comPort.BaseStream;
if (comPort.IsOpen)
comPort.Close();
comPort.ReadBufferSize = 1024 * 1024;
comPort.PortName = MainV2.comPortName;
comPort.BaudRate = int.Parse(MainV2._connectionControl.CMB_baudrate.Text);
comPort.Open();
comPort.toggleDTR();
comPort.DiscardInBuffer();
Console.WriteLine("Terminal_Load");
System.Threading.Thread t11 = new System.Threading.Thread(delegate()
{
threadrun = true;
Console.WriteLine("Terminal thread start");
// 10 sec
waitandsleep(10000);
Console.WriteLine("Terminal thread 1");
// 100 ms
readandsleep(100);
Console.WriteLine("Terminal thread 2");
try
{
if (!inlogview)
comPort.Write("\n\n\n");
// 1 secs
if (!inlogview)
readandsleep(1000);
if (!inlogview)
comPort.Write("\r\r\r?\r");
}
catch (Exception ex) { Console.WriteLine("Terminal thread 3 " + ex.ToString()); threadrun = false; return; }
Console.WriteLine("Terminal thread 3");
while (threadrun)
{
try
{
System.Threading.Thread.Sleep(10);
if (inlogview)
continue;
if (!comPort.IsOpen)
break;
if (comPort.BytesToRead > 0)
{
comPort_DataReceived((object)null, (SerialDataReceivedEventArgs)null);
}
}
catch (Exception ex) { Console.WriteLine("Terminal thread 4 " + ex.ToString()); }
}
threadrun = false;
try
{
comPort.DtrEnable = false;
}
catch { }
try
{
Console.WriteLine("term thread close");
comPort.Close();
}
catch { }
Console.WriteLine("Comport thread close");
});
t11.IsBackground = true;
t11.Name = "Terminal serial thread";
t11.Start();
// doesnt seem to work on mac
//comPort.DataReceived += new SerialDataReceivedEventHandler(comPort_DataReceived);
TXT_terminal.AppendText("Opened com port\r\n");
inputStartPos = TXT_terminal.SelectionStart;
}
catch (Exception) { TXT_terminal.AppendText("Cant open serial port\r\n"); return; }
//.........这里部分代码省略.........
示例6: Log_Load
private void Log_Load(object sender, EventArgs e)
{
status = serialstatus.Connecting;
comPort = GCSViews.Terminal.comPort;
try
{
Console.WriteLine("Log_load " + comPort.IsOpen);
if (!comPort.IsOpen)
comPort.Open();
//Console.WriteLine("Log dtr");
//comPort.toggleDTR();
Console.WriteLine("Log discard");
comPort.DiscardInBuffer();
Console.WriteLine("Log w&sleep");
try
{
// try provoke a response
comPort.Write("\n\n?\r\n\n");
}
catch
{
}
// 10 sec
waitandsleep(10000);
}
catch (Exception ex)
{
log.Error("Error opening comport", ex);
CustomMessageBox.Show("Error opening comport");
return;
}
var t11 = new System.Threading.Thread(delegate()
{
var start = DateTime.Now;
threadrun = true;
if (comPort.IsOpen)
readandsleep(100);
try
{
if (comPort.IsOpen)
comPort.Write("\n\n\n\nexit\r\nlogs\r\n"); // more in "connecting"
}
catch
{
}
while (threadrun)
{
try
{
updateDisplay();
System.Threading.Thread.Sleep(1);
if (!comPort.IsOpen)
break;
while (comPort.BytesToRead >= 4)
{
comPort_DataReceived((object) null, (SerialDataReceivedEventArgs) null);
}
}
catch (Exception ex)
{
log.Error("crash in comport reader " + ex);
} // cant exit unless told to
}
log.Info("Comport thread close");
}) {Name = "comport reader", IsBackground = true};
t11.Start();
// doesnt seem to work on mac
//comPort.DataReceived += new SerialDataReceivedEventHandler(comPort_DataReceived);
}
示例7: doCommand
public string doCommand(ICommsSerial comPort, string cmd, bool multiLineResponce = false, int level = 0)
{
if (!comPort.IsOpen)
return "";
comPort.DiscardInBuffer();
lbl_status.Text = "Doing Command " + cmd;
log.Info("Doing Command " + cmd);
comPort.Write(cmd + "\r\n");
comPort.ReadTimeout = 1000;
// command echo
var cmdecho = Serial_ReadLine(comPort);
if (cmdecho.Contains(cmd))
{
var value = "";
if (multiLineResponce)
{
DateTime deadline = DateTime.Now.AddMilliseconds(1000);
while (comPort.BytesToRead > 0 || DateTime.Now < deadline)
{
try
{
value = value + Serial_ReadLine(comPort);
}
catch
{
value = value + comPort.ReadExisting();
}
}
}
else
{
value = Serial_ReadLine(comPort);
if (value == "" && level == 0)
{
return doCommand(comPort, cmd, multiLineResponce, 1);
}
}
log.Info(value.Replace('\0', ' '));
return value;
}
comPort.DiscardInBuffer();
// try again
if (level == 0)
return doCommand(comPort, cmd, multiLineResponce, 1);
return "";
}
示例8: start_Terminal
private void start_Terminal(bool px4)
{
try
{
MainV2.comPort.giveComport = true;
comPort = MainV2.comPort.BaseStream;
if (comPort.IsOpen)
{
threadrun = false;
// if (DialogResult.Cancel == CustomMessageBox.Show("The port is open\n Continue?", "Continue", MessageBoxButtons.YesNo))
{
// return;
}
comPort.Close();
// allow things to cleanup
System.Threading.Thread.Sleep(200);
}
comPort.ReadBufferSize = 1024 * 1024;
comPort.PortName = MainV2.comPortName;
// test moving baud rate line
log.Info("About to open " + comPort.PortName);
comPort.Open();
comPort.BaudRate = int.Parse(MainV2._connectionControl.CMB_baudrate.Text);
if (px4)
{
TXT_terminal.AppendText("Rebooting");
// check if we are a mavlink stream
byte[] buffer = MainV2.comPort.readPacket();
if (buffer.Length > 0)
{
log.Info("got packet - sending reboot via mavlink");
MainV2.comPort.giveComport = true;
MainV2.comPort.doReboot(false);
try
{
comPort.Close();
}
catch { }
}
else
{
log.Info("no packet - sending reboot via console");
MainV2.comPort.giveComport = true;
MainV2.comPort.BaseStream.Write("exit\rreboot\r");
try
{
comPort.Close();
}
catch { }
}
MainV2.comPort.giveComport = true;
TXT_terminal.AppendText("Waiting for reboot");
// wait 7 seconds for px4 reboot
log.Info("waiting for reboot");
DateTime deadline = DateTime.Now.AddSeconds(8);
while (DateTime.Now < deadline)
{
System.Threading.Thread.Sleep(100);
Application.DoEvents();
}
int a = 0;
// while (a < 5)
{
try
{
comPort.Open();
}
catch { }
System.Threading.Thread.Sleep(200);
a++;
}
}
else
{
comPort.toggleDTR();
}
try
{
comPort.DiscardInBuffer();
//.........这里部分代码省略.........
示例9: Log_Load
private void Log_Load(object sender, EventArgs e)
{
status = serialstatus.Connecting;
comPort = MainV2.comPort.BaseStream;
try
{
comPort.toggleDTR();
comPort.DiscardInBuffer();
// 10 sec
waitandsleep(10000);
}
catch (Exception ex)
{
log.Error("Error opening comport", ex);
CustomMessageBox.Show("Error opening comport");
}
var t11 = new System.Threading.Thread(delegate()
{
var start = DateTime.Now;
threadrun = true;
readandsleep(100);
try
{
comPort.Write("\n\n\n\n"); // more in "connecting"
}
catch
{
}
while (threadrun)
{
try
{
updateDisplay();
System.Threading.Thread.Sleep(10);
if (!comPort.IsOpen)
break;
while (comPort.BytesToRead >= 4)
{
comPort_DataReceived((object)null, (SerialDataReceivedEventArgs)null);
}
}
catch (Exception ex)
{
log.Error("crash in comport reader " + ex);
} // cant exit unless told to
}
log.Info("Comport thread close");
}) {Name = "comport reader"};
t11.Start();
// doesnt seem to work on mac
//comPort.DataReceived += new SerialDataReceivedEventHandler(comPort_DataReceived);
}
示例10: Log_Load
private void Log_Load(object sender, EventArgs e)
{
if (MainV2.config["log_isarducopter"] != null)
{
CHK_arducopter.Checked = bool.Parse(MainV2.config["log_isarducopter"].ToString());
CHK_arduplane.Checked = bool.Parse(MainV2.config["log_isarduplane"].ToString());
CHK_ardurover.Checked = bool.Parse(MainV2.config["log_isardurover"].ToString());
}
status = serialstatus.Connecting;
MainV2.comPort.giveComport = true;
comPort = MainV2.comPort.BaseStream;
comPort.DtrEnable = false;
comPort.RtsEnable = false;
try
{
Console.WriteLine("Log_load " + comPort.IsOpen);
if (!comPort.IsOpen)
comPort.Open();
Console.WriteLine("Log dtr");
comPort.toggleDTR();
Console.WriteLine("Log discard");
comPort.DiscardInBuffer();
Console.WriteLine("Log w&sleep");
try
{
// try provoke a responce
comPort.Write("\n\n?\r\n\n");
}
catch
{
}
// 10 sec
waitandsleep(10000);
}
catch (Exception ex)
{
log.Error("Error opening comport", ex);
CustomMessageBox.Show("Error opening comport");
}
var t11 = new System.Threading.Thread(delegate()
{
var start = DateTime.Now;
threadrun = true;
readandsleep(100);
try
{
comPort.Write("\n\n\n\nexit\r\nlogs\r\n"); // more in "connecting"
}
catch
{
}
while (threadrun)
{
try
{
updateDisplay();
System.Threading.Thread.Sleep(10);
if (!comPort.IsOpen)
break;
while (comPort.BytesToRead >= 4)
{
comPort_DataReceived((object)null, (SerialDataReceivedEventArgs)null);
}
}
catch (Exception ex)
{
log.Error("crash in comport reader " + ex);
} // cant exit unless told to
}
log.Info("Comport thread close");
}) {Name = "comport reader"};
t11.Start();
// doesnt seem to work on mac
//comPort.DataReceived += new SerialDataReceivedEventHandler(comPort_DataReceived);
}