I'm sorry and my english is poor. If you can't understand what i mean and tell me, please! Because of I need your help.
When I communication to board through RS232 with API, but my program will be stopped in ReadFile() after running for a long time. There is a progarm below and happen to boldface. I can't find a answer and can't solve it. I have setted up timeout but it seems like don't active. Even through time is over but my program don't go next step. Something wrong with my program. Please give me a advice. Thanks.
(some classes I simplied it because it's all API's function)
const Int32 INVALID_HANDLE_VALUE = -1;
System.IntPtr handle = (IntPtr)INVALID_HANDLE_VALUE;
DCB dcb = new DCB();
CommTimeouts timeouts = new CommTimeouts();
private void Run()
{
readthread = true;
byte[] bufferr = new byte[230];
int reclength = SendByte[5] * 2 + 5;
System.Text.ASCIIEncoding Encoding = new System.Text.ASCIIEncoding();
int written = 0;
int readed = 0;
int secondnow;
int secondold;
byte[] SendByte = new byte[8];
do
{
if (handle == (IntPtr)INVALID_HANDLE_VALUE)
{
handle = CreateFile("COM7", GENERIC_READ | GENERIC_WRITE, 0, 0,
OPEN_EXISTING, 0, 0);
Thread.Sleep(1);
GetCommState(handle, dcb);
dcb.BaudRate = 57600;
dcb.Parity = 0;
dcb.ByteSize = 8;
dcb.StopBits = 1;
SetCommState(handle, dcb);
timeouts.ReadIntervalTimeout = 0;
timeouts.ReadTotalTimeoutMultiplier = 0;
timeouts.ReadTotalTimeoutConstant = 500;
timeouts.WriteTotalTimeoutMultiplier = 0;
timeouts.WriteTotalTimeoutConstant = 500;
IntPtr a = API_RS232.SetCommTimeouts(handle,ref timeouts);
Thread.Sleep(1);
}
if (handle != (IntPtr)INVALID_HANDLE_VALUE)
{
WriteFile(handle,SendByte,(uint)SendByte.Length,ref written, 0);
Thread.Sleep(50);
}
if (handle != (IntPtr)INVALID_HANDLE_VALUE)
{
ReadFile(handle, bufferr,(uint)reclength,ref readed,
0);
}
Thread.Sleep(50);
}