SerialPort serial port operation (ReadByte) read cache data "operation has timed

The following is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Threading;

namespace SerialPortTest
{
public class DriverTest
{
SerialPort sp = new SerialPort();

public string strStatus;

//Open the serial port data close serial

#region
/// <summary>
/// Open the serial port
/// </summary>
/// <param name="Portname">Serial port name</param>
/// <param name="baudRate">The serial baud rate</param>
/// <returns>Open the success</returns>
public bool Open(string Portname, int baudRate)
{
if (!sp.IsOpen)
{
//The name COM1
sp.PortName = Portname;
//The baud rate of 9600 or 115200
sp.BaudRate = baudRate;
//8 data bits
sp.DataBits = 8;
//Parity none
sp.Parity = Parity.None;
//1 stop bit
sp.StopBits = StopBits.One;

sp.ReadTimeout = 500;
sp.WriteTimeout = 500;

try
{
//Open the serial port
sp.Open();

}
catch (Exception ex)
{

strStatus = "Open error: " + ex.Message;

return false;
}

strStatus = "Open successfully";

return true;

}

else
{

strStatus = "Open error: Had Opened.";

return false;
}

}
/// <summary>
/// Close the serial port
/// </summary>
/// <returns>Close the success</returns>
public bool Close()
{
if (sp.IsOpen)
{
//Close the serial port
try
{
sp.Close();
}
catch (Exception ex)
{

strStatus = "Close Error: " + ex.Message;

return false;
}

strStatus = "Close Sucessfully.";

return true;
}

else
{

strStatus = "Error: port not open.";

return false;
}
}
#endregion

//Create command
/// <summary>
/// Create command
/// </summary>
/// <param name="address">Device address</param>
/// <param name="command">To establish the command</param>
private void BuildCommadN1012(byte address, ref byte[] command)
{
//Parity bit
byte[] CRC = new byte[2];
//Device address
command[0] = address;
//The type of operation to read
command[1] = 0x03;
//DI register address 0x02 0x00
command[2] = 0x02;
command[3] = 0x00;
//The number of DI 0x00 0x10
command[4] = 0x00;
command[5] = 0x10;

GetCRC(command, ref CRC);
command[command.Length - 2] = CRC[0];
command[command.Length - 1] = CRC[1];

}

//The establishment of CRC check
/// <summary>
/// The establishment of CRC check
/// </summary>
/// <param name="command">Command</param>
/// <param name="CRC">Check value</param>
private void GetCRC(byte[] command, ref byte[] CRC)
{
//Initialization
ushort CRCFULL = 0xFFFF;
byte CRCH = 0xFF;
byte CRCL = 0xFF;
char CRCLSB;

for (int i = 0; i <command.Length - 2; i++)
{
CRCFULL = (ushort)(CRCFULL ^ command[i]);

for (int j = 0; j <8; j++)
{
CRCLSB = (char)(CRCFULL & 0x0001);
CRCFULL = (ushort)((CRCFULL >> 1) & 0x7FFF);

if (CRCLSB == 1)
{
CRCFULL = (ushort)(CRCFULL ^ 0xA001);
}
}
}

CRC[1] = CRCH = (byte)((CRCFULL >> 8) & 0xFF);
CRC[0] = CRCL = (byte)(CRCFULL & 0xFF);
}
//Return the data
/// <summary>
/// Get back to the data
/// </summary>
/// <param name="response">Return data</param>
private void GetResponse(ref byte[] response)
{
if (sp.IsOpen)
{
for (int i = 0; i <response.Length; i++)
{

response[i] = (byte)sp.ReadByte();

}



}

else
{
strStatus = "port not open";
}
}
//Check the return data
/// <summary>
/// Check the return data
/// </summary>
/// <param name="response">Returns a byte</param>
/// <returns>Success</returns>
private bool CheckResponse(byte[] response)
{

byte[] CRC = new byte[2];
//Gets a CRC check
GetCRC(response, ref CRC);

if (CRC[0] == response[response.Length - 2] && CRC[1] == response[response.Length - 1])

return true;

else

return false;

}


#Region send and receive data M16

private bool SendGetDataN1012(byte address, ref byte[] response)
{
//Check whether the serial open

if (sp.IsOpen)
{
//To empty the cache
sp.DiscardInBuffer();

sp.DiscardOutBuffer();
//The command byte group
byte[] command = new byte[8];

BuildCommadN1012(address, ref command);

try
{

sp.Write(command, 0, command.Length);
GetResponse(ref response);

if (!CheckResponse(response))
{
return false;
}
}

catch (Exception ex)
{

strStatus = "N1012 Write Error: " + ex.Message;

return false;
}

return true;
}
else
{
strStatus = "port not open";

return false;
}
}

#endregion

private byte[] Convert2ByteTo1Byte(byte[] source)
{
byte[] buffer = new byte[16];
for (int i = 0; i <(source.Length / 2); i++)
{
buffer[i] = source[2 * i];
buffer[i] <<= 8;
buffer[i] += source[2 * i + 1];
}

return buffer;

}

private byte[] ConverN1012SourceData(byte[] source)
{
if (source[1] == 0x03)
{

byte[] buffer = new byte[32];

Array.Copy(source, 3, buffer, 0, 32);

return Convert2ByteTo1Byte(buffer);
}

else
{
return new byte[] { 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15 };
}

}

public byte[,] GetStatusByAddressN1012(int[] iaddress)
{

byte[] response = new byte[37];

byte[] values = new byte[16];

byte[,] buffer = new byte[iaddress.Length, 16];

for (int i = 0; i <iaddress.Length; i++)
{

SendGetDataN1012((byte)iaddress[i], ref response);

values = ConverN1012SourceData(response);

for (int j = 0; j <16; j++)
{
buffer[i, j] = values[j];
}

Thread.Sleep(20);

}

return buffer;
}


}
}
The red part is "the operation has timed out" abnormal part

Started by Augustine at November 17, 2016 - 8:30 AM

Overtime, no data transmission, long waiting time

Posted by Fitzgerald at November 24, 2016 - 9:27 AM

It is said that returns no data terminal equipment? Waiting for a long time does not appear, I issued a command data lamp equipment immediately flashed, should the network connection is good, it doesn't return data is generally what causes.?

Posted by Augustine at December 04, 2016 - 10:06 AM

    public string ZZID
    {
        get { return getstring(Request.QueryString["zzid"]); }
    }

Posted by Deborah at December 18, 2016 - 10:56 AM

Tangolivesky is what? Get some soy sauce?

Posted by Augustine at January 02, 2017 - 11:20 AM

No one stepped on? Beg God to answer ah, hurry

Posted by Augustine at January 08, 2017 - 12:19 PM

Landlord does not know the problem has no solution, I now also encountered such problems, I do not know why

Posted by Everley at January 14, 2017 - 12:15 PM