Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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();  //class of 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);
    }
Posted
Updated 21-Jun-10 11:35am
v4
Comments
Kythen 21-Jun-10 17:37pm    
Edited for code readability

1 solution

Why do you (almost) never check API return values?
:)
 
Share this answer
 
Comments
smallgreen29 21-Jun-10 4:24am    
hello! I have cheked all API return values. I just want to save the words of this article and I omited it.
It correct return values of the Creatfile() and WriteFile() but ReadFile() I can't. Because the program stopped and I don't know where is it. Can you help me again,please!! Thanks
CPallini 21-Jun-10 4:44am    
SetTimeouts is an API call too. Please provide the real code (and, please again, use the code block button).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900