Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
Donate
Please sign in before you donate.
Scan WeChat QR to Pay
Cancel
Complete
Prompt
Switch to Alipay.
OK
Cancel
1 Star 0 Fork 18

DebugMan/SharpSCADA

forked from 盘古软件/SharpSCADA
Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
文件
master
Branches (2)
master
develop
master
Branches (2)
master
develop
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
master
Branches (2)
master
develop
Eval.cs 29.69 KB
Copy Edit Raw Blame History
panvsoft authored 2020年04月10日 22:26 +08:00 . 初始版本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.InteropServices;
namespace DataService
{
public class ExpressionEval : IDisposable
{
Expression _param1;
List<ITag> _tagList = new List<ITag>();
public List<ITag> TagList
{
get
{
return _tagList;
}
}
IDataServer _server;
public ExpressionEval(IDataServer server)
{
_server = server;
_param1 = Expression.Constant(this);
}
public Delegate Eval(string expression)
{
if (string.IsNullOrEmpty(expression)) return null;
var lambda = ComplieRpnExp(RpnExpression(expression));
if (lambda != null) return lambda.Compile();
return null;
}
public Delegate WriteEval(string expression)
{
if (_server == null || string.IsNullOrEmpty(expression)) return null;
if (_server[expression.ToUpper()] != null)
{
return new Func<object, int>((object value) => { return WriteTag(expression, value); });
}
return null;
}
public Func<int> WriteEval(string expression1, string expression2)
{
if (_server == null || string.IsNullOrEmpty(expression2)) return null;
if (_server[expression1.ToUpper()] != null)
{
var dele = Eval(expression2);
var funcbool = dele as Func<bool>;
if (funcbool != null)
return () => { return WriteTag(expression1, funcbool()); };
var funcint = dele as Func<int>;
if (funcint != null)
return () => { return WriteTag(expression1, funcint()); };
var funcfloat = dele as Func<float>;
if (funcfloat != null)
return () => { return WriteTag(expression1, funcfloat()); };
var funcstring = dele as Func<string>;
if (funcstring != null)
return () => { return WriteTag(expression1, funcstring()); };
}
return null;
}
public static bool ValidationExpression(string expression)
{
return true;//可加入正则表达式验证
}
/// <summary>
/// 操作符运算级别
/// </summary>
/// <param name="strOperator">操作符</param>
/// <returns>操作运算符,空格返回0,出错返回-1</returns>
private static byte GetOperatorLevel(char strOperator)
{
switch (strOperator)
{
case '~':
return 10;
case '*':
case '/':
case '%':
return 9;
case '+':
case '-':
return 8;
case '>':
case '<':
return 7;
case '&':
return 6;
case '^':
return 5;
case '|':
return 4;
case '=':
case '!':
case '?':
return 3;
case '(':
return 2;
case ')':
return 1;
//case ':':
default:
return 0;
}
}
/// <summary>
/// 将中缀表达式转换为逆波兰表达式
/// </summary>
/// <param name="expression">标准中缀表达式</param>
/// <returns>标准逆波兰表达式</returns>
public static List<string> RpnExpression(string expression)
{
//加入结束标记
//定义出栈和入栈堆栈
string[] strNum = expression.Split('~', '%', '>', '<', '=', '!', '&', '|', '?', '#', '^', '+', '-', '*', '/', '(', ')');
if (strNum.Length < 2) return new List<string>() { expression };
//操作运算符堆栈
Stack<Operator> oper = new Stack<Operator>();
//定义输出堆栈
List<string> output = new List<string>();
//定义前缀表达式字符读取指针
int i = 0;
//定义当前读取数字数组指针
int n = -1;
//定义操作运算符级别函数
Operator op = new Operator();
//输出堆栈的大小
int intStackCount = 0;
//从左到右读取前缀表达式
while (i < expression.Length)
{
//读取一个字符
char strChar = expression[i];
//取字符的运算级别
if (strChar == '#') { i++; continue; }
byte intLevel = GetOperatorLevel(strChar);
if (intLevel == 0)
//数字直接推入输出堆栈
{
while (n++ < strNum.Length)
{
if (strNum[n] != "")
{
output.Add(strNum[n]);
i += strNum[n].Length;
//移动数组指针
break;
}
}
}
else //操作字符根据运算字符级别推入运算符堆栈
{
if (oper.Count == 0)
{
//运算符堆栈为空,直接推入堆栈
oper.Push(new Operator(strChar, intLevel));
//移动字符读取指针
i++;
}
else
{
op = oper.Peek();
if (intLevel > op.Level || intLevel == 2)
{
//运算字符比运算符堆栈最后的级别高或者运算符为'('直接推入运算符堆栈
oper.Push(new Operator(strChar, intLevel));
//移动字符读取指针
i++;
}
else
{
//运算字符不高于运算符堆栈最后的级别,则将运算符堆栈出栈,直到比其高为止
intStackCount = oper.Count;
for (int m = 0; m < intStackCount; m++)
{
op = oper.Peek();
if (op.Level >= intLevel)
{
//将操作符出栈并压入输入堆栈
char o = op.OperatorStack;
if (!(o == ')' || o == '('))
{
output.Add(o.ToString());
}
oper.Pop();
if (op.Level == 2)
{
//如果操作符堆栈中最后的操作符为'('则停止出栈
i++;
break;
}
}
else
{
//直到运算符已经高出运算符栈中最后的级别,则入栈
oper.Push(new Operator(strChar, intLevel));
i++;
break;
}
}
}
}
}
}
intStackCount = oper.Count;
for (int m = 0; m < intStackCount; m++)
{
op = oper.Peek();
output.Add(op.OperatorStack.ToString());
oper.Pop();
}
return output;
}
/// <summary>
/// 解逆波兰表达式
/// </summary>
/// <param name="expression">标准逆波兰表达式</param>
/// <returns>逆波兰表达式的解</returns>
public LambdaExpression ComplieRpnExp(List<string> strNum)
{
_tagList.Clear();
//拆分逆波兰表达式
int intLenth = strNum.Count;
if (intLenth == 0) return null;
//定义数字堆栈
try
{
Stack<Expression> number = new Stack<Expression>();
for (int i = 0; i < intLenth; i++)
{
string expr = strNum[i];
switch (expr)
{
case "~":
if (number.Count > 0)
{
Expression left = number.Pop();
number.Push(Expression.Not(left));
}
break;
case "*":
if (number.Count > 1)
{
Expression right = number.Pop();
Expression left = number.Pop();
if (left.Type != right.Type)
{
if (left.Type != typeof(float))
left = Expression.Convert(left, typeof(float));
if (right.Type != typeof(float))
right = Expression.Convert(right, typeof(float));
}
number.Push(Expression.Multiply(left, right));
}
break;
case "/":
if (number.Count > 1)
{
Expression right = number.Pop();
Expression left = number.Pop();
if (left.Type != right.Type)
{
if (left.Type != typeof(float))
left = Expression.Convert(left, typeof(float));
if (right.Type != typeof(float))
right = Expression.Convert(right, typeof(float));
}
number.Push(Expression.Divide(left, right));
}
break;
case "%":
if (number.Count > 1)
{
Expression right = number.Pop();
Expression left = number.Pop();
if (left.Type != right.Type)
{
if (left.Type != typeof(int))
left = Expression.Convert(left, typeof(int));
if (right.Type != typeof(int))
right = Expression.Convert(right, typeof(int));
}
number.Push(Expression.Modulo(left, right));
}
break;
case "+":
if (number.Count > 1)
{
Expression right = number.Pop();
Expression left = number.Pop();
if (left.Type == typeof(string) || right.Type == typeof(string))
{
if (left.Type != typeof(string))
left = Expression.Convert(left, typeof(object));
if (right.Type != typeof(string))
right = Expression.Convert(right, typeof(object));
number.Push(Expression.Call(typeof(string).GetMethod("Concat", new Type[] { typeof(object), typeof(object) }), left, right));
}
else
{
if (left.Type != right.Type)
{
if (left.Type != typeof(float))
left = Expression.Convert(left, typeof(float));
if (right.Type != typeof(float))
right = Expression.Convert(right, typeof(float));
}
number.Push(Expression.Add(left, right));
}
}
break;
case "-":
if (number.Count > 1)
{
Expression right = number.Pop();
Expression left = number.Pop();
if (left.Type != right.Type)
{
if (left.Type != typeof(float))
left = Expression.Convert(left, typeof(float));
if (right.Type != typeof(float))
right = Expression.Convert(right, typeof(float));
}
number.Push(Expression.Subtract(left, right));
}
break;
case ">":
if (number.Count > 1)
{
Expression right = number.Pop();
Expression left = number.Pop();
if (left.Type != right.Type)
{
if (left.Type != typeof(float))
left = Expression.Convert(left, typeof(float));
if (right.Type != typeof(float))
right = Expression.Convert(right, typeof(float));
}
number.Push(Expression.GreaterThan(left, right));
}
break;
case "<":
if (number.Count > 1)
{
Expression right = number.Pop();
Expression left = number.Pop();
if (left.Type != right.Type)
{
if (left.Type != typeof(float))
left = Expression.Convert(left, typeof(float));
if (right.Type != typeof(float))
right = Expression.Convert(right, typeof(float));
}
number.Push(Expression.LessThan(left, right));
}
break;
case "&":
if (number.Count > 1)
{
Expression right = number.Pop();
Expression left = number.Pop();
number.Push(Expression.And(left, right));
}
break;
case "^":
if (number.Count > 1)
{
Expression right = number.Pop();
Expression left = number.Pop();
number.Push(Expression.ExclusiveOr(left, right));
}
break;
case "|":
if (number.Count > 1)
{
Expression right = number.Pop();
Expression left = number.Pop();
number.Push(Expression.Or(left, right));
}
break;
case "=":
if (number.Count > 1)
{
Expression right = number.Pop();
Expression left = number.Pop();
if (left.Type != right.Type)
{
if (left.Type != typeof(float))
left = Expression.Convert(left, typeof(float));
if (right.Type != typeof(float))
right = Expression.Convert(right, typeof(float));
}
number.Push(Expression.Equal(left, right));
}
break;
case "!":
if (number.Count > 1)
{
Expression right = number.Pop();
Expression left = number.Pop();
if (left.Type != right.Type)
{
if (left.Type != typeof(float))
left = Expression.Convert(left, typeof(float));
if (right.Type != typeof(float))
right = Expression.Convert(right, typeof(float));
}
number.Push(Expression.NotEqual(left, right));
}
break;
case "?":
if (number.Count > 1)
{
Expression right = number.Pop();
Expression left = number.Pop();
if (left.Type != right.Type)
{
if (left.Type != typeof(float))
left = Expression.Convert(left, typeof(float));
if (right.Type != typeof(float))
right = Expression.Convert(right, typeof(float));
}
Expression test = number.Pop();
number.Push(Expression.Condition(test, left, right));
}
break;
default:
if (expr[0] == '@')
{
switch (expr.Substring(1).ToUpper())
{
case "TIME":
{
Expression<Func<string>> f = () => DateTime.Now.ToShortTimeString();
number.Push(f.Body);
}
goto lab1;
case "DATE":
{
Expression<Func<string>> f = () => DateTime.Now.ToShortDateString();
number.Push(f.Body);
}
goto lab1;
case "DATETIME":
{
Expression<Func<string>> f = () => DateTime.Now.ToString();
number.Push(f.Body);
}
goto lab1;
case "APP":
{
Expression<Func<string>> f = () => AppDomain.CurrentDomain.FriendlyName;
number.Push(f.Body);
}
goto lab1;
case "NAME":
{
Expression<Func<string>> f = () => Environment.MachineName;
number.Push(f.Body);
}
goto lab1;
case "PATH":
{
Expression<Func<string>> f = () => Environment.CurrentDirectory;
number.Push(f.Body);
}
goto lab1;
case "USER":
{
Expression<Func<string>> f = () => Environment.UserName;
number.Push(f.Body);
}
goto lab1;
case "REGION":
{
Expression<Func<string>> f = () => System.Globalization.CultureInfo.CurrentCulture.Name;
number.Push(f.Body);
}
goto lab1;
}
}
object result;
if (IsConstant(expr, out result))
{
number.Push(Expression.Constant(result));
}
else
{
number.Push(GetTagExpression(expr));
}
lab1:
break;
}
}
Expression d = number.Pop();
return Expression.Lambda(d);
}
catch (Exception e) { return null; }
}
MethodInfo _boolinfo = typeof(ExpressionEval).GetMethod("GetBool");
MethodInfo _floatinfo = typeof(ExpressionEval).GetMethod("GetFloat");
MethodInfo _intinfo = typeof(ExpressionEval).GetMethod("GetInt");
MethodInfo _stringinfo = typeof(ExpressionEval).GetMethod("GetString");
public Expression GetTagExpression(string tagName)
{
if (_server == null) return Expression.Empty();
ITag tag = _server[tagName];
switch (tag.Address.VarType)
{
case DataType.BOOL:
return Expression.Call(_param1, _boolinfo, Expression.Constant(tagName));
case DataType.BYTE:
case DataType.WORD:
case DataType.SHORT:
case DataType.DWORD:
case DataType.INT:
return Expression.Call(_param1, _intinfo, Expression.Constant(tagName));
case DataType.FLOAT:
return Expression.Call(_param1, _floatinfo, Expression.Constant(tagName));
case DataType.STR:
return Expression.Call(_param1, _stringinfo, Expression.Constant(tagName));
default:
return Expression.Empty();
}
}
public bool GetBool(string tagName)
{
if (_server == null) return false;
ITag tag = _server[tagName];
switch (tag.Address.VarType)
{
case DataType.BOOL:
return tag.Value.Boolean;
case DataType.BYTE:
return Convert.ToBoolean(tag.Value.Byte);
case DataType.WORD:
return Convert.ToBoolean(tag.Value.Word);
case DataType.SHORT:
return Convert.ToBoolean(tag.Value.Int16);
case DataType.DWORD:
return Convert.ToBoolean(tag.Value.DWord);
case DataType.INT:
return Convert.ToBoolean(tag.Value.Int32);
case DataType.FLOAT:
return Convert.ToBoolean(tag.Value.Single);
case DataType.STR:
return Convert.ToBoolean(tag.ToString());
default:
return false;
}
}
public float GetFloat(string tagName)
{
if (_server == null) return 0f;
ITag tag = _server[tagName];
return tag.ScaleToValue(tag.Value);
}
public int GetInt(string tagName)
{
if (_server == null) return 0;
ITag tag = _server[tagName];
switch (tag.Address.VarType)
{
case DataType.BOOL:
return tag.Value.Boolean ? 1 : 0;
case DataType.BYTE:
return tag.Value.Byte;
case DataType.WORD:
return tag.Value.Word;
case DataType.SHORT:
return tag.Value.Int16;
case DataType.DWORD:
return (int)tag.Value.DWord;
case DataType.INT:
return tag.Value.Int32;
case DataType.FLOAT:
return Convert.ToInt32(tag.Value.Single);
case DataType.STR:
return int.Parse(tag.ToString());
default:
return 0;
}
}
public string GetString(string tagName)
{
return _server == null ? null : _server[tagName].ToString();
}
public int WriteTag(string tagName, object value)
{
if (_server == null || value == null) return -1;
ITag tag = _server[tagName];
if (tag.Address.VarType == DataType.BOOL || tag.Address.VarType == DataType.STR)
return tag.Write(value);
else
{
float temp;
string str = value as string;
if (str == null) temp = Convert.ToSingle(value);
else
{
if (!float.TryParse(str, out temp))
return -1;
}
return tag.Write(tag.ValueToScale(temp));
}
}
private bool IsConstant(string str, out object value)
{
if (str.Length > 1 & str[0] == '\'' && str[str.Length - 1] == '\'')
{
value = str.Trim('\'');
return true;
}
string upp = str.ToUpper();
if (upp == "TRUE")
{
value = true;
return true;
}
else if (upp == "FALSE")
{
value = false;
return true;
}
if (_server != null)
{
var tag = _server[upp];
if (tag != null)
{
if (!_tagList.Contains(tag))
_tagList.Add(tag);
value = null;
return false;
}
}
int dotcount = 0;
for (int i = 0; i < str.Length; i++)
{
char opr = str[i];
if (opr < '0' || opr > '9')
{
if (opr != '.')
{
value = str;
return true;
}
else
{
if (dotcount > 0)
{
value = str;
return true;
}
dotcount++;
}
}
}
//value = (dotcount == 0 ? int.Parse(str) : float.Parse(str));
if (dotcount == 0)
value = int.Parse(str);
else value = float.Parse(str);
return true;
}
public void Clear()
{
//_param1 = null;
_tagList.Clear();
//_tagList = null;
}
public void Dispose()
{
_param1 = null;
_tagList.Clear();
_tagList = null;
_boolinfo = _floatinfo = _stringinfo = null;
}
}
[StructLayout(LayoutKind.Sequential)]
/// <summary>
/// 操作符结构
/// </summary>
public struct Operator
{
public char OperatorStack;
public byte Level;
public Operator(char OperatorStack, byte Level)
{
this.OperatorStack = OperatorStack;
this.Level = Level;
}
}
}
Loading...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

About

一款工业级轻量级组态软件 1.轻量级工控网关: 支持当前几种主要的工业协议如西门子的Profinet、AB的EtherNetIPs、施耐德的Modbus和OPC。采用类OPC接口网关。 2.数据采集、归档、预警及配置工具 支持实时数据采集、历史数据归档、变量触发预警,并使用TagConfig工具简单的配置实现。 3.人机界面(设计时和运行时)。
No labels
GPL-3.0
Use GPL-3.0
Cancel

Releases

No release

Contributors

All

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/Debugman/SharpSCADA.git
git@gitee.com:Debugman/SharpSCADA.git
Debugman
SharpSCADA
SharpSCADA
master
Going to Help Center

Search

Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /