|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- using System;
- using System.Windows.Forms;
-
- namespace BPASmartClient.DosingSystem.Model
- {
- public static class Expand
- {
- public static bool Tobool(this ushort value)
- {
- if (value == 1)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- /// <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 "";
- }
- }
- }
|