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


C# SerialPort.ReadLine方法代码示例

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


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

示例1: ParseResponse

        public bool ParseResponse(SerialPort serialPort)
        {
            try
            {
                // Dummy read of command;
                serialPort.ReadLine();

                var resp = serialPort.ReadLine().Trim();

                resp = resp.Replace('.', ',');

                Regex regex = new Regex(@"time=""(.*)"",P=""([0-9,]*)"",H=""([0-9,]*)"",T=""([0-9,]*)""");

                var match = regex.Match(resp);

                // Empty line
                serialPort.ReadLine();

                var ack = serialPort.ReadLine().Trim();

                Time = match.Groups[1].Value;
                Pressure = float.Parse(match.Groups[2].Value);
                Humidity = float.Parse(match.Groups[3].Value);
                Temperature = float.Parse(match.Groups[4].Value);

                return ack.Equals("OK");
            }
            catch (Exception)
            {
                return false;
            }
        }
开发者ID:eruanno123,项目名称:smart-pht,代码行数:32,代码来源:CmdGetFilteredValues.cs

示例2: ParseResponse

        public bool ParseResponse(SerialPort serialPort)
        {
            try
            {
                // Dummy read of command;
                serialPort.ReadLine();

                var resp = serialPort.ReadLine().Trim();

                Regex regex = new Regex(@"time=""(.*)"",P=""([0-9A-Fa-f]*)"",H=""([0-9A-Fa-f]*)"",T1=""([0-9A-Fa-f]*)""");

                var match = regex.Match(resp);

                // Empty line
                serialPort.ReadLine();

                var ack = serialPort.ReadLine().Trim();

                Time = match.Groups[1].Value;
                Pressure = int.Parse(match.Groups[2].Value, NumberStyles.AllowHexSpecifier);
                Humidity = int.Parse(match.Groups[3].Value, NumberStyles.AllowHexSpecifier);
                Temperature = int.Parse(match.Groups[4].Value, NumberStyles.AllowHexSpecifier);

                return ack.Equals("OK");
            }
            catch (Exception)
            {
                return false;
            }
        }
开发者ID:eruanno123,项目名称:smart-pht,代码行数:30,代码来源:CmdGetRawValues.cs

示例3: ParseResponse

        public bool ParseResponse(SerialPort serialPort)
        {
            try
            {
                StringBuilder sb = new StringBuilder(capacity: 50000);

                var s = serialPort.ReadLine();

                while (true)
                {
                    s = serialPort.ReadLine().Trim();

                    if (string.IsNullOrEmpty(s))
                        continue;

                    if (s == "OK") break;

                    sb.Append(s);
                }

                string bigStr = sb.ToString();

                SnapshotImage = MakeBitmap(bigStr);

                return true;
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); return false; }
        }
开发者ID:eruanno123,项目名称:smart-pht,代码行数:28,代码来源:CmdSnapshot.cs

示例4: connectDisplay

        public static bool connectDisplay(SerialPort serial)
        {
            if (serial == null || !serial.IsOpen)
            {
                throw new Exception(StringResource.E00001);
            }

            string accessString = "";
            try
            {
                accessString = serial.ReadLine();
            }
            catch (Exception)
            {
                //if Display is idle. Try to close it and reconnect
                serial.WriteLine("close:");

                try
                {
                    accessString = serial.ReadLine();
                }
                catch (TimeoutException)
                {
                    return false;
                }
            }

            if (accessString == DisplayToAppAccessString)
            {

                serial.WriteLine(AppToDisplayAccessString);
                return true;
            }
            return false;
        }
开发者ID:Grania,项目名称:ctm-project,代码行数:35,代码来源:DeviceControl.cs

