|
- using System;
-
- namespace BPASmartClient.Academy.Model
- {
- public static class Expand
- {
- /// <summary>
- /// 获取 M 区 和 VW 区的Modbus的地址
- /// </summary>
- /// <param name="address"></param>
- /// <returns></returns>
- public static string ToAdd(this string address)
- {
- if (address == null) return "";
- if (address.Length > 0)
- {
- address = address.Trim();
- if (address.ToUpper().Contains("GM") && address.Length >= 3)
- {
- var res = address.Remove(0, 2);
- if (res != null && res.Length > 0)
- {
- return (int.Parse(res) + 4096).ToString();
- }
- }
- else if (address.ToUpper().Contains("M") && address.Length >= 4)
- {
- var res = address.Substring(1).Split('.');
- if (res != null && res.Length == 2)
- {
- if (int.TryParse(res[0], out int firstAddress) && int.TryParse(res[1], out int ExitAddress))
- {
- if (ExitAddress >= 0 && ExitAddress <= 7)
- {
- return ((firstAddress * 8) + 320 + ExitAddress).ToString();
- }
- }
- }
- }
-
- else if (address.ToUpper().Contains("I") && address.Length >= 2)
- {
- var res = address.Substring(1).Split('.');
- if (res != null && res.Length == 2)
- {
- if (int.TryParse(res[0], out int firstAddress) && int.TryParse(res[1], out int ExitAddress))
- {
- if (ExitAddress >= 0 && ExitAddress <= 7)
- {
- return ((firstAddress * 8) + ExitAddress).ToString();
- }
- }
- }
- }
- else if (address.ToUpper().Contains("GI") && address.Length >= 3)
- {
- var res = address.Remove(0, 2);
- if (res != null && res.Length > 0)
- {
- return (int.Parse(res)).ToString();
- }
- }
- else if (address.ToUpper().Contains("LB") && address.Length >= 3)
- {
- var res = address.Substring(2);
- if (res != null && res.Length > 0)
- {
- if (int.TryParse(res, out int firstAddress))
- {
- return firstAddress.ToString();
- }
- }
- }
-
- else if ((address.ToUpper().Contains("VW") || address.ToUpper().Contains("VD")) && address.Length >= 3)
- {
- var res = address.Substring(2);
- if (res != null && int.TryParse(res, out int tempAddress))
- {
- return ((tempAddress / 2) + 100).ToString();
- }
- }
- else if (address.ToUpper().Contains("LW") && address.Length >= 3)
- {
- var res = address.Substring(2);
- if (res != null && int.TryParse(res, out int LwAddress))
- {
- return LwAddress.ToString();
- }
- }
- else if (address.ToUpper().Contains("D") && address.Length == 5)
- {
- try
- {
- //D1001
- string head = (System.Convert.ToInt32(address.Substring(1, 1))).ToString();
- int num = System.Convert.ToInt32(address.Substring(2, 3));
- int len = num.ToString().Length;
- string tail = string.Empty;
- switch (len)
- {
-
- case 1:
- if ((System.Convert.ToInt32(address.Substring(4, 1))).ToString().Length > 1)
- {
- tail = "0" + (System.Convert.ToInt32(address.Substring(4, 1))).ToString();
- }
- else
- {
- tail = "00" + (System.Convert.ToInt32(address.Substring(4, 1))).ToString();
- }
- break;
- case 2:
- if ((System.Convert.ToInt32(address.Substring(3, 2))).ToString().Length > 2)
- {
- tail = (System.Convert.ToInt32(address.Substring(3, 2))).ToString();
- }
- else
- {
- tail = "0" + (System.Convert.ToInt32(address.Substring(3, 2))).ToString();
- }
- break;
- case 3:
- tail = (System.Convert.ToInt32(address.Substring(2, 3))).ToString();
- break;
- }
-
- address = head + tail;
- return address;
- }
- catch (Exception)
- {
- //打印日志
-
- }
-
- }
- }
- return "";
- }
- }
- }
|