none
ReadFile doesn't return while reading from modem port RRS feed

  • Question

  • I am writing one AT command to modem port but while reading AT command response through Readfile, Readfile doesn't return and blocks. I am using following code for this.

    byte[] buffer = new byte[100];
    uint bytesWritten = 0;
    uint bytesRead = 0;
    string strAT = "AT+CGSN\r\n";
    IntPtr handle = IntPtr.Zero;
    string strCom = "\\\\.\\COM65";

    Dcb config = new Dcb();

                handle = CreateFile(strCom,
                                    GENERIC_READ | GENERIC_WRITE,
                                    FILE_SHARE_READ | FILE_SHARE_WRITE,
                                    IntPtr.Zero,
                                    CREATE_NEW | OPEN_EXISTING,
                                    FILE_ATTRIBUTE_NORMAL,
                                    IntPtr.Zero);


                //config.DCBlength = sizeof(config);

                if ((GetCommState(handle, ref config) == false))
                {
                    System.Console.WriteLine("Get configuration port has a problem.");
                }

                config.BaudRate = 9600;
                config.StopBits = ONESTOPBIT;
                config.Parity = PARITY_NONE;
                config.ByteSize = DATABITS_8;
                //config.fDtrControl = 0;
                //config.fRtsControl = 0;

                if (!SetCommState(handle, ref config))
                {
                    System.Console.WriteLine("Failed to Set Comm State Reason:");
                }

                if (!WriteFile(handle, strAT, (uint)strAT.Length, out bytesWritten, IntPtr.Zero))
                {
                    System.Console.WriteLine("Failed to write !!!");
                }

               if (!ReadFile(handle, buffer, 100, ref bytesRead, IntPtr.Zero))
               {
                   System.Console.WriteLine("Failed to read !!!");
               }

    Mrutyunjaya

    Monday, October 5, 2015 10:57 AM

Answers