本文整理汇总了C#中System.IO.Ports.SerialPort.Flush方法的典型用法代码示例。如果您正苦于以下问题:C# SerialPort.Flush方法的具体用法?C# SerialPort.Flush怎么用?C# SerialPort.Flush使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.Ports.SerialPort
的用法示例。
在下文中一共展示了SerialPort.Flush方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main(string[] args)
{
SerialPort ser = new SerialPort( "COM1", (int)BaudRate.Baudrate115200 );
byte[] buf = new byte[26];
byte[] ret = new byte[26];
ser.Handshake = Handshake.XOnXOff;
ser.DataReceived += new SerialDataReceivedEventHandler(ser_DataReceived);
ser.Open();
Debug.Print("Initializing Serial Test");
for (int i = 0;i < buf.Length;i++)
{
buf[i] = (byte)('a' + i);
}
ser.Write(buf, 0, buf.Length);
ser.Flush();
Thread.Sleep(1000);
ser.DataReceived -= new SerialDataReceivedEventHandler(ser_DataReceived);
ser.Write(buf, 0, buf.Length);
Thread.Sleep(1000);
ser.DataReceived += new SerialDataReceivedEventHandler(ser_DataReceived);
ser.ErrorReceived += new SerialErrorReceivedEventHandler(ser_ErrorReceived);
ser.Write(buf, 0, buf.Length);
Thread.Sleep(3000);
ser.Read(ret, 0, ret.Length);
Thread.Sleep(10000);
}
示例2: Main
public static void Main()
{
int count = 0;
SerialPort SPort = new SerialPort(SerialPorts.COM2, 9600, Parity.None,8,StopBits.One);
SPort.ReadTimeout = 1000;
SPort.WriteTimeout = 1000;
byte[] buf = new byte[5];
string CardId = "";
SPort.Open();
byte[] readCommand = { 0x21, 0x52, 0x57, 0x01, 0x03 };
int numCodes = 0;
while (true)
{
//SPort.Write(new byte[] { 0xFF, 0xFF, 0X39, 0x44 }, 0, 4);
SPort.Write(readCommand, 0, 5);
SPort.Flush();
int readcnt = SPort.Read(buf, 0, SPort.BytesToRead);
SPort.Flush();
string s = "";
if (buf[0] == 0x01 && numCodes < 10)
{
foreach (byte b in buf)
{
s = s + b.ToString() + ",";
}
if (s[0] == '1')
{
count++;
Debug.Print(count.ToString() + ":" + s + "\n");
numCodes++;
}
}
}
}
示例3: 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();
}
示例4: SerialPortHelper
public SerialPortHelper(string portName = "COM1", int baudRate = 250000, Parity parity = Parity.None, int dataBits = 8, StopBits stopBits = StopBits.One)
{
serialPort = new SerialPort(portName, baudRate, parity, dataBits, stopBits);
//serialPort.ReadTimeout = 1; // Set to 10ms. Default is -1?!
serialPort.DataReceived += new SerialDataReceivedEventHandler(SerialPortDataReceived);
serialPort.Flush();
serialPort.Open();
}
示例5: Amulet
private int _rx_buffer_pointer; // points to next available element in the rx buffer
//-------------------------------------------------------------------------------------------------------------------------------------------
public Amulet( string port, int baudrate, AmuletSlaveResponseType response_type, IAmuletClient client=null)
{
_client = client;
_response_type = response_type;
_response_event = new AutoResetEvent( false);
Debug.Print( "creating serial port");
_port = new SerialPort( port, baudrate, Parity.None, 8, StopBits.One);
Debug.Print( "setting serial port read timeout");
_port.ReadTimeout = 1000;
Debug.Print( "opening serial port");
_port.Open();
Debug.Print( "flushing serial port buffer");
_port.Flush();
Debug.Print( "registering serial port data event handler");
_port.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived);
Debug.Print( "RAM left: " + Debug.GC( true) + " bytes");
}
示例6: Main
public static void Main()
{
Radio = new SerialPort("COM1", 9600);
Radio.Open();
// Radio.DataReceived += new SerialDataReceivedEventHandler(UART_DataReceived);
uint i = 0;
while (true)
{
if (leftPwr != 0)
leftMotor.SetPulse(100000, (uint)(((double)leftPwr / 100) * 90000.0));
else
leftMotor.SetPulse(100000, 0);
if (rightPwr != 0)
rightMotor.SetPulse(100000, (uint)(((double)rightPwr / 100) * 90000.0));
else
rightMotor.SetPulse(100000, 0);
//leftMotor.SetPulse(100000, 90000);
Thread.Sleep(1);
string input = "";
// read the data
try
{
read_count = Radio.Read(rx_data, 0, Radio.BytesToRead);
if (read_count > 0)
{
for (int k = 0; k < read_count; k++ )
{
if (rx_data[k] == '$')
i = 0;
else if (rx_data[k] == '*')
{
Radio.Flush();
for(int j = 0; j < i; j++)
{
input += (char)str[j];
}
ParseCommand(input);
i = 0;
read_count = 0;
}
else
{
str[i] = rx_data[k];
i++;
if (i > str.Length) i = 0;
}
}
}
}
catch
{
}
}
}
示例7: DataRcdEvent
public MFTestResults DataRcdEvent()
{
if (!IsLoopback)
return MFTestResults.Skip;
// BUGBUG: 21216
result = MFTestResults.Fail;
try
{
eventCount = 0;
eventSerialPort = new SerialPort(Serial.COM1);
// register before open
eventSerialPort.DataReceived += new SerialDataReceivedEventHandler(eventserialPort_DataReceived_BeforeOpen);
eventSerialPort.Open();
eventSerialPort.DataReceived += new SerialDataReceivedEventHandler(eventSerialPort_DataReceived_AfterOpen);
eventSerialPort.DataReceived += new SerialDataReceivedEventHandler(eventSerialPort_DataReceived_AfterOpen2);
eventSerialPort.Write(sendbuff, 0, sendbuff.Length);
eventSerialPort.Flush();
for (int i = 0; i < 100; i++)
{
Thread.Sleep(100);
if (eventCount >= 3)
{
result = MFTestResults.Pass;
break;
}
}
Log.Comment(eventCount + " events fired");
eventSerialPort.Close();
}
catch (Exception ex)
{
Log.Exception(ex.Message);
}
finally
{
eventSerialPort.Dispose();
}
return result;
}
示例8: GetPPM
/// <summary>
/// Takes reading from Atlas Scientific ElectricalConductivity Stamp
/// </summary>
/// <returns></returns>
private float GetPPM()
{
float PPM = 0.0F;
SerialPort sp = new SerialPort(Serial.COM2, 38400, Parity.None, 8, StopBits.One);
sp.ReadTimeout = 1000;
try
{
string command = "";
string response = "";
char inChar;
// Send the temperature reading if available
if (m_Temperature > 0)
command = m_Temperature.ToString("F") + "\rR\r";
else
command = "R\r";
Debug.Print(command);
byte[] message = Encoding.UTF8.GetBytes(command);
sp.Open();
sp.Write(message, 0, message.Length);
sp.Flush();
Debug.Print("sending message");
// Now collect response
while ((inChar = (char)sp.ReadByte()) != '\r') { response += inChar; }
response = response.Split(',')[1];
// Stamp can return text if reading was not successful, so test before returning
double ppmReading;
if (Double.TryParse(response, out ppmReading)) PPM = (float)ppmReading;
}
catch (Exception e)
{
Debug.Print(e.StackTrace);
}
finally
{
sp.Close();
sp.Dispose();
}
return PPM;
}
示例9: Start
public static void Start(Handshake handshake)
{
int nWritten;
// Note: zeros must be sent first to help the receiver sync after the noise generated
// by the port initialization - don't send the important stuff right away...!
byte[] start = { 0 };
if (handshake == Handshake.None)
{
Debug.Print("***** Testing No Flow Control *****");
start = new byte[] { 0, 0, 0, 0, 0, (byte)'X', (byte)'2', (byte)'3', (byte)'\r', (byte)'\n', 0 };
}
else if (handshake == Handshake.XOnXOff)
{
Debug.Print("***** Testing SW Flow Control *****");
start = new byte[] { 0, 0, 0, 0, 0, (byte)'X', (byte)'2', (byte)'4', (byte)'\r', (byte)'\n', 0 };
}
else if (handshake == Handshake.RequestToSend)
{
Debug.Print("***** Testing HW Flow Control *****");
start = new byte[] { 0, 0, 0, 0, 0, (byte)'X', (byte)'2', (byte)'5', (byte)'\r', (byte)'\n', 0 };
}
SerialPortTest app = new SerialPortTest();
byte[] Record = new byte[c_BufferSize];
UInt32 uiRecordNumber = 0x1323; // Starting record number (pulled from a hat)
SerialPort hPort = new SerialPort("COM1", (int)BaudRate.Baudrate115200);
hPort.Handshake = handshake;
hPort.ReadTimeout = 500;
hPort.Open();
hPort.Flush();
while (0 < hPort.Read(Record, 0, Record.Length)) ;
// Note:
// When a hardware reset is issued to some targets prior to a deploy, the original deployed
// code begins to execute for a second or two before Visual Studio is able to gain control,
// halt the old deployed code and deploy the current program.
// The following two second delay is to prevent this app from issuing the start record prior
// to VS gaining control. Otherwise, the PC side app will get the signal to start before
// the target side app has even deployed. This will result in the loss of the first three
// or four data records and the test will fail.
Thread.Sleep(2000);
// Send out a simple signal to show the PC side test software that we're ready to go
hPort.Write(start, 0, start.Length);
// Test COM port handshake by flooding the port with testable sequential records
// and verifying that the records arrive correctly. The handshake is verified
// by causing 5 second pauses during reading which should translate to five
// second pauses in the transmit as well - unless the driver buffer is obviating
// the need for handshake - in which case, the buffer should be diminished or the
// volume of traffic should increase until handshake must be employed.
for (int i = 0; i < c_NumRecords; i++)
{
if (!app.ReceiveRecord(hPort, Record)) // If there were problems
{
Debug.Print("There were problems with test #1, record #" + toString(i + 1) + " of " + toString(c_NumRecords));
// return; // We may as well quit now
}
//Debug.Print(AsString(Record));
//Debug.Print(new string(System.Text.UTF8Encoding.UTF8.GetChars(Record)));
if (!app.CheckRecord(uiRecordNumber, Record)) // If there were problems
{
Debug.Print("There were problems with test #1, record #" + toString(i + 1) + " of " + toString(c_NumRecords));
//return; // We may as well quit now
}
uiRecordNumber++; // Next record
}
for (int i = 0; i < c_NumRecords; i++)
{
app.CreateRecord(uiRecordNumber, Record);
//Debug.Print("\nSending Record: " + uiRecordNumber);
//Debug.Print(new string(System.Text.UTF8Encoding.UTF8.GetChars(Record)));
if(!app.SendRecord(hPort, Record)) // If there were problems
{
Debug.Print("There were problems with test #2, record #" + toString(i + 1) + " of " + toString(c_NumRecords));
return; // We may as well quit now
}
uiRecordNumber++;
}
// This is just like the previous test - except that a gap of five seconds
// will be inserted before receiving the rest of the records which will
// hopefully hold off transmission due to a functioning handshake.
for (int i = 0; i < 5; i++)
{
if (!app.ReceiveRecord(hPort, Record)) // If there were problems
{
Debug.Print("There were problems with test #3, record #" + toString(i + 1) + " of 5");
return; // We may as well quit now
}
if (!app.CheckRecord(uiRecordNumber, Record)) // If there were problems
{
return; // We may as well quit now
}
uiRecordNumber++; // Next record
//.........这里部分代码省略.........
示例10: initialiser
// ----- INITIALISATION ACTIONNEURS -----
public void initialiser(couleur couleur, int position)
{
m_jambes = new CLocomotion();
m_direction = new OutputPort((Cpu.Pin)09, false);
m_portSerie = new SerialPort("COM1", 1000000, Parity.None, 8, StopBits.One);
m_portSerie.ReadTimeout = 250;
m_portSerie.Open();
if (m_portSerie.IsOpen)
{
m_portSerie.Flush();
m_ax12_pince = new CAX_12(2, m_portSerie, m_direction);
m_ax12_bras = new CAX_12(5, m_portSerie, m_direction);
m_pince = new CPince(m_ax12_pince);
m_bras = new CBras(m_ax12_bras);
}
m_controleur = new CSerializer("COM3");
m_moteurAscenceur = new CMoteur(m_controleur, 1);
m_moteurPompe = new CMoteur(m_controleur, 2);
m_potar = new CPotar(m_controleur, 1);
m_ascenseur = new CAscenseur(m_moteurAscenceur, m_potar);
m_pompe = new CPompe(m_moteurPompe);
initialiserAscenseur();
initialiserLocomotion(couleur, position);
saisirVerres = false;
threadSaisirVerre.Start();
}
示例11: Calibrate
/// <summary>
/// Relays data between a serial port and the telnet session.
/// This allows access from telnet to the serial
/// port of the EC and PH moduled directly.
/// </summary>
/// <param name="SerialPortName">String for the serial port you want to access.</param>
private void Calibrate(String SerialPortName)
{
SerialPort sp = new SerialPort(SerialPortName, 38400, Parity.None, 8, StopBits.One);
try
{
byte ExitCommand = 94; //^ Key
bool Continue = true;
int m_LoopCount = 0;
sp.ReadTimeout = 4000;
sp.WriteTimeout = 4000;
sp.Open();
do
{
m_LoopCount += 1; //Incriment loop count for inactivity.
if (activeConnection.Available > 0)
{
byte[] t_buffer = new byte[activeConnection.Available];
activeConnection.Receive(t_buffer);
if (t_buffer[0] == ExitCommand) //exit if ^ is pressed
{
Continue = false;
break;
}
else
{
if (t_buffer[t_buffer.Length - 2] == 13 && t_buffer[t_buffer.Length - 1] == 10) //When sending data to the stamp replace crlf with return.
{
m_LoopCount = 0; //Reset Loop Count
sp.Write(t_buffer, 0, t_buffer.Length - 1); //Send message to the serial port
}
else
{
m_LoopCount = 0; //Reset Loop Count
sp.Write(t_buffer, 0, t_buffer.Length); //Send message to the serial port
}
sp.Flush();
}
t_buffer = null;
}
if (sp.BytesToRead > 0)
{
byte[] ph_cmd = new byte[sp.BytesToRead];
sp.Read(ph_cmd, 0, ph_cmd.Length); //Read from serial port
activeConnection.Send(ph_cmd, SocketFlags.None); //Write what was read to telnet session
m_LoopCount = 0; //Reset Loop Count
ph_cmd = null;
}
Thread.Sleep(100);
} while (Continue && m_LoopCount < 60000); //Time out after 1 minute of inactivity.
}
catch (Exception e)
{
_sendReply("Error accessing serial port: \r\n" + e.Message + "\r\nIt may be in use. Try again later.\r\n");
}
finally
{
//close the serial port if its opened and Dispose it.
if (sp.IsOpen)
sp.Close();
sp.Dispose();
}
}