问题描述
我的项目回复了有关使用serialPort1.Read的错误:
my project give back an error about using serialPort1.Read:
byte[] buff={0};
serialPort1.Read(buff, 10, 10);
在button_click事件中,当我按下按钮时,返回错误并显示偏移和长度超出界限对于数组或计数大于从索引到源集合末尾的元素数。
是什么意思?
it's in "button_click" event, when i pressed on button, give back an error and display "Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection."
what does it mean?
推荐答案
byte[] buff= new byte[20];
serialPort1.Read(buff, 10, 10);
但相反,请不要使用幻数 - 使用常量,或检查串口长度,以确保至少有你需要的字节数。然后使用该大小来分配缓冲区,记住添加输出偏移所需的任何内容。
But instead, please don't use "magic numbers" - use constants, or check the serial port length to ensure there are at least as many bytes as you need first. Then use that size to allocate your buffer, remembering to add on whatever you need for the output offset.
这篇关于有关serialport.read(byte [] buffer,Int32,Int32)的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!