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


C# SerialPort.ReadLine方法代码示例

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


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

示例1: 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);
 }
开发者ID:g23988,项目名称:holyshit,代码行数:46,代码来源:main.cs

示例2: GetArduino

    private void GetArduino()
    {
        stream = new SerialPort(COMPort, 19200);
        stream.Open();

        Debug.Log("123");
        while (myThread.IsAlive && !bclose) {

            string value = stream.ReadLine(); //Read the information
            string[] vec3 = value.Split(',');
            if (vec3.Length >= 4)
            {
                fx_axis = Convert.ToSingle(vec3[0]);
                fy_axis = Convert.ToSingle(vec3[1]);
                fsound = Convert.ToSingle(vec3[2]);
                fbtn = Convert.ToSingle(vec3[3]);
            }

        }
    }
开发者ID:ntuetom,项目名称:NoiseRusher,代码行数:20,代码来源:SerialS.cs

示例3: OpenConnection

    public bool OpenConnection()
    {
        try
        {
            serialPort = new SerialPort("\\\\.\\" + portName, serialSpeed);
            serialPort.Open();
            if (serialPort.IsOpen)
            {
                serialPort.ReadTimeout = 1000 / 120; // this is half a unity frame
                serialPort.WriteTimeout = 1000 / 120;
                serialPort.WriteLine("arduinity_check"); // send message to arduino

                string reply = "";

                while (reply == "")
                {
                    reply = serialPort.ReadLine();
                    if (reply != null && reply != "")
                    {
                        if (reply.StartsWith("arduinity_present"))
                        {
                            #if debugSerial
                            Debug.Log("ArduinityCommunicator::OpenConnection(): found arduinity on " + portName);
                            #endif

                            // parse the name
                            arduinoName = reply.Substring(reply.LastIndexOf('_') + 1);

                            return true;
                        }
                        else
                        {
                            #if debugSerial
                            Debug.Log("ArduinityCommunicator::OpenConnection(): got wrong response on " + portName + " " + reply);
                            #endif

                            return false;
                        }
                    }
                }
            }
            else
            {
                #if debugSerial
                Debug.Log("ArduinityCommunicator::OpenConnection(): " + portName + " is not open");
                #endif
                return false;
            }
        }
        #if debugSerial
        catch(Exception e)
        {
            Debug.Log("ArduinityCommunicator::OpenConnection(): exception trying to read from " + portName + " " + e.Message);
            return false;
        }
        #else
        catch
        {
            return false;
        }
        #endif
        return false;
    }
开发者ID:tamkadro,项目名称:Arduinity,代码行数:63,代码来源:ArduinityCommunicator.cs

示例4: Start

    /// <summary>
    /// Initialisation of the Arduino IO-Box script. 
    /// </summary>
    /// 
    public void Start()
    {
        if ( !scriptVehicleData   ) Debug.LogError("No vehicle data collector script defined!");
        if ( !scriptConfiguration ) Debug.LogError("No simulation configuration script defined!");
        vehicleData = null;
        speedOfSound = scriptConfiguration.GetSpeedOfSound();

        serialPort = new SerialPort(modulePort, moduleSpeed, Parity.None, 8, StopBits.One);
        serialPort.Handshake   = Handshake.None;
        serialPort.RtsEnable   = false;
        serialPort.DtrEnable   = false;  // Disable DTR so NOT to reset Arduino board when connecting
        serialPort.ReadTimeout = 250;    // longer read timeout (module might have had a reset nevertheless)

        bool success = false;
        try
        {
            serialPort.Open();
        }
        catch (IOException)
        {
            serialPort = null;
        }

        if ( (serialPort != null) && serialPort.IsOpen )
        {
            int repeats = 8; // try several times to connect
            while ( (repeats-- > 0) && !success )
            {
                // send the ECHO command
                serialPort.WriteLine(CMD_ECHO);
                try
                {
                    String serialNo = serialPort.ReadLine();
                    if ( serialNo.Length > 1 )
                    {
                        Debug.Log ("Opened serial port " + modulePort + " to Arduino IO (Version: " + serialNo + ")");
                        success  = true;
                        serialPort.ReadTimeout = 50; // from now on, shorter response times, please
                        StartCoroutine(RunDiagnose());
                    }
                }
                catch (TimeoutException)
                {
                    // ignore
                }
            }
        }

        if ( !success )
        {
            Debug.LogError("Could not open serial port " + modulePort + " to the Arduino IO module.");
            if ( serialPort != null )
            {
                serialPort.Close();
                serialPort = null;
            }
        }
    }
开发者ID:stefanmarks,项目名称:jetblack-arduino-io-box,代码行数:62,代码来源:ArduinoIO_Module.cs

示例5: Read

 string Read(SerialPort port)
 {
     string message = "";
     try {
         for (int i=0; i<maxLinesPerBatch; ++i) {
             if (port.BytesToRead > 0) {
                 message = port.ReadLine();
             }
         }
         if (port.BytesToRead > 0) {
             port.ReadExisting();
         }
     } catch (Exception e) {
          // swallow read timeout exceptions
         if (e.GetType() == typeof(TimeoutException))
             return message;
         else
             throw;
     }
     return message;
 }
开发者ID:docilio,项目名称:BalanceGame,代码行数:21,代码来源:SerialParser.cs

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

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


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