0

我尝试使用 C# 语言使用 .net 紧凑框架在 Windows CE 上开发 FTP 程序。但是在过去的两天里,我没有得到答案

我做了以下

  1. 使用RAS Dial连接GPRS 2.导入wininet.dllFTP连接功能

但是InternetConnect 函数总是抛出错误代码12031

好的,我可以分享更多信息吗?请你看看这个

实际上我们正在使用另一家公司的 MDT 设备。他们提供了一个 dll,它可以完成 OpenGPRS 的一些初始任务,根据他们的手册,我们必须延迟大约 30 秒。

之后我们要发出一些AT指令,最后就可以使用RAS dial api进行连接了。

那部分如下

  public static bool TestRAS()
        {
            int hModem;
            COMMTIMEOUTS tm = new COMMTIMEOUTS();
            DCB dcb = new DCB();

           Console.WriteLine("Begin test RAS");
            IO_OpenGprsPower(false);
            System.Threading.Thread.Sleep(1000);
            IO_OpenGprsPower(true);

            //open serial port
            Console.WriteLine("Open gprs port(COM7:) ");
            hModem = CreateFile("COM7:", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
            if (hModem == -1)
            {
                Console.WriteLine("Error, End test RAS ");
                return false;
            }
            //set time out
            tm.ReadIntervalTimeout = 10;
            tm.ReadTotalTimeoutConstant = 10;
            tm.ReadTotalTimeoutMultiplier = 0;
            tm.WriteTotalTimeoutConstant = 5000;
            tm.WriteTotalTimeoutMultiplier = 0;
            if (!SetCommTimeouts(hModem, ref tm))
            {
                CloseHandle(hModem);
                Console.WriteLine("Error..End test RAS");
                return false;
            }
            //set state
            if (!GetCommState(hModem, ref dcb))
            {
                CloseHandle(hModem);
                Console.WriteLine("Error..End test RAS");
                return false;
            }
            dcb.BaudRate = 115200;
            dcb.StopBits = 0;
            dcb.Parity = 0;
            dcb.ByteSize = 8;
            if (!SetCommState(hModem, ref dcb))
            {
                CloseHandle(hModem);
                Console.WriteLine("Error..End test RAS");
                return false;
            }
            Console.WriteLine("OK..");

            //wait gprs module to register to network
            Console.WriteLine("Wait gprs module start ");
            for (int i = 30; i >= 0; i--)
            {
                 Console.WriteLine("Wait.." + i.ToString());
                System.Threading.Thread.Sleep(1000);
            }

            int iRet = 0;
            byte[] objBytes = new byte[1024];
            string strData;
            byte[] objAtCmd;

            //config context
            Console.WriteLine("Config gprs context");
            objAtCmd = System.Text.Encoding.ASCII.GetBytes("AT\r\n");
            WriteFile(hModem, objAtCmd, objAtCmd.Length, ref iRet, 0);
            System.Threading.Thread.Sleep(1000);
            ReadFile(hModem, objBytes, 1024, ref iRet, 0);
            objAtCmd = System.Text.Encoding.ASCII.GetBytes("AT\r\n");
            WriteFile(hModem, objAtCmd, objAtCmd.Length, ref iRet, 0);
            System.Threading.Thread.Sleep(1000);
            ReadFile(hModem, objBytes, 1024, ref iRet, 0);
            strData = System.Text.Encoding.ASCII.GetString(objBytes, 0, iRet);
            if (strData.IndexOf("OK") < 0)
            {
                CloseHandle(hModem);
                Console.WriteLine("AT error.. End test RAS");
                return false;
            }

            objAtCmd = System.Text.Encoding.ASCII.GetBytes("AT+CFUN=1\r\n");
            WriteFile(hModem, objAtCmd, objAtCmd.Length, ref iRet, 0);
            System.Threading.Thread.Sleep(1000);
            ReadFile(hModem, objBytes, 1024, ref iRet, 0);
            strData = System.Text.Encoding.ASCII.GetString(objBytes, 0, iRet);
            if (strData.IndexOf("OK") < 0)
            {
                CloseHandle(hModem);
                Console.WriteLine("AT+CFUN=1 error.. End test RAS");
                return false;
            }

            objAtCmd = System.Text.Encoding.ASCII.GetBytes("AT+COPS?\r\n");
            WriteFile(hModem, objAtCmd, objAtCmd.Length, ref iRet, 0);
            System.Threading.Thread.Sleep(1000);
            ReadFile(hModem, objBytes, 1024, ref iRet, 0);
            strData = System.Text.Encoding.ASCII.GetString(objBytes, 0, iRet);
            if (strData.IndexOf("OK") < 0)
            {
                CloseHandle(hModem);
                Console.WriteLine("AT+COPS? error.. End test RAS");
                return false;
            }

            //you can replace  isp.cingular with your local APN
           // objAtCmd = System.Text.Encoding.ASCII.GetBytes("AT+CGDCONT=1,\"IP\",\"bsnlnet\"\r\n");
            objAtCmd = System.Text.Encoding.ASCII.GetBytes("AT+CGDCONT=1,\"IP\",\"internet\"\r\n");
            Console.WriteLine("AT+CGDCONT=1,\"IP\",\"internet\"\r\n");
            WriteFile(hModem, objAtCmd, objAtCmd.Length, ref iRet, 0);
            System.Threading.Thread.Sleep(1000);
            ReadFile(hModem, objBytes, 1024, ref iRet, 0);
            strData = System.Text.Encoding.ASCII.GetString(objBytes, 0, iRet);
            Console.WriteLine(strData);
            if (strData.IndexOf("OK") < 0)
            {
                CloseHandle(hModem);
                Console.WriteLine("AT+CGDCONT=1 error..End test RAS");
                return false;
            }

            objAtCmd = System.Text.Encoding.ASCII.GetBytes("AT+CGATT=1\r\n");
            Console.WriteLine("AT+CGATT=1");
            WriteFile(hModem, objAtCmd, objAtCmd.Length, ref iRet, 0);
            System.Threading.Thread.Sleep(30000);
            ReadFile(hModem, objBytes, 1024, ref iRet, 0);
            strData = System.Text.Encoding.ASCII.GetString(objBytes, 0, iRet);
            Console.WriteLine(strData);
            if (strData.IndexOf("OK") < 0)
            {
                CloseHandle(hModem);
                Console.WriteLine("AT+CGATT=1 error.. End test RAS");
                return false;
            }
            CloseHandle(hModem);

            //begin set dial parameters
            Console.WriteLine("Begin dial.Please wait..");
            int iSize = 2 + RAS_MaxEntryName + 1 + RAS_MaxPhoneNumber + 1 +
                RAS_MaxCallbackNumber + 1 + UNLEN + 1 + PWLEN + 1 + DNLEN + 1;
            char[] paras = new char[iSize];
            for (int i = 0; i < iSize; i++)
                paras[i] = (char)0;
            iSize = iSize * 2;
            //RASDIALPARAMS.dwSize
            paras[0] = (char)(iSize & 0xFFFF);
            paras[1] = (char)((iSize >> 16) & 0xFFFF);

            //RASDIALPARAMS.szEntryName

            paras[2] = 'G';

            paras[3] = 'p';

            paras[4] = 'r';

            paras[5] = 's';

            paras[6] = 'M';

            paras[7] = 'o';

            paras[8] = 'd';

            paras[9] = 'e';

            paras[10] = 'm';



            string username = "";

            string password = "";

            char[] charUserName;

            charUserName = username.ToCharArray();

            char[] charPassword;

            charPassword = password.ToCharArray();

            char[] charPhone;

            charPhone = "*99***1#".ToCharArray();



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

                paras[22 + i] = charPhone[i];

            }



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

                paras[200 + i] = charUserName[i];

            }



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

                paras[457 + i] = charPassword[i];

            }

            //Start to dial
            IO_DisableRASDialUserNamePasswordVerifyDialog();
            int hRasConn = 0;
            if (RasDial(0, null, paras, 0, 0, ref hRasConn) != 0)
            {
                Console.WriteLine("Dial error");
                if (hRasConn != 0)
                    RasHangUp(hRasConn);
               Console.WriteLine("End test RAS");
                return false;
            }
            Console.WriteLine("Dial ok");

            //get ip address
            Console.WriteLine("Get IP addr ");
            iSize = 2 + 2 + RAS_MaxIpAddress + 1;
            char[] rasIp = new char[iSize];
            for (int i = 0; i < iSize; i++)
                paras[i] = (char)0;
            iSize = iSize * 2;
            //RASPPPIP.dwSize
            rasIp[0] = (char)(iSize & 0xFFFF);
            rasIp[1] = (char)((iSize >> 16) & 0xFFFF);

            iRet = iSize;
            if (RasGetProjectionInfo(hRasConn, 0x8021, rasIp, ref iRet) != 0)
            {
                Console.WriteLine("Error");
                RasHangUp(hRasConn);
                Console.WriteLine("End test RAS");
                return false;
            }
            for (int i = 0; i < RAS_MaxIpAddress + 1; i++)
            {
                if (rasIp[2 + 2 + i] == (char)0)
                    break;
                 Console.WriteLine(rasIp[2 + 2 + i].ToString());
            }


            Console.WriteLine("End RAS Test");
            //try
            //{
            //    Process.Start("IExplore.exe", "@http://www.google.co.in");
            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine(ex.Message);
            //}
           // RasHangUp(hRasConn);
            return true;
        }

对于 FTP 部分,我使用 wininet.dll 函数开发了一个库

OPen FTP的功能如下:

 public void Open()
        {
            if (String.IsNullOrEmpty(_host)) throw new ArgumentNullException("Host");

            _hInternet = WININET.InternetOpen(
                "FTPLibTest",
                WININET.INTERNET_OPEN_TYPE_DIRECT,
                null,
                null,
                0);

            if (_hInternet == IntPtr.Zero)
            {
                Error();
            }

            uint uintFlags = 0;
            WININET.InternetGetConnectedState(ref uintFlags, 0);
            if (uintFlags > 0)
            {
                Console.WriteLine("Internet Connected");
            }
        }

FTP登录功能:

public void Login(string username, string password)
        {
            if (username == null) throw new ArgumentNullException("username");
            if (password == null) throw new ArgumentNullException("password");
            {

            }
            _hConnect = WININET.InternetConnect(_hInternet,
                _host,
                _port,
                username,
                password,
                WININET.INTERNET_SERVICE_FTP,
                134217728,
                IntPtr.Zero);

            if (_hConnect == IntPtr.Zero)
            {
                Error();
            }


        }

从这个函数我得到了 12031 的错误

WININET 是我放置所有 dllimport 的类

4

0 回答 0