DataType:
- 0x01 - BIT
- :一个无符号的bit
- 0x02 - BYTE
- :一个8位的数字
- 0x03 - CHAR
- :一个字符
- 0x04 - WORD
- :两个字节宽的无符号整数
- 0x05 - INT
- :两个字节宽的有符号整数。
- 0x06 - DWORD
- :四字节宽的无符号整数
- 0x07 - DINT
- :四字节宽的有符号整数
- 0x08 - REAL
- :四个字节宽的IEEE浮点数
- 0x1c - COUNTER
- :PLC程序计数器使用的计数器类型
西门子
以下代码供参考,不保证能够正常编译。
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 bytedataType = SiemensArea.Input;dbNumber = 0;address = int.Parse(input.Substring(2));varType = SiemensVarType.Byte;defaultvalue = 0;return;case "EW":// Input worddataType = SiemensArea.Input;dbNumber = 0;address = int.Parse(input.Substring(2));varType = SiemensVarType.Word;defaultvalue = 0;return;case "ED":// Input double-worddataType = SiemensArea.Input;dbNumber = 0;address = int.Parse(input.Substring(2));varType = SiemensVarType.DWord;defaultvalue = 0;return;case "AB":// Output bytedataType = SiemensArea.Output;dbNumber = 0;address = int.Parse(input.Substring(2));varType = SiemensVarType.Byte;defaultvalue = 0;return;case "AW":// Output worddataType = SiemensArea.Output;dbNumber = 0;address = int.Parse(input.Substring(2));varType = SiemensVarType.Word;defaultvalue = 0;return;case "AD":// Output double-worddataType = SiemensArea.Output;dbNumber = 0;address = int.Parse(input.Substring(2));varType = SiemensVarType.DWord;defaultvalue = 0;return;case "MB":// Memory bytedataType = SiemensArea.Memory;dbNumber = 0;address = int.Parse(input.Substring(2));varType = SiemensVarType.Byte;defaultvalue = 0;return;case "MW":// Memory worddataType = SiemensArea.Memory;dbNumber = 0;address = int.Parse(input.Substring(2));varType = SiemensVarType.Word;defaultvalue = 0;return;case "MD":// Memory double-worddataType = 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":// InputdataType = SiemensArea.Input;varType = SiemensVarType.Bit;break;case "A":case "O":// OutputdataType = SiemensArea.Output;varType = SiemensVarType.Bit;break;case "M":// MemorydataType = SiemensArea.Memory;varType = SiemensVarType.Byte;break;case "T":// TimerdataType = SiemensArea.Timer;dbNumber = 0;address = int.Parse(input.Substring(1));varType = SiemensVarType.Timer;return;case "Z":case "C":// CounterdataType = 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;}}