当前位置: 首页>>代码示例>>C#>>正文


C# SerialPort.Open方法代码示例

本文整理汇总了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();
        }
    }
开发者ID:kurtvonehr,项目名称:ARP-Windows-Service,代码行数:36,代码来源:wifi_doorlock_service.cs

示例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);

            }

        }
开发者ID:BdGL3,项目名称:CXPortal,代码行数:33,代码来源:FileIO.cs

示例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);
    }
开发者ID:kdittle,项目名称:PhysicalComputing,代码行数:30,代码来源:PlayerScript.cs

示例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);
        }
    }
开发者ID:aagnieszka,项目名称:Pong-STM,代码行数:29,代码来源:STMController.cs

示例5: Start

	// Use this for initialization
	void Start () {
        rb = GetComponent<Rigidbody>();

        stream = new SerialPort("COM4", 9600);
        stream.ReadTimeout = 50;
        stream.Open();
    }
开发者ID:singlerider,项目名称:head-tracker,代码行数:8,代码来源:paddle.cs

示例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);
     }
 }
开发者ID:thisischris,项目名称:BLE-Shield,代码行数:26,代码来源:Program.cs

示例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);
        }
	}
开发者ID:glocklueng,项目名称:STMPong,代码行数:31,代码来源:STMController.cs

示例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;
    }
开发者ID:mandrolic,项目名称:pymavlink,代码行数:33,代码来源:ExampleCommon.cs

示例9: Start

	// Use this for initialization
	void Start () {
		serialPort = new SerialPort ("/dev/cu.usbmodem1421", 57600);
		serialPort.Open ();
		serialPort.ReadTimeout = 1;
		
		
	}
开发者ID:JonathanMcCaffrey,项目名称:ArdunioUnityControl,代码行数:8,代码来源:ArdunioControls.cs

示例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("无法连接串口");
            }
        }
开发者ID:ShallYu,项目名称:mood_massage,代码行数:25,代码来源:SerialData.cs

示例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);
	}
开发者ID:jpmckinney,项目名称:The-Machine-to-be-Another-Unity,代码行数:7,代码来源:arduinoControl.cs

示例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();
    }
开发者ID:GNOME,项目名称:chronojump,代码行数:26,代码来源:chronopic.cs

示例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();
    }
开发者ID:am-t,项目名称:mount-amanita,代码行数:31,代码来源:ArduinoSerialInterface.cs

示例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;
            }
        }
    }
开发者ID:darrenks,项目名称:The-Force,代码行数:32,代码来源:heliserialtest.cs

示例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());
 }
开发者ID:mongy910,项目名称:ActiveVR,代码行数:8,代码来源:ArduinoConnector.cs


注:本文中的SerialPort.Open方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。