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


C# SerialPort.Write方法代码示例

本文整理汇总了C#中SerialPort.Write方法的典型用法代码示例。如果您正苦于以下问题:C# SerialPort.Write方法的具体用法?C# SerialPort.Write怎么用?C# SerialPort.Write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在SerialPort的用法示例。


在下文中一共展示了SerialPort.Write方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: 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

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

示例3: Start

 void Start()
 {
     ports = SerialPort.GetPortNames();
     stream = new SerialPort(ports[0], 57600);
     //stream = new SerialPort(/*ports[0]*/"COM13", 57600);
     stream.Open();
     stream.Write(" ");
 }
开发者ID:ricklon,项目名称:Turret-Game,代码行数:8,代码来源:danceAfterKill.cs

示例4: testSerialWrite

	public void testSerialWrite(SerialPort _stream){
		if (_stream.IsOpen) {
			if (Input.GetKeyDown (KeyCode.Q)) {
				_stream.Write ("3");
				Debug.Log ("3");
			} else if (Input.GetKeyUp (KeyCode.Q)) {
				_stream.Write ("4");
				Debug.Log ("4");
			}

			else if (Input.GetKeyDown (KeyCode.O)) {
				_stream.Write ("5");
				Debug.Log ("5");
			} else if (Input.GetKeyUp (KeyCode.O)) {
				_stream.Write ("6");
				Debug.Log ("6");
			}
		}
		else{
			Debug.Log("srial write");
		}
	}
开发者ID:ekusy,项目名称:Wall-of-Kabe,代码行数:22,代码来源:testFunction.cs

示例5: 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();
    }
开发者ID:tummyacid,项目名称:VS2015,代码行数:49,代码来源:Program.cs

示例6: 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();
     }
 }
开发者ID:dnet,项目名称:sidbox-ctrl,代码行数:16,代码来源:usb.cs

示例7: ExecCommand

    public string ExecCommand(SerialPort port, string command, int responseTimeout, string errorMessage)
    {
        try
            {

                port.DiscardOutBuffer();
                port.DiscardInBuffer();

                port.Write(command + "\r");

                return "Success";
            }
            catch (Exception ex)
            {
                throw ex;
            }
    }
开发者ID:ShoeTurtle,项目名称:MyFooBar,代码行数:17,代码来源:MailAndSms.cs

示例8: findArduino

    private SerialPort findArduino()
    {
        SerialPort testPort;
        string[] ports = SerialPort.GetPortNames ();

        foreach (string port in ports) {
            testPort = new SerialPort(port, 9600);
            testPort.Open();
            testPort.ReadTimeout = 50;
            testPort.WriteTimeout = 50;
            byte[] c = {0xc0};
            testPort.Write(c, 0, 1);
            int msg = testPort.ReadChar();
            if (msg == 121) {
                Debug.LogWarning("found the arduino on port " + port);
                return testPort;
            }
        }

        Debug.LogWarning ("didn't find the arduino");
        return null;
    }
开发者ID:NYU-MRL,项目名称:UnityAirJump,代码行数:22,代码来源:FanController.cs

示例9: request_DoWork

    public void request_DoWork(object sender, DoWorkEventArgs e)
    {
        var worker = (BackgroundWorker)sender;

        worker.ReportProgress(0);

        ClsComSettingMain clsComSettingMain = (ClsComSettingMain)e.Argument;
        string comPort = clsComSettingMain.comport;
        int baudRate = clsComSettingMain.baudRate;

        if (comPort != null && baudRate != 0)
        {
            SerialPort serialPort = new SerialPort(comPort, baudRate);
            serialPort.DataReceived += new SerialDataReceivedEventHandler(process_DoWorkHandler);
            serialPort.Open();

            while (true)
            {
                if (worker.CancellationPending)
                {
                    worker.ReportProgress(100);
                    e.Cancel = true;
                    break;
                }
                else
                {
                    serialPort.DiscardInBuffer(); // Eingangspuffer leeren
                    serialPort.Write(adv_request, 0, 3); // Write byte array to serial port, with no offset, all 3 bytes

                }
            }
        }
    }
