DataType:
西门子
?以下代码供参考,不保证能够正常编译。
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
public static class SiemensTypeHelper
{
public static void Parse(string input, out SiemensArea dataType, out int dbNumber, out SiemensVarType varType, out int address, out int bitNumber,out object defaultvalue)
{
bitNumber = -1;
dbNumber = 0;
defaultvalue = 0;
switch (input.Substring(0, 2))
{
case "DB":
string[] strings = input.Split(new char[] { '.' });
if (strings.Length < 2)
throw new InvalidAddressException("To few periods for DB address");
var pos = GetNumPosition(strings[1]);
dataType = SiemensArea.DataBlock;
dbNumber = int.Parse(strings[0].Substring(2));
address = int.Parse(strings[1].Substring(pos));
string dbType = strings[1].Substring(0, pos);
switch (dbType)
{
case "DBB":
case "BYTE":
varType = SiemensVarType.Byte;
defaultvalue = 0;
return;
case "WORD":
case "DBW":
varType = SiemensVarType.Word;
defaultvalue = 0;
return;
case "DINT":
varType = SiemensVarType.DInt;
defaultvalue = 0;
return;
case "DWORD":
case "DBE":
varType = SiemensVarType.DWord;
defaultvalue = 0;
return;
case "INT":
case "SHORT":
case "DBI":
varType = SiemensVarType.Int;
defaultvalue = 0;
return;
case "REAL":
case "DBR":
varType = SiemensVarType.Real;
defaultvalue = 0;
return;
case "STRING":
case "DBS":
varType = SiemensVarType.String;
defaultvalue = "";
return;
case "BOOLEAN":
case "DBX":
bitNumber = int.Parse(strings[2]);
if (bitNumber > 7)
throw new InvalidAddressException("Bit can only be 0-7");
varType = SiemensVarType.Bit;
defaultvalue = 0;
return;
default:
throw new InvalidAddressException();
}
case "EB":
// Input byte
dataType = SiemensArea.Input;
dbNumber = 0;
address = int.Parse(input.Substring(2));
varType = SiemensVarType.Byte;
defaultvalue = 0;
return;
case "EW":
// Input word
dataType = SiemensArea.Input;
dbNumber = 0;
address = int.Parse(input.Substring(2));
varType = SiemensVarType.Word;
defaultvalue = 0;
return;
case "ED":
// Input double-word
dataType = SiemensArea.Input;
dbNumber = 0;
address = int.Parse(input.Substring(2));
varType = SiemensVarType.DWord;
defaultvalue = 0;
return;
case "AB":
// Output byte
dataType = SiemensArea.Output;
dbNumber = 0;
address = int.Parse(input.Substring(2));
varType = SiemensVarType.Byte;
defaultvalue = 0;
return;
case "AW":
// Output word
dataType = SiemensArea.Output;
dbNumber = 0;
address = int.Parse(input.Substring(2));
varType = SiemensVarType.Word;
defaultvalue = 0;
return;
case "AD":
// Output double-word
dataType = SiemensArea.Output;
dbNumber = 0;
address = int.Parse(input.Substring(2));
varType = SiemensVarType.DWord;
defaultvalue = 0;
return;
case "MB":
// Memory byte
dataType = SiemensArea.Memory;
dbNumber = 0;
address = int.Parse(input.Substring(2));
varType = SiemensVarType.Byte;
defaultvalue = 0;
return;
case "MW":
// Memory word
dataType = SiemensArea.Memory;
dbNumber = 0;
address = int.Parse(input.Substring(2));
varType = SiemensVarType.Word;
defaultvalue = 0;
return;
case "MD":
// Memory double-word
dataType = SiemensArea.Memory;
dbNumber = 0;
address = int.Parse(input.Substring(2));
varType = SiemensVarType.DWord;
defaultvalue = 0;
return;
default:
switch (input.Substring(0, 1))
{
case "E":
case "I":
// Input
dataType = SiemensArea.Input;
varType = SiemensVarType.Bit;
break;
case "A":
case "O":
// Output
dataType = SiemensArea.Output;
varType = SiemensVarType.Bit;
break;
case "M":
// Memory
dataType = SiemensArea.Memory;
varType = SiemensVarType.Byte;
break;
case "T":
// Timer
dataType = SiemensArea.Timer;
dbNumber = 0;
address = int.Parse(input.Substring(1));
varType = SiemensVarType.Timer;
return;
case "Z":
case "C":
// Counter
dataType = SiemensArea.Timer;
dbNumber = 0;
address = int.Parse(input.Substring(1));
varType = SiemensVarType.Counter;
return;
default:
throw new InvalidAddressException(string.Format("{0} is not a valid address", input.Substring(0, 1)));
}
string txt2 = input.Substring(1);
if (txt2.IndexOf(".") == -1)
throw new InvalidAddressException("To few periods for DB address");
address = int.Parse(txt2.Substring(0, txt2.IndexOf(".")));
bitNumber = int.Parse(txt2.Substring(txt2.IndexOf(".") + 1));
if (bitNumber > 7)
throw new InvalidAddressException("Bit can only be 0-7");
return;
}
}
private static int GetNumPosition(string str)
{
var regex = new Regex(@"\d");
return regex.Match(str).Index;
}
}