System.IO.Ports.SerialPort.ReadChar C# (CSharp) Method

ReadChar() public method

public ReadChar ( ) : int
return int
		public int ReadChar ()
		{
			CheckOpen ();
			
			byte [] buffer = new byte [16];
			int i = 0;

			do {
				int b = read_byte ();
				if (b == -1)
					return -1;
				buffer [i++] = (byte) b;
				char [] c = encoding.GetChars (buffer, 0, 1);
				if (c.Length > 0)
					return (int) c [0];
			} while (i < buffer.Length);

			return -1;
		}

Usage Example

示例#1
1
        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.");
        }
All Usage Examples Of System.IO.Ports.SerialPort::ReadChar