示例5: acquire

        void acquire()
        {
            if(port!=null)
                port.Dispose();
            port = new SerialPort("COM4", 9600);
            port.Open();
            DateTime refreshTimer = DateTime.Now;
            while (!closeThread)
            {
                try
                {
                    while (port.BytesToRead > 0 && !closeThread)
                    {
                        port.ReadLine();
                        string data = port.ReadLine();

                        DateTime newTime = DateTime.Now;

                        if (newTime.Subtract(refreshTimer).Milliseconds > 30)
                        {
                            data = data.Substring(7);
                            string[] vals = data.Split(new char[] { ' ' });
                            double[] dvals = new double[vals.Length];
                            double total = 0;
                            for (int i = 0; i < dvals.Length; i++)
                            {
                                dvals[i] = double.Parse(vals[i].Trim());
                                total += dvals[i];
                            }

                            if (setScalingValues)
                            {
                                scalingValues = new double[dvals.Length];
                                for (int i = 0; i < scalingValues.Length; i++)
                                    scalingValues[i] = 255/dvals[i];
                                setScalingValues = false;
                            }

                            double avg = total / dvals.Length;

                            refreshTimer = newTime;

                            AddPoint(avg);
                            RefreshForm(dvals);
                            port.DiscardInBuffer();
                        }
                    }
                }
                catch (Exception) { }
                Thread.Sleep(10);
            }
            closeThread = false;
        }
开发者ID:kevroy314,项目名称:Arduino-Light-Sensor-GUI,代码行数:53,代码来源:Form1.cs

示例6: Main

        public static void Main(string[] args)
        {
            activeConfig = new SerialMonitorConfig ("Serial.conf",args);

            //assign time outs to all network commands
            foreach (int  i in activeConfig.networkCommands.Keys) {
                timeouts.Add (i, DateTime.Now);
            }

            stamp ();

            SerialPort serialPort = new SerialPort ();
            serialPort.BaudRate = activeConfig.baudrate;
            serialPort.PortName = activeConfig.portname;
            //serialPort.Parity = activeConfig.Parity;
            serialPort.ReadTimeout = activeConfig.readtimeout;
            serialPort.WriteTimeout = activeConfig.writetimeout;

            serialPort.Open ();
            string message;
            while (true) {
                try{
                    message= serialPort.ReadLine ();

                    int cmd = 0;
                    if(int.TryParse(message, out cmd)){
                        readCommand(cmd);
                    }

                }
                catch(Exception ex){
                    //
                }
            }
        }
开发者ID:lbrunjes,项目名称:poop-cloud,代码行数:35,代码来源:Program.cs

示例7: SPICommunicator

        public SPICommunicator()
        {
            port = new SerialPort() {
                BaudRate = 115200,
                DataBits = 8,
                StopBits = StopBits.One,
                Parity = Parity.None,
                PortName = "COM3",
                Handshake = Handshake.None
            };

            if (!port.IsOpen) {
                port.Open();
            }

            string line = "TEST";
            int i = 0;
            do {
                port.Write(line);
                string inLine = port.ReadLine();
                i++;
            } while (i < 3);

            port.Close();
        }
开发者ID:petegleeson,项目名称:engg4810-t20-pc,代码行数:25,代码来源:SPICommunicator.cs

示例8: Main

        static void Main(string[] args)
        {
            try
            {
                Serfid appSerfid = new Serfid();
                appSerfid.Run();

                _serfidPort = new SerialPort("COM3");
                _serfidPort.Open();

                System.Console.WriteLine("Serfid reading on port: COM3");
                while (true)
                {
                    System.Console.WriteLine("\nWaiting for readings...");
                    string reading = _serfidPort.ReadLine();
                    appSerfid.ReadWeft(reading);

                    System.Console.SetCursorPosition(0, System.Console.CursorTop - 1);
                    string result = string.Format("Tag {0} processed successful", reading.Replace("\r", ""));
                    System.Console.WriteLine(result);
                }
            }
            finally
            {
                _serfidPort.Close();
            }
        }
开发者ID:nicoandresr,项目名称:serfid-lfs,代码行数:27,代码来源:Program.cs

