• <bdo id='MzYm0'></bdo><ul id='MzYm0'></ul>
    <i id='MzYm0'><tr id='MzYm0'><dt id='MzYm0'><q id='MzYm0'><span id='MzYm0'><b id='MzYm0'><form id='MzYm0'><ins id='MzYm0'></ins><ul id='MzYm0'></ul><sub id='MzYm0'></sub></form><legend id='MzYm0'></legend><bdo id='MzYm0'><pre id='MzYm0'><center id='MzYm0'></center></pre></bdo></b><th id='MzYm0'></th></span></q></dt></tr></i><div id='MzYm0'><tfoot id='MzYm0'></tfoot><dl id='MzYm0'><fieldset id='MzYm0'></fieldset></dl></div>

  • <legend id='MzYm0'><style id='MzYm0'><dir id='MzYm0'><q id='MzYm0'></q></dir></style></legend>

      <small id='MzYm0'></small><noframes id='MzYm0'>

    1. <tfoot id='MzYm0'></tfoot>

        从 C# 中的串口读取字符

        时间:2023-11-10
        • <bdo id='MzjyH'></bdo><ul id='MzjyH'></ul>

            • <small id='MzjyH'></small><noframes id='MzjyH'>

              <legend id='MzjyH'><style id='MzjyH'><dir id='MzjyH'><q id='MzjyH'></q></dir></style></legend>
                1. <i id='MzjyH'><tr id='MzjyH'><dt id='MzjyH'><q id='MzjyH'><span id='MzjyH'><b id='MzjyH'><form id='MzjyH'><ins id='MzjyH'></ins><ul id='MzjyH'></ul><sub id='MzjyH'></sub></form><legend id='MzjyH'></legend><bdo id='MzjyH'><pre id='MzjyH'><center id='MzjyH'></center></pre></bdo></b><th id='MzjyH'></th></span></q></dt></tr></i><div id='MzjyH'><tfoot id='MzjyH'></tfoot><dl id='MzjyH'><fieldset id='MzjyH'></fieldset></dl></div>

                  <tfoot id='MzjyH'></tfoot>
                    <tbody id='MzjyH'></tbody>

                2. 本文介绍了从 C# 中的串口读取字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  您好,我正在使用 Read() 方法从串口读取 10 个字符,例如 0123456789.实际上字符是由PIC单片机发送的.

                  Hello I am using Read() method to read 10 characters say 0123456789 from serial port. Actually the characters are sent by a PIC Micro-controller.

                  这是我的代码:

                  serialPort1.PortName = "com4";
                  serialPort1.BaudRate = 9600;
                  serialPort1.Open();
                  char[] result = new char[10];
                  serialPort1.Read(result, 0, result.Length);
                  string s = new string(result);
                  MessageBox.Show(s);
                  serialPort1.Close();
                  

                  当我运行代码时,会出现一个消息框并且只显示第一个字符.消息框中只显示0".

                  When I run the code, a message box shows up and displays only the first character. "0" alone is displayed in the message box.

                  我哪里出错了??

                  推荐答案

                  你做错的是没有注意Read()的返回值.它告诉你读取了多少字节.

                  What you are doing wrong is not paying attention to the return value of Read(). Which tells you how many bytes were read.

                  串行端口是非常慢的设备,在典型的波特率设置为 9600 时,传输一个字节需要一毫秒.对于现代处理器来说,这是一个巨大的时间,它可以轻松地在一毫秒内执行数百万条指令.Read() 方法会在 一些 字节可用时立即返回,如果您人为地使程序变慢,那么您只能获得全部 10 个字节,以便驱动程序有足够的时间来接收所有这些字节.

                  Serial ports are very slow devices, at a typical baudrate setting of 9600 it takes a millisecond to get one byte transferred. That's an enormous amount of time for a modern processor, it can easily execute several million instructions in a millisecond. The Read() method returns as soon as some bytes are available, you only get all 10 of them if you make your program artificially slow so the driver gets enough time to receive all of them.

                  一个简单的解决方法是继续调用 Read() 直到你把它们全部搞定:

                  A simple fix is to keep calling Read() until you got them all:

                  char[] result = new char[10];
                  for (int len = 0; len < result.Length; ) {
                     len += serialPort1.Read(result, len, result.Length - len);
                  }
                  

                  另一种常见的解决方案是发送一个唯一字符来指示数据的结束.换行 (' ') 是一个很好的选择.现在它变得简单多了:

                  Another common solution is to send a unique character to indicate the end of the data. A line feed (' ') is a very good choice for that. Now it becomes much simpler:

                  string result = serialPort.ReadLine();
                  

                  现在还支持任意响应长度.只需确保数据不包含换行符即可.

                  Which now also supports arbitrary response lengths. Just make sure that the data doesn't also contain a line feed.

                  这篇关于从 C# 中的串口读取字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:用于多个平台的 Mono 中的串行端口 (rs232) 下一篇:关闭串行端口时,是什么导致我的 UI 冻结?

                  相关文章

                3. <tfoot id='3XP32'></tfoot>

                  1. <small id='3XP32'></small><noframes id='3XP32'>

                  2. <legend id='3XP32'><style id='3XP32'><dir id='3XP32'><q id='3XP32'></q></dir></style></legend>

                      <i id='3XP32'><tr id='3XP32'><dt id='3XP32'><q id='3XP32'><span id='3XP32'><b id='3XP32'><form id='3XP32'><ins id='3XP32'></ins><ul id='3XP32'></ul><sub id='3XP32'></sub></form><legend id='3XP32'></legend><bdo id='3XP32'><pre id='3XP32'><center id='3XP32'></center></pre></bdo></b><th id='3XP32'></th></span></q></dt></tr></i><div id='3XP32'><tfoot id='3XP32'></tfoot><dl id='3XP32'><fieldset id='3XP32'></fieldset></dl></div>
                        <bdo id='3XP32'></bdo><ul id='3XP32'></ul>