简体   繁体   English

从C#中的串行端口接收时处理大字节

[英]Handle big bytes when receiving from serial port in c#

I am new in the serial port. 我是串口新手。 Currently, my project is to extract data from the machine. 当前,我的项目是从机器中提取数据。 I'm getting data via an event onDataReceive and the machine is sending bytes. 我通过onDataReceive事件获取数据,并且机器正在发送字节。

My problem is that the first wave of bytes seemed to be converted correctly to string but in the second batch of bytes, I got garbage data. 我的问题是第一批字节似乎正确转换为字符串,但是在第二批字节中,我得到了垃圾数据。

Screen Shot of the output(this is the output given by the multi-currency reader machine: 输出的屏幕截图(这是多币种阅读器机器给出的输出: 在此处输入图片说明

The garbage data is I think the Serial Nos. 我认为垃圾数据是序列号。

Here is my code in onDataReceive method: 这是我在onDataReceive方法中的代码:

private void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        while (serialPort.BytesToRead > 0)
        {
            // Initialize a buffer to hold the received data
            byte[] buffer = new byte[serialPort.ReadBufferSize];
            //// There is no accurate method for checking how many bytes are read
            //// unless you check the return from the Read method
            int bytesRead = serialPort.Read(buffer, 0, buffer.Length);
            String asd = System.Text.ASCIIEncoding.ASCII.GetString(buffer, 0, bytesRead);

            //// For the example assume the data we are received is ASCII data.
            tString += Encoding.ASCII.GetString(buffer, 0, bytesRead);
            temp += System.Text.Encoding.Unicode.GetString(buffer, 0, bytesRead);
            temp2 += System.Text.Encoding.UTF32.GetString(buffer, 0, bytesRead);
            System.IO.File.WriteAllText(@"C:\OutputTextFiles\WriteLines.txt", tString);
            System.IO.File.WriteAllText(@"C:\OutputTextFiles\WriteLines2.txt", temp);
            System.IO.File.WriteAllText(@"C:\OutputTextFiles\WriteLines3.txt", temp2);
        }
    }

I'm trying to put the output with a txt file. 我正在尝试将输出与txt文件放在一起。

I hope someone could help me in my problem. 我希望有人可以帮助我解决我的问题。 Any tips and suggestions in data handling especially bytes? 在数据处理方面有什么技巧和建议,尤其是字节?

Without knowing the size of serialPort.ReadBufferSize I can only suspect that your buffer is breaking the encoding bytes of your string. 在不知道serialPort.ReadBufferSize的大小的情况下,我只能怀疑您的缓冲区正在破坏字符串的编码字节。 A character can be made of one or more bytes. 一个字符可以由一个或多个字节组成。

The trick is to read all of the bytes before decoding the string. 诀窍是在解码字符串之前先读取所有字节。

Try this example program: 试试这个示例程序:

var encoding = System.Text.Encoding.Unicode;
var message = "I am new in serial port. Currently my project is to extract data from machine.";
using (var ms = new MemoryStream(encoding.GetBytes(message)))
{
    var bytes = new List<byte>();
    var buffer = new byte[23];
    var bytesRead = ms.Read(buffer, 0, buffer.Length);
    while (bytesRead > 0)
    {
        Console.WriteLine(encoding.GetString(buffer, 0, bytesRead));
        bytes.AddRange(buffer.Take(bytesRead));
        bytesRead = ms.Read(buffer, 0, buffer.Length);
    }
    Console.WriteLine(encoding.GetString(bytes.ToArray(), 0, bytes.Count));
}

It will output the following: 它将输出以下内容:

I am new in�
猀攀爀椀愀氀 瀀漀爀琀�
. Currently�
洀礀 瀀爀漀樀攀挀琀 �
is to extra�
琀 搀愀琀愀 昀爀漀洀�
 machine.
I am new in serial port. Currently my project is to extract data from machine.

The final line is correct because it uses all of the bytes to decode. 最后一行是正确的,因为它使用所有字节进行解码。 The previous lines have errors because I've used a buffer size of 23 which breaks the string encoding. 前几行有错误,因为我使用的缓冲区大小为23,这会破坏字符串编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM