[英]Dialing RAS from WinCE device
我正在做一個更大的項目,該部分旨在為Motorola MC9596掃描儀創建軟件,但我始終無法通過FTP服務器通過GPRS移動連接進行數據傳輸。
為此,我正在使用OpenNETCF。
問題是我無法撥打與我創建的條目的連接。 這意味着當我在設備上手動配置連接和test2時(通過編程方式使用以下方式創建),設備的電話簿中有2個條目,即test1-由設備生成。
private const string CONNAME = "test2";
private const string PHONENR = "~GPRS!xxx.xxx-xxxxxxxx.eu";
private const string USER = "xx";
private const string PWD = "xx";
private const string DEVICE_TYPE = "modem";
private const string DEVICE_NAME = "Cellular Line";
private void createConnectionEntry()
{
RasEntry rasEnt = new RasEntry()
{
Name = CONNAME,
CountryCode = 0,
AreaCode = "",
PhoneNumber = PHONENR,
DeviceName = DEVICE_NAME,
DeviceType = DEVICE_TYPE,
IPAddress = "0.0.0.0",
IPAddressDns = "0.0.0.0"
};
/*rasEnt.Options |= (int)ConnectionOptions.UseCountryAndAreaCodes;
rasEnt.Options |= (int)ConnectionOptions.UseLogonCredentials;*/
rasEnt.Options = 4194304;
RasDialParams dialParams = new RasDialParams()
{
UserName = USER,
Password = PWD,
};
cEntry = Ras.CreateEntry(rasEnt, dialParams);
}
注意“ rasEnt.Options = 4194304”(已進行硬編碼),以具有通過手動配置連接而由設備生成的設置的准確副本。 奇怪的是,如果我在調試模式下比較2個條目,則兩者相等-這意味着所有屬性都相等,唯一的區別是名稱。 我敢肯定,也使用反射來比較對象。
我通過以下方式撥打連接:
RasError re = cEntry.Dial(false, new RasDialParams(CONNAME, USER, PWD));
萬一Test1我得到“成功”,萬一Test2我得到“未知”錯誤。
您能幫我解決那個討厭的問題嗎?
現在,我以手動添加必要的注冊表項結束-只是在創建連接之前和之后檢查注冊表中的差異。 不是一個干凈的解決方案,但沒有找到其他解決方案。 似乎工作穩定,我可以撥打以此方式創建的連接。 我會看到,在生產階段是否可以。
主要問題是通過代碼撥號:在嘗試時,我遇到了其他問題。 盡管以上來自Hogo的代碼提供了很多幫助,但是有一些技巧對於無憂編碼很有用:
首先,以下提示要求您手動撥打連接並通過在Internet Explorer上運行任何網站來測試Internet。 如果互聯網正常運行,那么最好通過代碼撥號。
要手動執行:
使用“網絡和撥號連接”建立一個名為“ GPRS”的新連接(使用此鏈接: http : //www.e-consystems.com/gprs.asp )。 如果您使用的是airtel SIM,則將baudarate(波特率)從19200更改為115200。連接它並檢查互聯網是否正常工作。
通過代碼撥號:
手動創建名為“ GPRS”的連接時,在GPRS文件夾下會創建3個寄存器。 (HKEY_CURRENT_USER \\ Comm \\ RasBook \\ GPRS)。 這些文件夾無法正常查看。 要求將應用程序安裝在可讀取寄存器的PDA中。無需查看內部內容,如果這樣做,則根據Windows版本下載它們。
在創建的3個寄存器中,只有兩個是相關的(DevCfg和Entry)。 手動撥打並連接到Internet后,將數據從DevCfg和Entry寄存器復制到文本文件(DevCfg.txt和Entry.txt),以便以后可以復制這些值。 要將寄存器復制到文本文件/從寄存器復制到文本文件,請使用:RegQueryValueEx(...),RegOpenKeyEx(..),RegSetValueEx(...)和其他相關功能(請參閱http://msdn.microsoft.com/zh-cn/庫/ windows /桌面/ms724911%28v=vs.85%29.aspx )
例如:讀取注冊並寫入文本文件:
public static bool ReadRegString(IntPtr hKey, string lpszSubKey, string lpValueName)
{
try
{
string str = "";
byte[] lpData = new byte[684];
uint lpcbValue = 684;
uint dwType = Utils.REG_BINARY;
IntPtr phkResult = new IntPtr();
Utils.RegOpenKeyEx(hKey, lpszSubKey, 0, 0, ref phkResult);
if (phkResult != IntPtr.Zero)
{
int x = Utils.RegQueryValueEx(phkResult, lpValueName, 0, ref dwType, lpData, ref lpcbValue);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 684; i++)
{
if (i != 683)
sb.Append(lpData[i].ToString() + "|");
else
sb.Append(lpData[i].ToString());
}
using (System.IO.StreamWriter outfile = new System.IO.StreamWriter(filePath))
{
outfile.Write(sb.ToString());
}
Utils.RegCloseKey(phkResult);
}
}
if (Utils.ReadRegString(Utils.HKEY_CURRENT_USER, @"Comm\RasBook\GPRS", "DevCfg"))
{
textBoxSysVers.Text = "Succeeded Make Text File.";
}
a)創建RasEntry對象
b)將數據從創建的“ Entry.txt和DevCfg.txt”文本文件復制到寄存器Entry和DevCfg。
c)撥打RasError re = cEntry.Dial(...)
例如:
private const string CONNAME = "GPRS";
private const string PHONENR = "*99#";
private const string USER = "xx";
private const string PWD = "xx";
private const string DEVICE_TYPE = "modem";
private const string DEVICE_NAME = "Cellular Line";
{ RasEntry cEntry = new RasEntry()
{
Name = CONNAME,
CountryCode = 91,
AreaCode = "120",
PhoneNumber = PHONENR,
DeviceName = DEVICE_NAME,
DeviceType = DEVICE_TYPE,
IPAddress = "0.0.0.0",
IPAddressDns = "0.0.0.0",Options=4194304
};
RasDialParams dialParams = new RasDialParams()
{
UserName = USER,
Password = PWD,
EntryName = CONNAME,
Domain = " "
}
if (Utils.WriteRegValue(Utils.HKEY_CURRENT_USER, @"Comm\RasBook\GPRS", "DevCfg","Entry",3))
{
RasError re = cEntry.Dial(false, new RasDialParams(CONNAME, USER, PWD));
RasError rs = re;
textBoxInfo.Text = re.ToString() + " : Dial Status";
if(rs.ToString()=="Success")
textBoxInfo.Text = cEntry.Status.State.ToString() + " : Dial Status";
}
}
public static Boolean WriteRegValue(IntPtr hKey, string lpszSubKey, string lpValueName1, string lpValueName1,uint dwType)
{
int iOper = 1;
filePath = @"`enter code here`\DevCfg.txt";
IntPtr phkResult = new IntPtr();
Utils.RegOpenKeyEx(hKey, lpszSubKey, 0, 0, ref phkResult);
if (phkResult == IntPtr.Zero)
{
int iSecurity = 0;
int dwDisp = 0;
RegCreateKeyEx(hKey, lpszSubKey, 0, null, 0, 0, ref iSecurity, ref phkResult, ref dwDisp);
}
if (phkResult != IntPtr.Zero)
{
byte[] bytes = new byte[684];
string[] text = new string[684];
using (System.IO.StreamReader streamReader = new System.IO.StreamReader(filePath, Encoding.UTF8))
{
text = streamReader.ReadToEnd().Split('|');
}
for (int i = 0; i < 684; i++)
{
bytes[i] = Convert.ToByte(Convert.ToInt32(text[i]));
}
iOper = Utils.RegSetValueEx(phkResult, lpValueName1, 0, dwType, bytes, (uint)bytes.Length);
Utils.RegCloseKey(phkResult);
}}
// SImilary從lpValueName2完成
}}
hogo的代碼與這之間的區別在於,此處未創建RasEntry。 它由寄存器提供。 我在創建對象和提出建議時遇到困難。 希望能幫助到你 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.