• <bdo id='UPy8x'></bdo><ul id='UPy8x'></ul>

    1. <small id='UPy8x'></small><noframes id='UPy8x'>

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

        从串口读取时如何应用编码

        时间:2023-11-10

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

              1. <small id='Vd5GF'></small><noframes id='Vd5GF'>

                1. <legend id='Vd5GF'><style id='Vd5GF'><dir id='Vd5GF'><q id='Vd5GF'></q></dir></style></legend>
                  本文介绍了从串口读取时如何应用编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在从串行端口读取数据.我读了这篇文章:http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/a709d698-5099-4e37-9e10-f66ff22cdd1e

                  I'm reading data from a serial port. I read this posting: http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/a709d698-5099-4e37-9e10-f66ff22cdd1e

                  他正在写我遇到的许多问题,但在他的文章中他提到使用:System.Text.Encoding.GetEncoding("Windows-1252").我遇到的问题是何时以及如何应用它.在我看来,有三个潜在的地方.定义串口对象时:

                  He is writing about many of the issues I have encounter, but in his writing he refers to using: System.Text.Encoding.GetEncoding("Windows-1252"). The problem I'm having is when and how to apply this. There are three potitional spots in my opinion. When the serial port object is define:

                  private SerialPort comport = new SerialPort();
                  

                  事件处理程序:

                  comport.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
                  

                  或者在读取数据时:

                  string data = comport.ReadExisting();
                  

                  无论我在哪里添加它.我似乎得到了错误.如何使用编码?

                  No matter where I add it. I seem to get errors. How would one use Encoding?

                  推荐答案

                  不使用 ReadExisting,而是使用端口的 Read 方法获取字节,然后将其转换为具有所需编码的字符串,如下所示:

                  Instead of using ReadExisting, use the port's Read method to get the bytes and then convert them to a string with the desired encoding, like this:

                  void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
                  {
                      SerialPort port = (SerialPort)sender;
                      byte[] data = new byte[port.BytesToRead];
                      port.Read(data, 0, data.Length);
                      string s = Encoding.GetEncoding("Windows-1252").GetString(data);
                  }
                  

                  更新:根据 Joo 的回答,这是一个更简单、仍然对 C#-2.0 友好的版本.在你实例化你的 SerialPort 对象后,像这样设置它的 Encoding 属性:

                  Update: Here's a simpler, still-C#-2.0-friendly version based on Joo's answer. After you instantiate your SerialPort object, set its Encoding property like so:

                  port.Encoding = Encoding.GetEncoding("Windows-1252");
                  

                  那么你的 DataReceived 方法就变成了这样:

                  Then your DataReceived method becomes just this:

                  void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
                  {
                      SerialPort port = (SerialPort)sender;
                      string s = port.ReadExisting();
                  }
                  

                  这篇关于从串口读取时如何应用编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 C# 中以编程方式更改串行端口配置 下一篇:C#串口通信中的await事件和超时

                  相关文章

                    <bdo id='MwIfW'></bdo><ul id='MwIfW'></ul>

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

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

                    <legend id='MwIfW'><style id='MwIfW'><dir id='MwIfW'><q id='MwIfW'></q></dir></style></legend>

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