格式如下: [Communication] COM=3 Baudrate=19200 Parity=e DataBit=7 StopBit=1[Initial Command] Start PLC =sdjkhjfhowehor[Normal Command] Get X Data = jaksbdkajsbd Get Y Data = kjsndkjas Get M Data = kjqhdkhja[Direct Command] Start Process = kjasdkjs Push the Door = kjskhjas[Other] Timeout in ms = 1000 Retry Time = 3
</appSettings>2、输入 <add key="conn" value ="database ......"/> 3、在程序中读取。 string conn = System.Configuration.ConfigurationSettings.AppSettings["conn"] as string;
读ini可以做,但是现在基本不用了。多用xml,按我的步骤做一下。
哎~~謝謝各位說了這麼多!可是我的意思是1.我有一個INI文件 內容如下 [Communication] COM=3 Baudrate=19200 Parity=e DataBit=7 StopBit=1[Initial Command] Start PLC =sdjkhjfhowehor[Normal Command] Get X Data = jaksbdkajsbd Get Y Data = kjsndkjas Get M Data = kjqhdkhja[Direct Command] Start Process = kjasdkjs Push the Door = kjskhjas[Other] Timeout in ms = 1000 Retry Time = 3其中 [Communication]裏,定義的是要和通訊端達成協議(Shake Hand)所需要的參數。 [Initial Command]裏,定義的是通訊完成後,一開始所需要執行的指令。 [Normal Command]裏,定義的是[Initial Command]後,所需要常規性執行的指令,比如說是取得X值或是取得M值之類,這些命令會一直循環執行,以更新取得後的資料。 [Direct Command]裏,定義的是可以即時下的指令,下了指令之後,可以即時性的更改通訊端的設定。 [Other]裏,定義一些必要的參數 [Communication] COM=3 Baudrate=19200 Parity=e DataBit=7 StopBit=1[Initial Command] Start PLC =sdjkhjfhowehor[Normal Command] Get X Data = jaksbdkajsbd Get Y Data = kjsndkjas Get M Data = kjqhdkhja[Direct Command] Start Process = kjasdkjs Push the Door = kjskhjas[Other] Timeout in ms = 1000 Retry Time = 3其中 [Communication]裏,定義的是要和通訊端達成協議(Shake Hand)所需要的參數。 [Initial Command]裏,定義的是通訊完成後,一開始所需要執行的指令。 [Normal Command]裏,定義的是[Initial Command]後,所需要常規性執行的指令,比如說是取得X值或是取得M值之類,這些命令會一直循環執行,以更新取得後的資料。 [Direct Command]裏,定義的是可以即時下的指令,下了指令之後,可以即時性的更改通訊端的設定。 [Other]裏,定義一些必要的參數 2.定義一個類COM Class PLC = new COMClass();命令 開啟設定檔 PLC.Open(“PLC.TXT”);建立通訊 PLC.Connect();開始執行[Initial Command]及[Normal Command]指令 PLC.Start();開始執行[Direct Command]指令 PLC.Command(“指令前的文字“);結束通訊 PLC.Clos();事件 連線錯誤 PLC.ConnectError();通訊錯誤 PLC.CommError(); 我應該怎麼根據這個INI來和這些屬性,方法互動傳值呢?
[Communication]
COM=3
Baudrate=19200
Parity=e
DataBit=7
StopBit=1[Initial Command]
Start PLC =sdjkhjfhowehor[Normal Command]
Get X Data = jaksbdkajsbd
Get Y Data = kjsndkjas
Get M Data = kjqhdkhja[Direct Command]
Start Process = kjasdkjs
Push the Door = kjskhjas[Other]
Timeout in ms = 1000
Retry Time = 3
{
DateTime dtNow = DateTime.Now;
string strFilePath = strFile + dtNow.ToString("yyyyMMdd") + ".txt";
StreamWriter sw = new StreamWriter(strFilePath, true);
sw.WriteLine(strData);
sw.Close();
return true;
}
可以输出TXT
string CurrentPath = Directory.GetCurrentDirectory();
if(CurrentPath.Length!=3)
CurrentPath=CurrentPath+"\\";
string inipath=CurrentPath+"ini.ini";
Console.WriteLine("inipath={0}",inipath);
IniFile ini = new IniFile(inipath);
int firstrun,timer;
string appname;
firstrun=ini.IniReadValueInt("SYSTEM","FIRSTRUN",0);
appname=ini.IniReadValueString("SYSTEM","APPNAME","ini.exe");
timer=ini.IniReadValueInt("SYSTEM","TIMER",0);
Console.WriteLine("firstrun={0},appname={1},timer={2}",firstrun,appname,timer);
string ORACLESERVICE,ORACLEUSERNAME,ORACLEPASSWORD;
ORACLESERVICE=ini.IniReadValueString("ORACLESERVER","ORACLESERVICENAME","YXZC");
ORACLEUSERNAME=ini.IniReadValueString("ORACLESERVER","ORACLEUSERNAME","PMS");
ORACLEPASSWORD=ini.IniReadValueString("ORACLESERVER","ORACLEPASSWORD","111111");
int year,month,day,hour,minute,second;
year=ini.IniReadValueInt("TIME","YEAR",2005);
month=ini.IniReadValueInt("TIME","MONTH",7);
day=ini.IniReadValueInt("TIME","DAY",8);
hour=ini.IniReadValueInt("TIME","HOUR",9);
minute=ini.IniReadValueInt("TIME","MINUTE",10);
second=ini.IniReadValueInt("TIME","SECOND",11);
Console.WriteLine("{0}-{1}-{2} {3}:{4}:{5}",year,month,day,hour,minute,second);
int count=ini.IniReadValueInt("ACCMU","COUNT",0);
int numAccmu=5;
string[] Accmustr=new string[numAccmu+1];
double[] Accmu=new double[numAccmu+1];
for(int i=1;i=numAccmu;i++)
{
Accmustr[i]="A"+i.ToString();
Accmu[i]=double.Parse(ini.IniReadValueString("ACCMU",Accmustr[i],"0.0"));
Console.WriteLine("{0}={1}",Accmustr[i],Accmu[i]);
}
for(int i=1;i=numAccmu;i++)
{
Accmu[i]=Accmu[i]+i;
ini.IniWriteValue("ACCMU",Accmustr[i],Accmu[i].ToString());
}
C# 操作 INI 文件
public class IniFile
{
public string path; //INI文件名
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section,string key,string val,string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section,string key,string def,StringBuilder retVal,int size,string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileInt(string section,string key,int def,string filePath );
public IniFile(string INIPath)
{
path = INIPath;
}
//类的构造函数,传递INI文件名
public void IniWriteValue(string Section,string Key,string Value)
{
WritePrivateProfileString(Section,Key,Value,this.path);
}
//写INI文件
public string IniReadValueString(string Section,string Key,string def)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section,Key,def,temp,255,this.path);
return temp.ToString();
}
public int IniReadValueInt(string Section,string Key,int def)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileInt(Section,Key,def,this.path);
return (i);
}
}
</appSettings>2、输入 <add key="conn" value ="database ......"/>
3、在程序中读取。 string conn = System.Configuration.ConfigurationSettings.AppSettings["conn"] as string;
[Communication]
COM=3
Baudrate=19200
Parity=e
DataBit=7
StopBit=1[Initial Command]
Start PLC =sdjkhjfhowehor[Normal Command]
Get X Data = jaksbdkajsbd
Get Y Data = kjsndkjas
Get M Data = kjqhdkhja[Direct Command]
Start Process = kjasdkjs
Push the Door = kjskhjas[Other]
Timeout in ms = 1000
Retry Time = 3其中
[Communication]裏,定義的是要和通訊端達成協議(Shake Hand)所需要的參數。
[Initial Command]裏,定義的是通訊完成後,一開始所需要執行的指令。
[Normal Command]裏,定義的是[Initial Command]後,所需要常規性執行的指令,比如說是取得X值或是取得M值之類,這些命令會一直循環執行,以更新取得後的資料。
[Direct Command]裏,定義的是可以即時下的指令,下了指令之後,可以即時性的更改通訊端的設定。
[Other]裏,定義一些必要的參數
[Communication]
COM=3
Baudrate=19200
Parity=e
DataBit=7
StopBit=1[Initial Command]
Start PLC =sdjkhjfhowehor[Normal Command]
Get X Data = jaksbdkajsbd
Get Y Data = kjsndkjas
Get M Data = kjqhdkhja[Direct Command]
Start Process = kjasdkjs
Push the Door = kjskhjas[Other]
Timeout in ms = 1000
Retry Time = 3其中
[Communication]裏,定義的是要和通訊端達成協議(Shake Hand)所需要的參數。
[Initial Command]裏,定義的是通訊完成後,一開始所需要執行的指令。
[Normal Command]裏,定義的是[Initial Command]後,所需要常規性執行的指令,比如說是取得X值或是取得M值之類,這些命令會一直循環執行,以更新取得後的資料。
[Direct Command]裏,定義的是可以即時下的指令,下了指令之後,可以即時性的更改通訊端的設定。
[Other]裏,定義一些必要的參數
2.定義一個類COM Class
PLC = new COMClass();命令
開啟設定檔
PLC.Open(“PLC.TXT”);建立通訊
PLC.Connect();開始執行[Initial Command]及[Normal Command]指令
PLC.Start();開始執行[Direct Command]指令
PLC.Command(“指令前的文字“);結束通訊
PLC.Clos();事件
連線錯誤
PLC.ConnectError();通訊錯誤
PLC.CommError();
我應該怎麼根據這個INI來和這些屬性,方法互動傳值呢?