本文整理匯總了C#中System.IO.Ports.SerialPort.DiscardInBuffer方法的典型用法代碼示例。如果您正苦於以下問題:C# SerialPort.DiscardInBuffer方法的具體用法?C# SerialPort.DiscardInBuffer怎麽用?C# SerialPort.DiscardInBuffer使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.IO.Ports.SerialPort
的用法示例。
在下文中一共展示了SerialPort.DiscardInBuffer方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: Button_Click
private void Button_Click(object sender, RoutedEventArgs e)//按鈕打開事件
{
try
{
open.IsEnabled = false;
stop.IsEnabled = true;
control.IsEnabled = true;
port = new SerialPort();
port.BaudRate = 115200;
port.PortName = COMName.Text;
port.DataBits = 8;
port.Open();
port.DiscardInBuffer();
port.DiscardOutBuffer();
MessageBox.Show("串口打開成功", "係統提示");
}
catch (IOException ex)
{
MessageBox.Show("串口打開失敗" + ex, "係統提示");
return;
}
_keepReading = true;
_readThread = new Thread(ReadPort);
_readThread.Start();
}
示例2: Open
public bool Open(string[] args)
{
string portName = args[0]; // we must pass serial port name here
int baudRate = 115200;
Debug.WriteLine("Trying serial port: " + portName);
serialPort = new SerialPort(portName, baudRate, Parity.None, 8, StopBits.One);
serialPort.PortName = portName;
serialPort.BaudRate = baudRate;
serialPort.Parity = Parity.None;
serialPort.DataBits = 8;
serialPort.StopBits = StopBits.One;
serialPort.DtrEnable = true; // Arduino Leonardo requires this
//serialPort.RtsEnable = false;
//serialPort.ReadTimeout = 300;
//serialPort.WriteTimeout = 10000;
serialPort.DataReceived += serialPort_DataReceived;
serialPort.ErrorReceived += serialPort_ErrorReceived;
//serialPort.
//serialPort.NewLine = "\r";
Debug.WriteLine("IP: serial port - opening...");
serialPort.Open();
Debug.WriteLine("OK: opened");
// Clear receive buffer out, since the bootloader can send
// some junk characters, which might hose subsequent command responses:
serialPort.DiscardInBuffer();
stopWatch.Start();
return true;
}
示例3: BK8500
public BK8500(string comPortname, byte address)
{
instrumentError = false;
//Setup serial port
P = new SerialPort(comPortname, 38400);
P.DataBits = 8;
P.StopBits = StopBits.One;
P.Parity = Parity.None;
P.RtsEnable = true;
P.ReadTimeout = 200;
//Setup event for received data
P.ReceivedBytesThreshold = packetLength; //all packets for this device are 26 bytes in length
P.DataReceived += new SerialDataReceivedEventHandler(P_DataReceived);
dataWaitHandle = new EventWaitHandle(false, EventResetMode.ManualReset); //this syncs the received data event handler to the main thread
P.Open();
P.DiscardInBuffer();
this.address = address;
//enter local mode
this.remoteOperation = true;
this.loadON = false;
this.deviceConnected = true;
}
示例4: Serial2Matlab
// public Serial2Matlab(string port, int baudRate = 115200, Parity parity = Parity.None, int dataBits = 8, StopBits stopBits = StopBits.One)
public Serial2Matlab()
{
serialPort = new SerialPort("COM43", 115200, Parity.None, 8, StopBits.One);
serialPort.ReadTimeout = 2;
serialPort.Open();
serialPort.DiscardInBuffer();
}
示例5: Main
//public static void Main()
public static void Main()
{
SerialPort port = new SerialPort("COM7", 115200, Parity.None, 8, StopBits.One);
SerialPortInterface portInterface = new SerialPortInterface(port);
portInterface.Open();
port.DiscardInBuffer();
while (true)
{
while (portInterface.ReadByte() != 'S')
{
}
float sintime = portInterface.ReadShort();
float sinval = portInterface.ReadFloat();
float fsintime = portInterface.ReadShort();
float fsinval = portInterface.ReadFloat();
Console.WriteLine(String.Format("{0},{1},{2},{3}", sintime, sinval, fsintime, fsinval));
/*
float accx = portInterface.ReadFloat();
float accy = portInterface.ReadFloat() * (float)(180.0f / Math.PI);
float accz = portInterface.ReadFloat() * (float)(180.0f / Math.PI);
float magx = portInterface.ReadFloat();
float magy = portInterface.ReadFloat();
float magz = portInterface.ReadFloat();
Console.WriteLine(String.Format("{0},{1},{2},{3},{4},{5}", accx, accy, accz, magx, magy, magz));
*/
}
}
示例6: CSerializer
public CSerializer(string com)
{
m_serial = new SerialPort(com, 19200, Parity.None, 8, StopBits.One);
m_serial.Open();
m_serial.Flush();
m_serial.DiscardInBuffer();
}
示例7: CompanySerialIO
public CompanySerialIO(int serialPortNumber)
{
_semaphore = new Semaphore(1, 1);
_serialPort = new SerialPort("COM" + serialPortNumber);
_serialPort.ReadTimeout = 1000;
_serialPort.Open();
_serialPort.DiscardInBuffer(); // Discard unsynchronized data
_serialPort.DiscardOutBuffer(); // Discard unsynchronized data
}
示例8: theLoop
public void theLoop()
{
int listenPort = 11000;
UdpClient listener = new UdpClient(listenPort);
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);
SerialPort portCOM1 = new SerialPort();
try{
portCOM1 = new SerialPort("/dev/ttyS0", 115200, Parity.None, 8, StopBits.One);
portCOM1.Open();
}
catch(Exception ee)
{
Console.WriteLine("Not connected to COM1");
Console.WriteLine(ee.Message.ToString());
}
string received_data;
byte[] received_byte_array;
bool done = false;
while(!done)
{
Console.WriteLine("Waiting For Data - press (q) to quit");
received_byte_array = listener.Receive(ref groupEP);
received_data = "";
for(int i = 0; i < received_byte_array.Length; i++)
{
received_data = received_data+received_byte_array[i]+" ";
}
Console.WriteLine("Recived Data From {0}: {1}", groupEP.ToString(), received_data);
try{
portCOM1.Write(received_byte_array,0,received_byte_array.Length); // write what was recived on UDP to com 1
Console.WriteLine("Wrote data to COM1 @ 115200 8N1");
}
catch(Exception ee)
{
Console.WriteLine(ee.Message.ToString());
try{
portCOM1.Close();
portCOM1.DiscardOutBuffer();
portCOM1.DiscardInBuffer();
listener.Close();
listener = new UdpClient(listenPort);
groupEP = new IPEndPoint(IPAddress.Any, listenPort);
}
catch(Exception eee)
{
Console.WriteLine(eee.Message.ToString());
}
}
}
}
示例9: SerialComm
public SerialComm(string port)
{
serial = new SerialPort(port, 115200, Parity.None, 8, StopBits.One)
{
Handshake = Handshake.None, //Handshake not needed
};
serial.DataReceived += SerialDataReceived;
serial.Open();
serial.DiscardOutBuffer();
serial.DiscardInBuffer();
}
示例10: Open
/* open serial port */
public void Open(string portName, uint baudRate)
{
/* initialize serial port */
sp = new SerialPort(portName, (int)baudRate, Parity.Even, 8);
/* open serial port */
sp.Open();
/* discard buffers */
sp.DiscardInBuffer();
sp.DiscardOutBuffer();
}
示例11: ChangePort
/// <summary>
/// Changes the current port to the given port.
/// </summary>
/// <param name="portName">The name of the port to change to.</param>
public void ChangePort(string portName)
{
if (_port != null && _port.IsOpen)
ClosePort();
_port = new SerialPort(portName);
_port.BaudRate = 9600;
_port.DataReceived += new SerialDataReceivedEventHandler(onPortDataReceived);
_port.Open();
_port.DiscardInBuffer();
}
示例12: ExecCommand
public static string ExecCommand(SerialPort port, string command, int responseTimeout, string errorMessage)
{
port.DiscardOutBuffer();
port.DiscardInBuffer();
receiveNow.Reset();
port.Write(command + "\r");
string input = ReadResponse(port, responseTimeout);
if ((input.Length == 0) || ((!input.EndsWith("\r\n> ")) && (!input.EndsWith("\r\nOK\r\n"))))
throw new ApplicationException("No success message was received.");
return input;
}
示例13: Modem
public Modem(SerialPort port)
{
serialPort = port;
//init port
EnsurePortOpen();
//we will handle read timeout manually, system implementation is not consistent
serialPort.ReadTimeout = 0;
serialPort.WriteTimeout = SerialPort.InfiniteTimeout;
serialPort.DiscardInBuffer();
serialPort.DiscardOutBuffer();
serialPort.NewLine = "\r";
serialPort.Encoding = Encoding.ASCII;
}
示例14: gainsmessageasync_test
public void gainsmessageasync_test()
{
SerialPort port = new SerialPort("COM7", 250000, Parity.None, 8, StopBits.One);
SerialPortInterface portInterface = new SerialPortInterface(port);
using (FlightComputerInterface fcInt = new FlightComputerInterface(portInterface))
{
fcInt.Open();
port.DiscardInBuffer();
port.DiscardOutBuffer();
FlightComputerTelemetryMessage gains = null;
for (int i = 0; i < 1000; i++)
{
gains = (FlightComputerTelemetryMessage) fcInt.Receive();
Assert.IsTrue(gains.LateralInnerLoopGain == 1.1f);
Assert.IsTrue(gains.LongitudeInnerLoopGain == 1.2f);
Assert.IsTrue(gains.PitchAngularVelocityGain == 1.3f);
Assert.IsTrue(gains.RollAngularVelocityGain == 1.4f);
Assert.IsTrue(gains.XAntiWindupGain == 1.5f);
Assert.IsTrue(gains.XDerivativeGain == 1.6f);
Assert.IsTrue(gains.XIntegralGain == 1.7f);
Assert.IsTrue(gains.XProportionalGain == 1.8f);
Assert.IsTrue(gains.YAntiWindupGain == 1.9f);
Assert.IsTrue(gains.YawAntiWindupGain == 1.0f);
Assert.IsTrue(gains.YawDerivativeGain == 1.11f);
Assert.IsTrue(gains.YawIntegralGain == 1.12f);
Assert.IsTrue(gains.YawProportionalGain == 1.13f);
Assert.IsTrue(gains.YDerivativeGain == 1.14f);
Assert.IsTrue(gains.YIntegralGain == 1.15f);
Assert.IsTrue(gains.YProportionalGain == 1.16f);
Assert.IsTrue(gains.ZAntiWindupGain == 1.17f);
Assert.IsTrue(gains.ZDerivativeGain == 1.18f);
Assert.IsTrue(gains.ZIntegralGain == 1.19f);
Assert.IsTrue(gains.ZProportionalGain == i);
Debug.WriteLine(i);
fcInt.Transmit(gains);
}
gains = (FlightComputerTelemetryMessage) fcInt.Receive();
Assert.IsTrue(gains.ZProportionalGain == 12);
}
}
示例15: ClearBuffer
public static bool ClearBuffer(SerialPort port)
{
try
{
port.DiscardOutBuffer();
port.DiscardInBuffer();
return true;
}
catch(Exception ex)
{
FileWorker.WriteEventFile(DateTime.Now, "ASubDriver", "ClearBuffer", ex.Message);
return false;
}
}