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


C# SerialPort.ReadChar方法代码示例

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


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

示例1: WritePage

 public static void WritePage(byte[] pageData, int pageNumber, int pageSize, SerialPort port)
 {
     port.Write(new byte[1]{Convert.ToByte(pageNumber)},0,1);
     port.Write(pageData,pageNumber*pageSize,pageSize);
     if(port.ReadChar()!='A')
         throw new Exception("No acknowledge from target!");
 }
开发者ID:halitalptekin,项目名称:Little-Wire,代码行数:7,代码来源:FirmwareLink.cs

示例2: SearchPort

        public static AsyncSerial SearchPort(string write, char search, int baud=9600)
        {
            foreach (string port in SerialPort.GetPortNames())
            {
                SerialPort temp;
                try
                {
                    temp = new SerialPort(port, baud);
                    try
                    {
                        temp.Open();
                        temp.ReadTimeout = 2500;
                        temp.Write(write);
                        var v = temp.ReadChar();
                        MainForm.Instance.logControl.Add(v.ToString(), LogEntryType.Error);
                        if (v == search)
                        {
                            temp.Close();
                            return new AsyncSerial(port);
                        }
                    }
                    catch (Exception ex)
                    {}
                    finally
                    {
                        temp.Close();
                    }
                }
                catch (Exception ex)
                {}
            }

            throw new Exception("Port not found.");
        }
开发者ID:DavidTyler,项目名称:Door-Lock,代码行数:34,代码来源:AsyncSerial.cs

示例3: DetectSerialCom

 private bool DetectSerialCom()
 {
     foreach (var portName in SerialPort.GetPortNames())
     {
         _serialPort = new SerialPort(portName, 38400, Parity.None, 8, StopBits.One) { ReadTimeout = 10 };
         try
         {
             _serialPort.Open();
         }
         catch (IOException)
         {
             continue;
         }
         try
         {
             _serialPort.Write("T");
             int read = _serialPort.ReadChar();
             if ((char)read == 'T')
                 return true;
         }
         catch (Exception)
         {
             continue;
         }
     }
     return false;
 }
开发者ID:pawlos,项目名称:LEDBoard,代码行数:27,代码来源:MainWindowViewModel.cs

示例4: InternalConditions

        InternalConditions()
        {
            //verify available ports through printout
            string[] ports = SerialPort.GetPortNames();
            foreach (string s in ports)
            {
                System.Console.WriteLine(s);
            }

            weatherPort = new SerialPort("COM7", 9600, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One);
            weatherPort.ReadTimeout = 500;
            weatherPort.Close();
            weatherPort.Open();
            if (weatherPort.IsOpen)
            {
                System.Console.WriteLine("Port open!");
                char[] readStream = new char[255];
                byte commaCount = 0;
                int availableBytes;

                while (true)
                {
                    /* System.Threading.Thread.Sleep(500);//give the board long enough to poll data
                     availableBytes = Math.Min(255,weatherPort.BytesToRead);
                     weatherPort.Read(readStream, 0, availableBytes);
                     for (int i = 0; i < availableBytes; i++)
                     {
                         System.Console.Write(readStream[i]);
                     }
                     System.Console.WriteLine();*/

                    try
                    {
                        System.Console.Write(weatherPort.ReadChar());
                    }
                    catch (TimeoutException)
                    {
                        System.Console.WriteLine("Timed out.");
                    }

                    //System.Console.WriteLine("Data: " + weatherPort.ReadExisting());
                }

            }
            else
            {
                System.Console.WriteLine("Port not open.");
            }
            System.Console.ReadLine();
        }
开发者ID:MorgThom,项目名称:sos-sub,代码行数:50,代码来源:InternalConditions.cs

示例5: Main

        public static void Main(string[] args)
        {
            SerialPort sp = new SerialPort("/dev/ttyACM0", 115200, Parity.None, 8, StopBits.One);
            sp.Open();
            //byte buff[];
            int input;

            for (int index = 0; index < 10; index++)
            {
                //string result = string.Format("{0} Testing", index);
                //sp.Write(result);
                input = sp.ReadChar();
                Console.Write("Recieved: ");
                Console.WriteLine(input);
            }

            sp.Close();
        }
开发者ID:CoroBot,项目名称:CoroBot_HW_API,代码行数:18,代码来源:serial.cs

示例6: SendSerialCommand

        public static String SendSerialCommand(SerialCommand cmd)
        {
            System.Diagnostics.Debug.Assert(!String.IsNullOrEmpty(cmd.Command));
            System.Diagnostics.Debug.Assert(cmd.SerialPort != 0);

            using (SerialPort serialPort = new SerialPort(cmd.SerialPortString, cmd.BaudRate, cmd.Parity, cmd.DataBits, cmd.StopBits))
            {
                try
                {
                    serialPort.ReadTimeout = 1000;

                    serialPort.Open();
                    if (cmd.Hex)
                    {
                        byte[] buffer = HexStringToBytes(cmd.Command);
                        serialPort.Write(buffer, 0, buffer.Length);
                    }
                    else
                        serialPort.WriteLine(cmd.Command + "\r");   // Add a carriage return because most devices want one

                    string actualResponse = String.Empty;
                    foreach (char c in cmd.ExpectedResponse)
                    {
                        char rcvdChar = (char)serialPort.ReadChar();
                        actualResponse += rcvdChar;
                        if (rcvdChar != c)
                        {
                            actualResponse += serialPort.ReadExisting();
                            return actualResponse + "!=" + cmd.ExpectedResponse;
                        }
                    }

                    //System.Threading.Thread.Sleep(cmd.SleepTime);
                    return actualResponse;
                }
                finally
                {
                    serialPort.Close();
                }
            }
        }
