serialPort.ReadByte() 操作超时
设置的操时时间是500毫秒.ReadBufferSize和WriteBufferSize是20480
public void SendCMD(Byte[] _OutputData, SerialPort serialPort)
{
_InputData = new Byte[1];
try
{
if (serialPort.IsOpen)
{
serialPort.DiscardInBuffer();
serialPort.DiscardOutBuffer();//清除缓冲区
}
else
{
serialPort.Open();//打开串口
}
try
{
serialPort.Write(_OutputData, 0, _OutputData.Length);
Array.Clear(_InputData, 0, _InputData.Length);
Array.Resize(ref _InputData, 0);
Byte FirstByte;
try
{
FirstByte = (Byte)serialPort.ReadByte();//这里操时了
}
catch (TimeoutException)
{
return;
}
catch (Exception)
{
throw;
}
Array.Resize(ref _InputData, 1);
_InputData[0] = FirstByte;
int Partition = 40;
while (true)
{
Thread.Sleep(Partition);//字节间超时毫秒
int BufferLength = 0;
int InputLength = _InputData.Length;
BufferLength = serialPort.BytesToRead;//检测接收缓冲区有效字节长度
if (BufferLength == 0)
{
break;
}
Array.Resize(ref _InputData, InputLength + BufferLength);
serialPort.Read(_InputData, InputLength, BufferLength);//读取有效字节
}
}
finally
{
serialPort.DiscardInBuffer();
serialPort.DiscardOutBuffer();//清除缓冲区
serialPort.Close();
}
}
finally
{
WriteLog(_InputData, 1);
}
}