本文整理汇总了C#中SerialPort.Open方法的典型用法代码示例。如果您正苦于以下问题:C# SerialPort.Open方法的具体用法?C# SerialPort.Open怎么用?C# SerialPort.Open使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SerialPort
的用法示例。
在下文中一共展示了SerialPort.Open方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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();
}
}
示例2: ExampleThreadFunction
public static void ExampleThreadFunction()
{
// bit rate change acording your GPS
SerialPort serialPort = new SerialPort("COM3", 4800);
serialPort.Open();
//here we create file in SD card main folder
string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;
FileStream FileHandle = new FileStream(rootDirectory + @"\gps.txt", FileMode.Create);
while (true)
{
int bytesToRead = serialPort.BytesToRead;
if (bytesToRead > 0)
{
// all struff from GPS streams in to file
byte[] buffer = new byte[bytesToRead];
serialPort.Read(buffer, 0, buffer.Length);
Debug.Print(new String(System.Text.Encoding.UTF8.GetChars(buffer)) + "\n");
FileHandle.Write(buffer, 0, buffer.Length);
Thread.Sleep(500);
}
// Cleaning
Debug.GC(true);
Debug.EnableGCMessages(false);
}
}
示例3: Start
// Use this for initialization
void Start()
{
//Screen.showCursor = false;
isPlaying = false;
//Health = 100;
isDead = false;
Health = 100;
_curHealth = Health;
PlArmIdentity = PlayerArm.transform.rotation;
Instantiate(Spells[0], PlayerArm.transform.GetChild(0).position, Quaternion.identity);
CastingSpell = GameObject.FindGameObjectWithTag("Spell");
string[] ports = SerialPort.GetPortNames();
// foreach (string port in ports)
// {
// Console.WriteLine(port);
// }
_serialPort = new SerialPort(ports[1], 9600);
_serialPort.Open();
Debug.Log(_serialPort.PortName);
Debug.Log(_serialPort.BaudRate);
Debug.Log(_serialPort.IsOpen);
}
示例4: Start
// Use this for initialization
void Start()
{
stm = new SerialPort("COM3", 38400, Parity.None, 8, StopBits.One);
try
{
stm.Open();
if (!stm.IsOpen)
{
Debug.LogWarning("Nie udalo sie otworzyc polaczenia z portem: " + nazwaPortu);
Destroy(this);
}
else
{
Debug.Log("Otwarto polaczenie z portem: " + nazwaPortu);
stmThread = new Thread(stmOdczyt);
stmThread.Start();
}
}
catch (Exception e)
{
Debug.LogWarning("Nie udalo sie otworzyc polaczenia z portem: " + nazwaPortu);
Destroy(this);
}
}
示例5: Start
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody>();
stream = new SerialPort("COM4", 9600);
stream.ReadTimeout = 50;
stream.Open();
}
示例6: Main
public static void Main()
{
Debug.Print("BLE Shield");
var port = SerialPorts.COM1; // using D0 & D1
//var port = SerialPorts.COM2; // using D2 & D3
var bleShield = new SerialPort(port, 19200, Parity.None, 8, StopBits.None);
bleShield.DataReceived += (sender, args) =>
{
var receiveBuffer = new byte[16];
int bytesReceived = bleShield.Read(receiveBuffer, 0, receiveBuffer.Length);
if (bytesReceived > 0)
{
Debug.Print("Bytes received: " + bytesReceived);
Debug.Print(new String(Encoding.UTF8.GetChars(receiveBuffer)));
}
};
bleShield.Open();
while (true)
{
var random = new Random();
var sendBuffer = new byte[4];
random.NextBytes(sendBuffer);
bleShield.Write(sendBuffer, 0, sendBuffer.Length);
Thread.Sleep(1000);
}
}
示例7: Start
// Use this for initialization
void Start()
{
nazwaPortu = COM.com;
// utworzenie obiektu do komunikacji na danym porcie
stm = new SerialPort(nazwaPortu, 38400, Parity.None, 8, StopBits.One);
// proba otwarcia portu
try
{
stm.Open();
if (!stm.IsOpen)
{
// jezeli nie udalo sie utworzyc portu to komponent jest likwidowany
Debug.LogWarning("Nie udalo sie otworzyc polaczenia z portem: " + nazwaPortu);
Destroy(this);
}
else
{
Debug.Log("Otwarto polaczenie z portem: " + nazwaPortu);
// udalo sie utworzyc polaczenie z portem - startuje watek do czytania danych z portu
stmThread = new Thread(stmOdczyt);
stmThread.Start();
}
}
catch (Exception e)
{
// jezeli nie udalo sie utworzyc portu to komponent jest likwidowany
Debug.LogWarning("Nie udalo sie otworzyc polaczenia z portem: " + nazwaPortu);
Destroy(this);
}
}
示例8: GetMavStreamFromArgs
public static Stream GetMavStreamFromArgs(string[] args)
{
Stream strm = null;
if (args.Length == 0)
{
strm = Console.OpenStandardInput();
}
else if (args.Length == 1)
{
Console.WriteLine("Usage (todo)");
Console.ReadKey();
Environment.Exit(0);
}
else if (args[0] == "-S")
{
if (args.Length != 3)
{
Console.WriteLine("Usage (todo)");
Environment.Exit(1);
}
var comport = args[1];
var baud = Convert.ToInt32(args[2]);
var port = new SerialPort(comport, baud);
port.Open();
strm = port.BaseStream;
}
else
{
strm = File.OpenRead(args[1]);
}
return strm;
}
示例9: Start
// Use this for initialization
void Start () {
serialPort = new SerialPort ("/dev/cu.usbmodem1421", 57600);
serialPort.Open ();
serialPort.ReadTimeout = 1;
}
示例10: spInit
private void spInit()
{
sp = new SerialPort();
sp.PortName = "COM40";
sp.BaudRate = 57600;
sp.DataBits = 8;
sp.Parity = Parity.Even;
try
{
sp.Open();
try
{
sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);//事件
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
catch (Exception)
{
MessageBox.Show("无法连接串口");
}
}
示例11: Open
public void Open () {
// Opens the serial port
stream = new SerialPort(port, baudrate);
stream.ReadTimeout = 50;
stream.Open();
//this.stream.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
}
示例12: 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();
}
示例13: 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();
}
示例14: runProgram
public static void runProgram(SerialPort port)
{
//System.IO.Ports.SerialPort serialPort1;
//System.ComponentModel.IContainer components = new System.ComponentModel.Container();
//serialPort1 = new System.IO.Ports.SerialPort(components);
//serialPort1.PortName = "COM4";
//serialPort1.BaudRate = 9600;
port.Open();
// this turns on !
// serialPort1.DtrEnable = true;
Console.Write("starting loop");
while (true)
{
//port.Write(new byte[] { 1 }, 0, 1);
//System.Threading.Thread.Sleep(1000);
//port.Write(new byte[] { 0 }, 0, 1);
//System.Threading.Thread.Sleep(1000);
Transmit(port, 63, 63, 40, 63);
//wait for received
while (port.BytesToRead==0)
{
System.Threading.Thread.Sleep(10);
}
if (port.ReadByte() != 103)
{
Console.Write("Bad ack");
break;
}
}
}
示例15: Open
public void Open () {
// Opens the serial port
stream = new SerialPort("COM3", baudrate);
stream.ReadTimeout = 50;
stream.Open();
//this.stream.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
Debug.Log(ReadFromArduino());
}