开发者ID:talbotmcinnis,项目名称:HomeAutomationWebsite,代码行数:41,代码来源:SerialPortWrapper.cs

示例7: Main

        public static void Main()
        {
            string[] porte = GetPortNames();
            foreach(string porta in porte)
            {
                Console.WriteLine(porta);
            }
            // initialize the serial port
            serial = new SerialPort(porte[0], 115200, Parity.None, 8, StopBits.One);
            // open the serial-port, so we can send & receive data
            serial.Open();
            // add an event-handler for handling incoming data
            //serial.DataReceived += new SerialDataReceivedEventHandler(serial_DataReceived);

            while(true)
            {
                Console.WriteLine("Stringa da spedire, poi Enter");
                string toSend = Console.ReadLine();
                // send the same byte back
                serial.Write(toSend);

                // create a single byte array
                byte[] bytes = new byte[1];

                string str = "";
                // as long as there is data waiting to be read
                while (serial.BytesToRead > 0)
                {
                    // read a single byte
                    //serial.Read(bytes, 0, bytes.Length);
                    char ch = (char) serial.ReadChar();
                    str += ch;
                }
                Console.Write(str);
            }
        }
开发者ID:gamondue,项目名称:GrowBox-5F-16,代码行数:36,代码来源:Program.cs

示例8: StartCounterThread

        private void StartCounterThread()
        {
            SerialPort ComPort = new SerialPort(selectedPortName);
            ComPort.Open();

            using (System.IO.StreamWriter fs = System.IO.File.AppendText(this.fileName.Text))
            {
                fs.WriteLine(DateTime.Now);
                fs.Close();
            }

            while (durationTimer.Enabled)
            {
                int num = (int)ComPort.ReadChar();
                if ((num==48)|| (num==49))
                    { counts++;}
            }
            ComPort.Close();
        }
开发者ID:liona,项目名称:Geiger-Counts-per-Second,代码行数:19,代码来源:Form1.cs

示例9: CommandCompleted

		private static bool CommandCompleted(SerialPort serialPort)
		{
			serialPort.Write("Q\r");
			char result = (char)serialPort.ReadChar();
			Console.WriteLine(result);

			if (result == '.')
			{
				return true;
			}
			else
			{
				return false;
			}
		}
开发者ID:eldb2,项目名称:robotic-tic-tac-toe-lynxmotion,代码行数:15,代码来源:Program.cs

示例10: EnsurePortOpen

        private bool EnsurePortOpen()
        {
            try
            {
                if (m_Port != null && m_Port.IsOpen)
                    return true;

                lock (m_Lock)
                {
                    // Re-check after acquiring lock
                    if (m_Port != null && m_Port.IsOpen)
                        return true;

                    // Get rid of any old closed ports
                    if (m_Port != null)
                        m_Port.Dispose();

                    m_Port = new SerialPort(m_PortName);
                    m_Port.BaudRate = 1200;
                    m_Port.DtrEnable = true;
                    m_Port.Open();

                    // A quick echo test to make sure it exists
                    m_Port.DiscardInBuffer();
                    m_Port.Write(new byte[] { 0x00, 0x04, 0x12 }, 0, 3);
                    int echo = m_Port.ReadChar();
                    if (echo != 0x12)
                        return false; // Something went wrong opening the port - not a WinKey?

                    // Open host mode
                    m_Port.Write(new byte[] { 0x00, 0x02 }, 0, 2);
                    int version = m_Port.ReadChar();
                    if (version < 20 || version > 30)
                        return false; // Weird version code - not a WinKey?

                    m_Port.DataReceived += new SerialDataReceivedEventHandler(DataReceived);

                    // Use the speed pot value
                    m_Port.Write(new byte[] { 0x02, 0x00 }, 0, 2);

                    return true;
                }
            }
            catch
            {
                return false;
            }
        }
开发者ID:rmc47,项目名称:CamLog,代码行数:48,代码来源:WinKey.cs

示例11: StartCounterThread

        private void StartCounterThread()
        {
            SerialPort ComPort = new SerialPort(selectedPortName);
            ComPort.Open();

            using (System.IO.StreamWriter fs = System.IO.File.AppendText(this.fileName.Text))
            {
               // fs.WriteLine("StartTime: {0:O} EndTime{1:O}", num, time);
                fs.AutoFlush = false;
                while (durationTimer.Enabled)
                {
                    char num = (char) ComPort.ReadChar();
                    DateTime time = DateTime.Now;
                    fs.WriteLine("{0} {1:O}", num, time);

                    AddText($"{num} {time:O} \n");
                }
                fs.Close();
            }
            ComPort.Close();
        }
开发者ID:liona,项目名称:Geiger,代码行数:21,代码来源:Form1.cs

示例12: SearchPort

        public static AsyncSerial SearchPort(string write, char search, int baud = 9600)
        {
            foreach (string port in SerialPort.GetPortNames())
            {
                SerialPort temp;
                try
                {
                    temp = new SerialPort(port, baud);
                    try
                    {
                        temp.Open();
                        temp.ReadTimeout = 2500;
                        temp.Write(write);
                        var v = temp.ReadChar();
                        if (v == search)
                        {
                            temp.Close();
                            return new AsyncSerial(port);
                        }
                    }
                    catch
                    { }
                    finally
                    {
                        temp.Close();
                    }
                }
                catch
                { }
            }

            throw new Exception("Port not found.");
        }
开发者ID:tylermenezes,项目名称:SerialServe,代码行数:33,代码来源:AsyncSerial.cs


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