简体   繁体   English

奇怪的行为形式C#串行端口

[英]Strange behavior form C# serial port

I'm getting strange behavior from simple program, which I'm using to learn how to use the serial port. 我从简单的程序中得到了奇怪的行为,我正在用它来学习如何使用串行端口。 The form have just one serial port control and one TextBox. 该窗体只有一个串行端口控件和一个TextBox。 Because it's just a test program I have disabled the thread call check. 因为它只是一个测试程序,所以我禁用了线程调用检查。 I forgot to mention,that I'm using micro-controller to send 1000 bytes of data (read EEPROM). 我忘了提一下,我正在使用微控制器发送1000字节的数据(读取EEPROM)。 The strange thing is that, when I read the data and just append it directly to the text box in the DataReceived event, everything is fine, but when I first pass the values to int[] array,and then use a loop to convert them to strings in HEX format and append them to the TextBox, there are some zeros, between the values. 奇怪的是,当我读取数据并将其直接附加到DataReceived事件中的文本框中时,一切都很好,但是当我首先将值传递给int []数组,然后使用循环将其转换时转换为十六进制格式的字符串,并将其附加到TextBox,值之间有一些零。

Some code with results. 一些带有结果的代码。

Case 1: read data and directly append to TextBox 情况1:读取数据并直接附加到TextBox

private void sp1_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        while (sp1.BytesToRead > 0)
        {
            textBox1.AppendText(sp1.ReadByte().ToString("X")+ " ");
        }

    }

And the result is (well,part of it,as i said there are 1000 bytes to receive...) 结果是(嗯,部分,因为我说有1000个字节要接收...)

0 1 2 3 4 5 6 7 8 9 A B C D E F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F 60 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F 70 71 72 73 74 75 76 77 78 79 7A 7B 7C 7D 7E 7F 80 81 82 83 84 85 86 87 88 89 8A 8B 8C 8D 8E 8F 90 91 92 93 94 95 96 97 98 99 9A 9B 9C..... and so on

Case 2: first store the values to int array,and than convert them to string, and append to TextBox 情况2:首先将值存储到int数组,然后将其转换为字符串,然后追加到TextBox

 private void sp1_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        int[] buffer = new int[1000];
        int i = 0;
        while (sp1.BytesToRead > 0)
        {
            //textBox1.AppendText(sp1.ReadByte().ToString("X")+ " ");
            buffer[i] = sp1.ReadByte();
            i++;
        }
        int j = 0;
        while (j < 1000)
        {
            textBox1.AppendText(buffer[j].ToString("X"));
            j++;
        }

I get a lot of 0's at random places, and it reads 4-5 times more data, than the 1000 in the loop 我在随机位置得到很多0,它读取的数据是循环中1000的4-5倍

0123456789ABCDEF101112131415161718191A1B1C1D1E1F00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E7F808182838485868788898A8B8C8D8E8F909192939495969798999A9B9C9D9E9FA0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBFC0C1C2C3C4C5C6C7C8C9CACBCCCDCECFD0D1D2D3D4D5D6D7D8D9DADBDCDDDEDFE0E1E2E3E4E5E6E7E8E9EAEBECEDEEEFF0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF0123456789ABCDEF101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E7F808182838485868788898A8B8C8D8E8F909192939495969798999A9B9C9D9E9FA0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBFC0C1C2C3C4C5C6C7C8C9CACBCCCDCECFD0D1D2D3D4D5D6D7D8D9DADBDCDDDEDFE0E1E2E3E4E5E6E7E8E9EAEBECEDEEEFF0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF0123456789ABCDEF101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E7F808182838485868788898A8B8C8D8E8F909192939495969798999A9B9C9D9E9FA0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBFC0C1C2C3C4C5C6C7C8C9CACBCCCDCECFD0D1D2D3D4D5D6D7D8D9DADBDCDDDEDFE0E1E2E3E4E5E6E7E8E9EAEBECEDEEEFF0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF0123456789ABCDEF101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E7F808182838485868788898A8B8C8D8E8F909192939495969798999A9B9C9D9E9FA0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBFC0C1C2C3C4C5C6C7C8C9CACBCCCDCECFD0D1D2D3D4D5D6D7D8D9DADBDCDDDEDFE0E1E2E3E4E5E6E7000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

What might be the reason for this strange behavior? 这种奇怪行为的原因可能是什么? Thanks in advance 提前致谢

sp1_DataReceived is typically not called only once. 通常不只一次调用sp1_DataReceived Typically your computer handles received data faster than received. 通常,您的计算机处理收到的数据的速度比收到数据的速度快。 This means at some point the loop 这意味着在某些时候循环

while (sp1.BytesToRead > 0)

is left before all 1000 bytes are received. 在接收所有1000字节之前保留。 Just a short time later sp1_DataReceived is already called again because more of the 1000 bytes are now available. 不久之后就再次调用了sp1_DataReceived ,因为现在可以使用1000个字节中的更多字节。 Since your first implementation only appends the bytes it doesen't matter. 由于您的第一个实现仅附加字节,因此没有关系。 But your second implementation differs because you are always appending 1000 characters to your text. 但是第二种实现方式有所不同,因为您总是在文本后附加1000个字符。 This could lead to a result of a multiple of 1000 characters with zeros appended. 这可能导致结果是1000个字符的倍数并附加零。

To fix your problem you need to combine the bytes of multiple events. 要解决您的问题,您需要组合多个事件的字节。 One solution could be to use a list like 一种解决方案是使用类似

private List<byte> buffer = new List<byte>();
private void sp1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    while (sp1.BytesToRead > 0)
    {
        buffer.Add(sp1.ReadByte());
    }

    //Print if all bytes are available
    if (buffer.Count >= 1000)
    {
        //Join the bytes to a string using LINQ
        textBox1.Text = String.Join("", buffer.Select(b => b.ToString("X")));
        buffer.Clear();
    }
}

or an array like 或类似的数组

private byte[] buffer = new byte[1000];
private int bufferIndex = 0;
private void sp1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    while (sp1.BytesToRead > 0 && bufferIndex  < 1000)
    {
        buffer[bufferIndex ] = sp1.ReadByte();
        bufferIndex ++;
    }

    //Print if all bytes are available
    if (bufferIndex  >= 1000)
    {
        //Join the bytes to a string using LINQ
        textBox1.Text = String.Join("", buffer.Select(b => b.ToString("X")));
        bufferIndex = 0;
    }
}

Note that this are only some ideas and example implementations. 请注意,这只是一些想法和示例实现。 Since I do not know if you are also receiving other messages at the port it is not possible to give a perfect suitable solution to solve your problem. 由于我不知道您是否还在端口上接收到其他消息,因此无法给出完美的合适解决方案来解决您的问题。

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

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