本文整理汇总了C#中SerialPort.Close方法的典型用法代码示例。如果您正苦于以下问题:C# SerialPort.Close方法的具体用法?C# SerialPort.Close怎么用?C# SerialPort.Close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SerialPort
的用法示例。
在下文中一共展示了SerialPort.Close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Chronopic
//-- Constructor
public Chronopic(SerialPort sp)
{
//-- Comprobar si puerto serie ya estaba abierto
if (sp != null)
if (sp.IsOpen)
sp.Close();
//-- Abrir puerto serie
sp.Open();
//-- Configurar timeout por defecto
//de momento no en windows (hasta que no encontremos por qué falla)
//OperatingSystem os = Environment.OSVersion;
//not used, now there's no .NET this was .NET related
//on mono timeouts work on windows and linux
//if( ! os.Platform.ToString().ToUpper().StartsWith("WIN"))
sp.ReadTimeout = DefaultTimeout;
//-- Guardar el puerto serie
this.sp = sp;
// //-- Vaciar buffer
// //done in a separate method
// this.flush();
}
示例2: checkConnection
// The state object is necessary for a TimerCallback.
public void checkConnection(object stateObject)
{
Process p = new Process();
Ping pingSender = new Ping ();
p.StartInfo.FileName = "arp";
p.StartInfo.Arguments = "-a";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
string data = "a";
byte[] buffer = Encoding.ASCII.GetBytes (data);
for(int i = 0; i < 25 ; i++){
pingSender.Send ("10.0.0."+i.ToString(),10,buffer);
}
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
string MAC = "xx-xx-xx-xx-xx-xx";
if(output.Contains(MAC)){
SerialPort port = new SerialPort("COM5", 9600);
port.Open();
port.Write("u");
port.Close();
}
else{
SerialPort port = new SerialPort("COM5", 9600);
port.Open();
port.Write("l");
port.Close();
}
}
示例3: Start
// Use this for initialization
void Start()
{
Debug.Log(SerialPort.GetPortNames().ToString());
mySerialPort = new SerialPort("\\\\.\\COM18");
mySerialPort.BaudRate = 9600;
//mySerialPort.Parity = Parity.None;
//mySerialPort.StopBits = StopBits.One;
//mySerialPort.DataBits = 8;
//mySerialPort.Handshake = Handshake.None;
//mySerialPort.RtsEnable = true;
//mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
if ( mySerialPort != null )
{
if ( mySerialPort.IsOpen ) // close if already open
{
mySerialPort.Close();
Debug.Log ("Closed stream");
}
mySerialPort.Open();
Debug.Log ("Opened stream");
}
else
{
Debug.Log ("ERROR: Uninitialized stream");
}
myThread = new Thread(new ThreadStart(GetArduino));
myThread.Start();
}
示例4: Start
// Use this for initialization
void Start () {
sp = new SerialPort(serialPortName, 9600);
if (sp.IsOpen)
{
sp.Close();
}
else {
sp.Open();
sp.ReadTimeout = 1;
}
}
示例5: Main
public static void Main()
{
string name;
string message;
StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
Thread readThread = new Thread(Read);
_serialPort = new SerialPort();
_serialPort.PortName = "COM2";
_serialPort.BaudRate = 9600;
_serialPort.Parity = Parity.None;
_serialPort.DataBits = 8;
_serialPort.StopBits = StopBits.One;
_serialPort.Handshake = Handshake.XOnXOff;
// Set the read/write timeouts
_serialPort.ReadTimeout = 500;
_serialPort.WriteTimeout = 500;
_serialPort.Open();
_continue = true;
readThread.Start();
Console.Write("Name: ");
name = Console.ReadLine();
Console.WriteLine("Type QUIT to exit");
while (_continue)
{
message = Console.ReadLine();
if (stringComparer.Equals("quit", message))
{
_continue = false;
}
else
{
_serialPort.WriteLine(" " + message + " ");
//String.Format("{0}:{1}", name, message));
}
}
readThread.Join();
_serialPort.Close();
_serialPort = null;
}
示例6: MainS
public static void MainS()
{
string name;
string message;
StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
Thread readThread = new Thread(Read);
// Create a new SerialPort object with default settings.
_serialPort = new SerialPort();
// Allow the user to set the appropriate properties.
_serialPort.PortName = SetPortName(_serialPort.PortName);
_serialPort.BaudRate = SetPortBaudRate(_serialPort.BaudRate);
_serialPort.Parity = SetPortParity(_serialPort.Parity);
_serialPort.DataBits = SetPortDataBits(_serialPort.DataBits);
_serialPort.StopBits = SetPortStopBits(_serialPort.StopBits);
_serialPort.Handshake = SetPortHandshake(_serialPort.Handshake);
// Set the read/write timeouts
_serialPort.ReadTimeout = 500;
_serialPort.WriteTimeout = 500;
_serialPort.Open();
_continue = true;
readThread.Start();
Console.Write("Name: ");
name = Console.ReadLine();
Console.WriteLine("Type QUIT to exit");
while(_continue)
{
message = Console.ReadLine();
if(stringComparer.Equals("quit", message))
{
_continue = false;
}
else
{
_serialPort.WriteLine(
String.Format("<{0}>: {1}", name, message));
}
}
readThread.Join();
_serialPort.Close();
}
示例7: Main
public static void Main()
{
string name;
string message;
StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
Thread readThread = new Thread(Read);
// Create a new SerialPort object with default settings.
_serialPort = new SerialPort();
// Allow the user to set the appropriate properties.
_serialPort.PortName = "COM3";
_serialPort.BaudRate = 9600;
_serialPort.Parity = Parity.None;
_serialPort.DataBits = 8;
_serialPort.StopBits = StopBits.One;
_serialPort.Handshake = Handshake.None;
// Set the read/write timeouts
_serialPort.ReadTimeout = 500;
_serialPort.WriteTimeout = 500;
_serialPort.Open();
_continue = true;
readThread.Start();
Console.Write("Name: ");
name = Console.ReadLine();
Console.WriteLine("Type QUIT to exit");
byte[] Command = new byte[] { Convert.ToByte(1), Convert.ToByte(4), Convert.ToByte(1), Convert.ToByte(30) };
while (_continue)
{
message = Console.ReadLine();
if (stringComparer.Equals("quit", message))
{
_continue = false;
}
else
{
_serialPort.Write(Command, 0, 4);
}
}
_serialPort.Close();
}
示例8: Main
public static void Main(string[] args)
{
SerialPort sp = new SerialPort("/dev/ttyUSB0", 115200);
sp.Open();
try {
byte[] b = new byte[1];
while (true) {
int i = Console.Read();
if (i == -1) break;
b[0] = (byte)i;
sp.Write(b, 0, 1);
}
} catch {
sp.Close();
}
}
示例9: Main
public static void Main()
{
string name;
string message;
StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
Thread readThread = new Thread(Read);
// Создание нового объекта SerialPort с установками по умолчанию.
_serialPort = new SerialPort();
// Позволяем пользователю установить подходящие свойства.
_serialPort.PortName = SetPortName(_serialPort.PortName);
_serialPort.BaudRate = SetPortBaudRate(_serialPort.BaudRate);
_serialPort.Parity = SetPortParity(_serialPort.Parity);
_serialPort.DataBits = SetPortDataBits(_serialPort.DataBits);
_serialPort.StopBits = SetPortStopBits(_serialPort.StopBits);
_serialPort.Handshake = SetPortHandshake(_serialPort.Handshake);
// Установка таймаутов чтения/записи (read/write timeouts)
_serialPort.ReadTimeout = 500;
_serialPort.WriteTimeout = 500;
_serialPort.Open();
_continue = true;
readThread.Start();
Console.Write("Name: ");
name = Console.ReadLine();
Console.WriteLine("Type QUIT to exit");
while (_continue)
{
message = Console.ReadLine();
if (stringComparer.Equals("quit", message))
{
_continue = false;
} else
{
_serialPort.WriteLine(
String.Format("< {0} >: {1}", name, message));
}
}
readThread.Join();
_serialPort.Close();
}
示例10: SendInfo
public static string SendInfo(HttpListenerRequest request)
{
string context = "";
Boolean succeeded = false;
int count = 0;
SerialPort serialPort = new SerialPort("COM3", 9600, Parity.None, 8, StopBits.One);
while (!succeeded)
{
try
{
serialPort.Open();
int cm1 = Convert.ToInt32(serialPort.ReadLine());
int cm2 = Convert.ToInt32(serialPort.ReadLine());
if (cm1 >= 30)
{
context = "false:" + cm1.ToString();
Console.WriteLine(context);
}
else
{
context = "true:" + cm1.ToString();
Console.WriteLine(context);
}
succeeded = true;
if (cm1 != cm2)
{
succeeded = false;
count++;
}
if (count>=9){
context = "false:outOfRange";
Console.WriteLine(context);
succeeded = true;
}
}
catch (Exception ex)
{
context = "open error " + ex.Message;
}
finally
{
serialPort.Close();
}
}
return string.Format(context);
}
示例11: Main
public static void Main()
{
string name;
string message;
var readThread = new Thread(Read);
// Create a new SerialPort object with default settings.
_serialPort = new SerialPort();
// Allow the user to set the appropriate properties.
InitializePort(_serialPort);
// Set the read/write timeouts
_serialPort.ReadTimeout = 500;
_serialPort.WriteTimeout = 500;
_serialPort.Open();
_continue = true;
readThread.Start();
Console.Write("Name: ");
name = Console.ReadLine();
Console.WriteLine("Type QUIT to exit");
while (_continue)
{
message = Console.ReadLine();
if (message != null && "quit" == message.ToLowerInvariant())
{
_continue = false;
}
else
{
_serialPort.WriteLine(
String.Format("<{0}>: {1}", name, message));
}
}
readThread.Join();
_serialPort.Close();
}
示例12: 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();
}
示例13: OpenConnection
void OpenConnection(SerialPort _stream) {
if (_stream != null) {
if (_stream.IsOpen) {
_stream.Close ();
Debug.LogError ("Failed to open Serial Port, already open!");
} else {
_stream.Open ();
_stream.ReadTimeout = 10;
_stream.WriteTimeout = 40;
Debug.Log ("Open Serial port1");
}
}
}
示例14: Main
public static void Main()
{
string message;
//Configuration c = new Configuration();
//Configuration c = Configuration.Deserialize("u2wits.xml");
StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
Thread readThread = new Thread(Read);
// Create a new SerialPort object with default settings.
_uniproPort = new SerialPort();
_witsPort = new SerialPort();
// Allow the user to set the appropriate properties.
if (c.uniproName == "") c.uniproName = SetPortName(_uniproPort.PortName);
_uniproPort.PortName = c.uniproName;
if (c.uniproBaudRate == -1) c.uniproBaudRate = SetPortBaudRate(_uniproPort.BaudRate);
_uniproPort.BaudRate = c.uniproBaudRate;
if (c.uniproParity == "") c.uniproParity = SetPortParity(_uniproPort.Parity);
_uniproPort.Parity = (Parity)Enum.Parse(typeof(Parity), c.uniproParity);
if (c.uniproDataBits == -1) c.uniproDataBits = SetPortDataBits(_uniproPort.DataBits);
_uniproPort.DataBits = c.uniproDataBits;
if (c.uniproStopBits == "") c.uniproStopBits = SetPortStopBits(_uniproPort.StopBits);
_uniproPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits),c.uniproStopBits);
if (c.uniproHandshake == "") c.uniproHandshake = SetPortHandshake(_uniproPort.Handshake);
_uniproPort.Handshake = (Handshake)Enum.Parse(typeof(Handshake), c.uniproHandshake);
if (c.uniproPressure == -1) c.uniproPressure = SetPressure();
if (c.uniproDensity == -1) c.uniproDensity = SetDensity();
if (c.uniproRate == -1) c.uniproRate = SetRate();
if (c.uniproVolume == -1) c.uniproVolume = SetVolume();
if (c.witsName == "") c.witsName = SetPortName(_witsPort.PortName);
_witsPort.PortName = c.witsName;
if (c.witsBaudRate == -1) c.witsBaudRate = SetPortBaudRate(_witsPort.BaudRate);
_witsPort.BaudRate = c.witsBaudRate;
if (c.witsParity == "") c.witsParity = SetPortParity(_witsPort.Parity);
_witsPort.Parity = (Parity)Enum.Parse(typeof(Parity), c.witsParity);
if (c.witsDataBits == -1) c.witsDataBits = SetPortDataBits(_witsPort.DataBits);
_witsPort.DataBits = c.witsDataBits;
if (c.witsStopBits == "") c.witsStopBits = SetPortStopBits(_witsPort.StopBits);
_witsPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits), c.witsStopBits);
if (c.witsHandshake == "") c.witsHandshake = SetPortHandshake(_witsPort.Handshake);
_witsPort.Handshake = (Handshake)Enum.Parse(typeof(Handshake), c.witsHandshake);
if (c.witsHeadder == "") c.witsHeadder = SetHeadder();
if (c.witsFotter == "") c.witsFotter = SetFotter();
Configuration.Serialize("u2wits.xml", c);
// Set the read/write timeouts
_uniproPort.ReadTimeout = 500;
_uniproPort.WriteTimeout = 500;
_witsPort.ReadTimeout = 500;
_witsPort.WriteTimeout = 500;
_uniproPort.Open();
_witsPort.Open();
_continue = true;
readThread.Start();
Console.WriteLine("Type QUIT to exit");
while (_continue)
{
message = Console.ReadLine();
if (stringComparer.Equals("quit", message))
{
_continue = false;
}
}
readThread.Join();
_uniproPort.Close();
}
示例15: BUT_getcurrent_Click
//.........这里部分代码省略.........
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();
}
}
}
}
}
// 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].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]).Text = values[2].Trim();
}
}
else
{
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;
}