示例9: Reading

        private void Reading()
        {
            var currentPort = new SerialPort(_name, 115200);
            currentPort.Open();
            while (!stop)
            {
                var line = currentPort.ReadLine();
                try
                {
                    var strings = line.Replace("\r", "").Split(',');
                    var x = (int)int.Parse(strings[0]) / 16384.0F;
                    var y = (int)int.Parse(strings[1]) / 16384.0F;
                    var z = (int)int.Parse(strings[2]) / 16384.0F;
                    if (!_valuesInitialized)
                    {
                        _x = x;
                        _y = y;
                        _z = z;
                        _valuesInitialized = true;
                    }

                    _x = (_x*3 + x)/4;
                    _y = (_y*3 + y)/4;
                    _z = (_z*3 + z)/4;
                }
                catch (Exception)
                {

                }

                HasNew = true;
            }
        }
开发者ID:x4m,项目名称:lr17,代码行数:33,代码来源:AccelListener.cs

示例10: Main

        static void Main(string[] args)
        {
            String RxedData;                         // String to store received data
                String COM_PortName;                     // String to store the name of the serial port
                SerialPort MyCOMPort = new SerialPort(); // Create a new SerialPort Object

                Console.WriteLine("\t+------------------------------------------+");
                Console.WriteLine("\t|    Reading from Serial Port using C#     |");
                Console.WriteLine("\t+------------------------------------------+");
                Console.WriteLine("\t|          (c) www.xanthium.in             |");
                Console.WriteLine("\t+------------------------------------------+");
                Console.Write("\n\t  Enter the COM port [eg COM32] -> ");
                COM_PortName = Console.ReadLine();

                COM_PortName = COM_PortName.Trim();     // Remove any extra spaces
                COM_PortName = COM_PortName.ToUpper();  // convert the string to upper case

                //------------ COM port settings to 8N1 mode ------------------//

                MyCOMPort.PortName = COM_PortName;       // Name of the COM port
                MyCOMPort.BaudRate = 9600;               // Baudrate = 9600bps
                MyCOMPort.Parity   = Parity.None;        // Parity bits = none
                MyCOMPort.DataBits = 8;                  // No of Data bits = 8
                MyCOMPort.StopBits = StopBits.One;       // No of Stop bits = 1

                MyCOMPort.Open();                                 // Open the port
                Console.WriteLine("\n\t  Waiting for Data....");
                RxedData = MyCOMPort.ReadLine();                  // Wait for data reception
                Console.WriteLine("\n\t  \" {0} \"",RxedData);    // Write the data to console
                Console.WriteLine("\n\t  Press any Key to Exit");
                Console.Read();                                   // Press any key to exit

                MyCOMPort.Close();                       // Close port
        }
开发者ID:xanthium-enterprises,项目名称:Serial-Programming-CSharp,代码行数:34,代码来源:SerialPort_Read_UI.cs

示例11: Main

        public static void Main()
        {
            string ttyname = "";         // for storing name of the serial port
            string ReceivedData = "";    // for storing received data from microcontroller

            SerialPort Serial_tty = new SerialPort(); //Create a new serialPort object

            Console.Write("\nEnter Your SerialPort[tty] name (eg ttyUSB0)->");
            ttyname = Console.ReadLine();             // Read the name of the port eg ttyUSB0
            ttyname = @"/dev/" + ttyname;             // Concatanate so name looks like /dev/ttyUSB0

            Serial_tty.PortName = ttyname;            // assign the port name
            Serial_tty.BaudRate = 9600;               // Baudrate = 9600bps
            Serial_tty.Parity   = Parity.None;        // Parity bits = none
            Serial_tty.DataBits = 8;                  // No of Data bits = 8
            Serial_tty.StopBits = StopBits.One;       // No of Stop bits = 1

            try
            {
                Serial_tty.Open();                             //open the port
                Console.WriteLine("\nWaiting for Data......");
                ReceivedData = Serial_tty.ReadLine();     // Read the data send from microcontroller
                Serial_tty.Close();                       // Close port
                Console.WriteLine("\n{0} received\n", ReceivedData);
            }
            catch
            {
                Console.WriteLine("Error in Opening {0}\n",Serial_tty.PortName);
            }
        }