开发者ID:stoamandl,项目名称:PowerTune,代码行数:33,代码来源:MainWindow.xaml.cs

示例10: handleRs232c

    private returnType handleRs232c(ref SerialPort mySP, ref UdpClient client, string fromUdp, int fromPort)
    {
        // UDP to RS-232C
        try {
            if (fromUdp.Length > 0) {
                mySP.Write (fromUdp);
                fromUdp = "";
            }
        }
        catch (System.Exception) {
        }

        // RS-232C to UDP
        try {
            byte rcv = (byte)mySP.ReadByte();
            if (rcv != 255) {
                char tmp = (char)rcv;
                byte [] data = System.Text.Encoding.ASCII.GetBytes(tmp.ToString());
                client.Send(data, data.Length, ipadr1, fromPort);
                return returnType.Continue;
            }
        } catch (System.Exception) {
        }

        return returnType.FallThrough;
    }
开发者ID:yasokada,项目名称:unity-150923-udpRs232c,代码行数:26,代码来源:udpRs232cScript.cs

示例11: Main

    static void Main()
    {
        try
        {
            for (;;)
            {
                string port = null;
                SerialPort s = null;
                {
                    var pp = new List<string>();
                    for (int p = 0; p < 10; ++p)
                        pp.Add(string.Format("/dev/ttyACM{0}", p));
                    for (int p = 0; p < 10; ++p)
                        pp.Add(string.Format("/dev/ttyUSB{0}", p));

                    bool ok = false;
                    do
                    {
                        for (int p = 0; p < pp.Count; ++p)
                        {
                            try
                            {
                                port = pp[p];
                                s = new SerialPort(port);
                                //s.BaudRate = 19200;
                                //s.BaudRate = 115200;
                                s.BaudRate = 38400;
                                s.DataBits = 8;
                                s.Parity = Parity.None;
                                s.StopBits = StopBits.One;
                                s.Handshake = Handshake.None;
                                s.DtrEnable = false;   // cosi non resetta
                                s.ReadTimeout = -1;
                                s.Open();

                                ok = true;
                                break;
                            }
                            catch (Exception)
                            {
                            }
                        }
                    }
                    while(ok == false);
                }

                Console.WriteLine("Port open on {0}", port);

                for (;;)
                {
                    int b = s.ReadByte();
                    if (b == '7')
                    {
                        var n = (DateTime.Now - new DateTime (1970, 1, 1)).TotalSeconds;
                        var str = String.Format("T{0} ", n);

                        s.Write(str);
                        Console.WriteLine("Sync done");
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Task.Run error. {0}", ex);
        }
    }
开发者ID:lstefani006,项目名称:teensy,代码行数:67,代码来源:ArduinoSync.cs

示例12: Transmit

 public static void Transmit(SerialPort port, int x1, int x2, int x3, int x4)
 {
     port.Write(new byte[] {(byte) x1,(byte) x2,(byte) x3,(byte) x4, 103}, 0, 5);
 }
开发者ID:darrenks,项目名称:The-Force,代码行数:4,代码来源:heliserialtest.cs

示例13: checkPort

 public void checkPort(int player)
 {
     switch (player)
     {
         case 1:
             if (portStr.Contains(player1.text))
             {
                 stream1 = new SerialPort(player1.text, 115200);
                 stream1.Open();
                 stream1.Write("v");
                 string version = stream1.ReadLine();
                 if (version == "")
                 {
                     accept1.sprite = no;
                     GameData.Instance.Port1Open = 0;
                 }
                 else
                 {
                     accept1.sprite = yes;
                     GameData.Instance.Port1Open = 1;
                     GameData.Instance.Port1 = player1.text;
                 }
             }
             else
             {
                 accept1.sprite = no;
                 GameData.Instance.Port1Open = 0;
             }
             break;
         case 2:
             if (portStr.Contains(player2.text))
             {
                 stream2 = new SerialPort(player2.text, 115200);
                 stream2.Open();
                 stream2.Write("v");
                 string version = stream2.ReadLine();
                 if (version == "")
                 {
                     accept2.sprite = no;
                     GameData.Instance.Port2Open = 0;
                 }
                 else
                 {
                     accept2.sprite = yes;
                     GameData.Instance.Port2Open = 1;
                     GameData.Instance.Port2 = player2.text;
                 }
             }
             else
             {
                 accept2.sprite = no;
                 GameData.Instance.Port2Open = 0;
             }
             break;
         case 3:
             if (portStr.Contains(player3.text))
             {
                 stream3 = new SerialPort(player3.text, 115200);
                 stream3.Open();
                 stream3.Write("v");
                 string version = stream3.ReadLine();
                 if (version == "")
                 {
                     accept3.sprite = no;
                     GameData.Instance.Port3Open = 0;
                 }
                 else
                 {
                     accept3.sprite = yes;
                     GameData.Instance.Port3Open = 1;
                     GameData.Instance.Port3 = player3.text;
                 }
             }
             else
             {
                 accept3.sprite = no;
                 GameData.Instance.Port3Open = 0;
             }
             break;
     }
 }
开发者ID:Avensuhra,项目名称:Interaktion,代码行数:81,代码来源:Menu.cs

示例14: checkPorts

    public void checkPorts()
    {
        SerialPort stream1 = new SerialPort(GameData.Instance.Port1, 115200);
        SerialPort stream2 = new SerialPort(GameData.Instance.Port2, 115200);
        SerialPort stream3 = new SerialPort(GameData.Instance.Port3, 115200);

        if (GameData.Instance.Port1Open == 1)
        {
            stream1.Open();
        }
        if (GameData.Instance.Port2Open == 1)
        {
            stream2.Open();
        }
        if (GameData.Instance.Port3Open == 1)
        {
            stream3.Open();
        }

        if (stream1.IsOpen)
        {
            stream1.Write("v");
            string version1 = stream1.ReadLine();
            if (version1 == "")
            {
                p1 = false;
            }
            else
            {
                p1 = true;
            }
        }

        if (stream2.IsOpen)
        {
            stream2.Write("v");
            string version2 = stream2.ReadLine();
            if (version2 == "")
            {
                p2 = false;
            }
            else
            {
                p2 = true;
            }
        }

        if (stream3.IsOpen)
        {
            stream3.Write("v");
            string version3 = stream3.ReadLine();
            if (version3 == "")
            {
                p3 = false;
            }
            else
            {
                p3 = true;
            }
        }

        int p = 0;
        if (p1)
        {
            player1 = stream1;
            p++;
        }
        if (p2)
        {
            p++;
        }
        if (p3)
        {
            p++;
        }

        print("Players: " + p + ", " + p1);
        if (p == 0)
        {
            print("ERROR");
        }
        if (p != playerNumber)
        {
            initCamera(p);
            playerNumber = p;
        }
    }
开发者ID:Avensuhra,项目名称:Interaktion,代码行数:87,代码来源:Controller.cs

示例15: writeArduino

    void writeArduino(char _data,SerialPort stream1)
    {
        if (stream1.IsOpen)
        {
            while (true)
            {
                try
                {
                    stream1.Write(_data.ToString());

                }
                catch (TimeoutException e)
                {
                    Debug.Log("time out Write:" + _data);
                }
                Debug.Log("OK test serial,send:" + _data.ToString());
                break;
            }
        }
        else
        {
            Debug.Log("not connected serial");
        }
    }
开发者ID:ekusy,项目名称:Kabe-script,代码行数:24,代码来源:testFunction.cs


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