• <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# 中的串口读取字符

        Read characters from serial port in c#(从 C# 中的串口读取字符)
        • <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# 中的串口读取字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
                  Parameter count mismatch with Invoke?(参数计数与调用不匹配?)
                  How to store delegates in a List(如何将代表存储在列表中)
                  How delegates work (in the background)?(代表如何工作(在后台)?)
                  C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)
                  Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)
                3. <tfoot id='3XP32'></tfoot>

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

                          <tbody id='3XP32'></tbody>
                      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>