开发者ID:xanthium-enterprises,项目名称:Serial-Programming-mono-CSharp-Linux,代码行数:30,代码来源:MonoSerialRead.cs

示例12: Main

        static void Main(string[] args)
        {
            ////////////// KONSOL AYARLARI ///////////////////////////////////
            Console.SetWindowSize(70, 30);
            Console.BackgroundColor = ConsoleColor.DarkBlue;
            Console.ForegroundColor = ConsoleColor.White;
            Console.Clear();
            /////////////////////////////////////////////////////////////////

            /////////////////////////////////////////////////////////////////
            SerialPort myport = new SerialPort();
            myport.BaudRate = 9600;
            myport.PortName = "COM3";
            myport.Open();
            /////////////////////////////////////////////////////////////////

            /////////////////////////////////////////////////////////////////
            bool devametsinmi = true;
            while (devametsinmi && true)
            {
                string data_rx = myport.ReadLine();
                Console.WriteLine(data_rx);
            }
            /////////////////////////////////////////////////////////////////
        }
开发者ID:TJMicroPower,项目名称:dht11_comport,代码行数:25,代码来源:Program.cs

示例13: ParseReceivedLine

        private static decimal ParseReceivedLine(SerialPort port)
        {
            string line = port.ReadLine();
            var match = Regex.Match(line, @"^(\d+\.\d+)\s*$");
            var degrees = decimal.Parse(match.Groups[1].Value);

            return degrees;
        }
开发者ID:nzbart,项目名称:Atmega328TempLogger,代码行数:8,代码来源:Program.cs

示例14: Main

        static void Main(string[] args)
        {
            _port = new SerialPort();
            _port.PortName = "COM3";
            _port.BaudRate = 9600;

            _port.ReadTimeout = 500;
            _port.WriteTimeout = 500;

            _port.Open();

            Thread thread = new Thread(new ThreadStart(delegate()
                {
                    while (_continue)
                    {
                        try
                        {
                            string data = _port.ReadLine();
                            string[] bits = data.Replace("\r", "").Split('\t').ToArray();
                            Console.WriteLine(data);

                            if (bits.Length == 3 && bits[0].Length > 0 && bits[1].Length > 0 && bits[2].Length > 0)
                            {
                                int xOriginal = int.Parse(bits[1]);
                                int yOriginal = int.Parse(bits[2]);

                                if (Math.Abs(xOriginal) > 50 || Math.Abs(yOriginal) > 50)
                                {
                                    Cursor.Position = new System.Drawing.Point
                                    {
                                        X = Cursor.Position.X + -1 * xOriginal / 10,
                                        Y = Cursor.Position.Y + yOriginal / 10
                                    };
                                }

                                if (bits[0] == "1" && !_inClick)
                                {
                                    _inClick = true;
                                    mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, (uint)Cursor.Position.X, (uint)Cursor.Position.Y, 0, 0);
                                }
                                else if (bits[0] == "0")
                                {
                                    _inClick = false;
                                }
                            }
                        }
                        catch (TimeoutException) { }
                    }
                }));
            thread.Start();

            Console.WriteLine("Hit ENTER to quit.");
            Console.ReadLine();

            _continue = false;
            thread.Join();
        }
开发者ID:kwende,项目名称:haymakers,代码行数:57,代码来源:Program.cs

示例15: GetBridgeSignature

 // Gets the uart->spi bridge's signature (MSP430 etc)
 public static string GetBridgeSignature(SerialPort sp)
 {
     //sp.Write("{g00}");
     sp.Write("{");
     sp.Write("g");
     sp.Write("0");
     sp.Write("0");
     sp.Write("}");
     return sp.ReadLine();
 }
开发者ID:jglim,项目名称:CCManager,代码行数:11,代码来源:Serial.cs


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