S7DB类型

发布时间:2023年12月22日

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 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;

}

}

文章来源:https://blog.csdn.net/daonidedie/article/details/135130371
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。