當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。