开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
捐赠
捐赠前请先登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
1 Star 0 Fork 18

张亮/SharpSCADA

forked from 盘古软件/SharpSCADA
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (2)
master
develop
master
分支 (2)
master
develop
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (2)
master
develop
SharpSCADA
/
src
/
Program
/
ClientDriver
/
ClientDriver.cs
SharpSCADA
/
src
/
Program
/
ClientDriver
/
ClientDriver.cs
ClientDriver.cs 51.25 KB
一键复制 编辑 原始数据 按行查看 历史
panvsoft 提交于 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 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using DataService;
namespace ClientDriver
{
[Description("客户端驱动")]
public class ClientReader : IDriver//客户端存在对TLV数据的字节序转换问题,需测试
{
short _id;
public short ID
{
get
{
return _id;
}
}
private int _timeout = 0;
public int TimeOut
{
get { return _timeout; }
set { _timeout = value; }
}
string _name;
public string Name
{
get
{
return _name;
}
}
string _ip;
public string ServerName
{
get { return _ip; }
set { _ip = value; }
}
internal Socket tcpSend;
internal Socket tcpRecive;
public bool IsClosed
{
get
{
//return tcpASynCl.Poll(-1, SelectMode.SelectRead);
return tcpSend == null || tcpRecive == null || !tcpSend.Connected || !tcpRecive.Connected;
}
}
List<IGroup> _grps = new List<IGroup>(1);
public IEnumerable<IGroup> Groups
{
get { return _grps; }
}
IDataServer _server;
public IDataServer Parent
{
get { return _server; }
}
public ClientReader(IDataServer server, short id, string name)
{
_id = id;
_server = server;
_name = name;
}
public bool Connect()
{
int port = 6543;
lock (this)
{
try
{
if (tcpRecive != null)
{
tcpRecive.Dispose();
}
if (tcpSend != null)
{
tcpSend.Dispose();
}
tcpRecive = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
tcpRecive.Connect(_ip, port);
tcpRecive.SendTimeout = _timeout;
tcpRecive.ReceiveTimeout = -1;
tcpSend = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
tcpSend.Connect(_ip, port);
tcpSend.SendTimeout = _timeout;
tcpSend.ReceiveTimeout = _timeout;
return true;
}
catch (SocketException error)
{
if (OnError != null)
OnError(this, new IOErrorEventArgs(error.Message));
return false;
}
}
}
public IGroup AddGroup(string name, short id, int updateRate, float deadBand = 0f, bool active = false)
{
ClientGroup grp = new ClientGroup(id, name, updateRate, active, this);
_grps.Add(grp);
return grp;
}
public bool RemoveGroup(IGroup grp)
{
grp.IsActive = false;
return _grps.Remove(grp);
}
public event IOErrorEventHandler OnError;
public void Dispose()
{
foreach (IGroup grp in _grps)
{
grp.Dispose();
}
_grps.Clear();
try
{
if (tcpRecive != null)
{
if (tcpRecive.Connected)
tcpRecive.Shutdown(SocketShutdown.Both);
tcpRecive.Dispose();
}
if (tcpSend != null)
{
if (tcpSend.Connected)
tcpSend.Shutdown(SocketShutdown.Both);
tcpSend.Dispose();
}
}
catch (SocketException err)
{
if (OnError != null)
OnError(this, new IOErrorEventArgs(err.Message));
}
}
}
public class ClientGroup : IGroup
{
bool _active = false;
public bool IsActive
{
get
{
return _active;
}
set
{
_active = value;
if (value)
{
if (_plcReader != null && _plcReader.tcpSend != null)
{
this._tcpRecive = _plcReader.tcpRecive;
this._tcpSend = _plcReader.tcpSend;
try
{
_addr = (_tcpSend.RemoteEndPoint as IPEndPoint).Address;
}
catch { }
Thread workItem = new Thread(new ThreadStart(ReciveData));
workItem.Priority = ThreadPriority.Highest;
workItem.Start();
if (_updateRate > 0)
ThreadPool.QueueUserWorkItem(new WaitCallback(this.OnUpdate));
else
Init();
}
}
}
}
protected short _id;
public short ID
{
get
{
return _id;
}
}
protected int _updateRate;
public int UpdateRate
{
get
{
return _updateRate;
}
set
{
_updateRate = value;
}
}
protected DeviceAddress _start;
public DeviceAddress Start
{
get
{
return _start;
}
}
protected string _name;
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
protected float _deadband;
public float DeadBand
{
get
{
return _deadband;
}
set
{
_deadband = value;
}
}
protected ClientReader _plcReader;
public IDriver Parent
{
get
{
return _plcReader;
}
}
protected Dictionary<short, ITag> _items;
public IEnumerable<ITag> Items
{
get { return _items.Values; }
}
IDataServer _server;
public IDataServer Server
{
get
{
return _server;
}
}
Socket _tcpSend, _tcpRecive;
byte[] tcpBuffer;
IPAddress _addr;
public IPAddress RemoteAddress
{
get { return _addr; }
}
public ClientGroup(short id, string name, int updateRate, bool active, ClientReader plcReader)
{
this._id = id;
this._name = name;
this._updateRate = updateRate;
this._active = active;
this._plcReader = plcReader;
this._server = plcReader.Parent;
tcpBuffer = new byte[8192];
}
object sendasync = new object();
private byte[] ReadSingleData(DeviceAddress address, DataSource source = DataSource.Cache)
{
if (_tcpSend == null) return null;
short ID = (short)address.Start;
byte type = (byte)address.VarType;
byte len = (byte)(address.DataSize);
byte[] idbits = BitConverter.GetBytes(ID);
byte[] write_data = new byte[6] { FCTCOMMAND.fctHead, FCTCOMMAND.fctReadSingle,
source == DataSource.Cache?(byte)0:(byte)1, idbits[0], idbits[1] ,len};
byte[] data = new byte[len];
SocketError error;
lock (sendasync)
{
_tcpSend.Send(write_data, 0, 6, SocketFlags.None, out error);
int result = _tcpSend.Receive(tcpBuffer, 0, data.Length + 5, SocketFlags.None, out error);
}
Array.Copy(tcpBuffer, 5, data, 0, data.Length);
if (error == SocketError.Success)
return data;
else
{
return null;
}
}
private int WriteSingleData(DeviceAddress address, byte[] value)
{
if (_tcpSend == null) return -1;
short ID = (short)address.Start;
byte type = (byte)address.VarType;
byte[] idbits = BitConverter.GetBytes(ID);
byte[] write_data = new byte[6] { FCTCOMMAND.fctHead, FCTCOMMAND.fctWriteSingle, 1, idbits[0], idbits[1], (byte)(value.Length) };
byte[] data = new byte[6 + value.Length];
write_data.CopyTo(data, 0);
value.CopyTo(data, 6);
SocketError error;
lock (sendasync)
{
_tcpSend.Send(data, 0, data.Length, SocketFlags.None, out error);
_tcpSend.Receive(tcpBuffer, 0, 2, SocketFlags.None, out error);
}
if (error == SocketError.Success)
return tcpBuffer[1];//可在此处返回一个错误号
else
{
return (int)error;
}
}
public void Init()
{
if (_items != null && _tcpSend != null)
{
int len = 0;
List<ITag> tags = new List<ITag>();
foreach (var tag in _items.Values)
{
len += (3 + tag.Address.DataSize);
if (len >= tcpBuffer.Length - 10)
{
len = 0;
BatchRead(DataSource.Cache, true, tags.ToArray());
tags.Clear();
}
tags.Add(tag);
}
BatchRead(DataSource.Cache, true, tags.ToArray());
}
}
private void ReciveData()
{
if (!_active || _plcReader.tcpRecive == null) return;
List<HistoryData> historys = null;
byte[] bytes = new byte[ushort.MaxValue];
byte[] temp = new byte[_tcpRecive.ReceiveBufferSize];
Storage value = Storage.Empty;
int result = 0;
int start = 0;
SocketError error;
do
{
if (!_tcpRecive.Connected) return;
result = _tcpRecive.Receive(bytes, 0, bytes.Length, SocketFlags.None, out error);
if (error == SocketError.Success)
{
if (DataChange != null)
historys = new List<HistoryData>();
//DateTime time = DateTime.Now;//当前时间戳
if (start != 0 && temp[0] == FCTCOMMAND.fctHead)
{
int j = 3;
if (start < 0)
{
Array.Copy(bytes, 0, temp, -start, 5 + start);
}
short tc = BitConverter.ToInt16(temp, j);//总字节数
if (start < 0)
start += tc;
Array.Copy(bytes, 0, temp, tc - start, start);
j += 2;
while (j < tc)
{
short id = BitConverter.ToInt16(temp, j);//标签ID、数据长度、数据值(T,L,V)
j += 2;
byte length = temp[j++];
ITag tag;
if (_items.TryGetValue(id, out tag))
{
//数据类型
switch (tag.Address.VarType)
{
case DataType.BOOL:
value.Boolean = BitConverter.ToBoolean(temp, j);
break;
case DataType.BYTE:
value.Byte = temp[j];
break;
case DataType.WORD:
value.Word = BitConverter.ToUInt16(temp, j);//需测试
break;
case DataType.SHORT:
value.Int16 = BitConverter.ToInt16(temp, j);//需测试
break;
case DataType.DWORD:
value.DWord = BitConverter.ToUInt32(temp, j);//需测试
break;
case DataType.INT:
value.Int32 = BitConverter.ToInt32(temp, j);//需测试
break;
case DataType.FLOAT:
value.Single = BitConverter.ToSingle(temp, j);
break;
case DataType.STR:
StringTag strTag = tag as StringTag;
if (strTag != null)
{
strTag.String = Encoding.ASCII.GetString(temp, j, length).Trim((char)0);
}
break;
default:
break;
}
j += length;
DateTime time = DateTime.FromFileTime(BitConverter.ToInt64(temp, j));
j += 8;
tag.Update(value, time, QUALITIES.QUALITY_GOOD);
if (historys != null)
historys.Add(new HistoryData(id, QUALITIES.QUALITY_GOOD, value, time));
}
else
j += length + 8;
}
}
byte head = bytes[start];
int count = start;
while (head == FCTCOMMAND.fctHead && result > count)
{
if (count + 5 > bytes.Length)
{
start = count - bytes.Length;
Array.Copy(bytes, count, temp, 0, -start);
break;
}
int j = count + 3;
short tc = BitConverter.ToInt16(bytes, j);//总标签数
count += tc;
if (count >= bytes.Length)
{
start = count - bytes.Length;
Array.Copy(bytes, count - tc, temp, 0, tc - start);
break;
}
else start = 0;
j += 2;
while (j < count)
{
short id = BitConverter.ToInt16(bytes, j);//标签ID、数据长度、数据值(T,L,V)
j += 2;
byte length = bytes[j++];
ITag tag;
if (_items.TryGetValue(id, out tag))
{
//数据类型
switch (tag.Address.VarType)
{
case DataType.BOOL:
value.Boolean = BitConverter.ToBoolean(bytes, j);
break;
case DataType.BYTE:
value.Byte = bytes[j];
break;
case DataType.WORD:
value.Word = BitConverter.ToUInt16(bytes, j);//需测试
break;
case DataType.SHORT:
value.Int16 = BitConverter.ToInt16(bytes, j);//需测试
break;
case DataType.DWORD:
value.DWord = BitConverter.ToUInt32(bytes, j);//需测试
break;
case DataType.INT:
value.Int32 = BitConverter.ToInt32(bytes, j);//需测试
break;
case DataType.FLOAT:
value.Single = BitConverter.ToSingle(bytes, j);
break;
case DataType.STR:
StringTag strTag = tag as StringTag;
if (strTag != null)
{
strTag.String = Encoding.ASCII.GetString(bytes, j, length).Trim((char)0);
}
break;
default:
break;
}
j += length;
DateTime time = DateTime.FromFileTime(BitConverter.ToInt64(bytes, j));
j += 8;
tag.Update(value, time, QUALITIES.QUALITY_GOOD);
if (historys != null)
historys.Add(new HistoryData(id, QUALITIES.QUALITY_GOOD, value, time));
}
else
j += length + 8;
}
head = bytes[count];
}
if (DataChange != null && historys.Count > 0)
DataChange(this, new DataChangeEventArgs(1, historys));
}
else if (error == SocketError.ConnectionReset || error == SocketError.Interrupted
|| error == SocketError.HostDown || error == SocketError.NetworkDown || error == SocketError.Shutdown)
{
_tcpRecive.Dispose();
_active = false;
return;
}
}
while (result > 0);
}
public void OnUpdate(object stateInfo)
{
while (true)
{
Thread.Sleep(_updateRate);
lock (this)
{
if (!_active)
{
return;
}
Init();
}
}
}
public bool AddItems(IList<TagMetaData> items)
{
int count = items.Count;
if (_items == null) _items = new Dictionary<short, ITag>(count);
lock (_server.SyncRoot)
{
for (int i = 0; i < count; i++)
{
ITag dataItem = null;
TagMetaData meta = items[i];
DeviceAddress addr = new DeviceAddress(0, 0, 0, meta.ID, meta.Size, 0, meta.DataType);
switch (meta.DataType)
{
case DataType.BOOL:
dataItem = new BoolTag(meta.ID, addr, this);
break;
case DataType.BYTE:
dataItem = new ByteTag(meta.ID, addr, this);
break;
case DataType.WORD:
dataItem = new UShortTag(meta.ID, addr, this);
break;
case DataType.SHORT:
dataItem = new ShortTag(meta.ID, addr, this);
break;
case DataType.INT:
dataItem = new IntTag(meta.ID, addr, this);
break;
case DataType.DWORD:
dataItem = new UIntTag(meta.ID, addr, this);
break;
case DataType.FLOAT:
dataItem = new FloatTag(meta.ID, addr, this);
break;
case DataType.STR:
dataItem = new StringTag(meta.ID, addr, this);
break;
default:
dataItem = new BoolTag(meta.ID, addr, this);
break;
}
_items.Add(meta.ID, dataItem);
_server.AddItemIndex(meta.Name, dataItem);
}
}
//Init();
return true;
}
public bool AddTags(IEnumerable<ITag> tags)
{
if (_items == null)
{
_items = new Dictionary<short, ITag>();
}
foreach (ITag tag in tags)
{
if (tag != null)
{
_items.Add(tag.ID, tag);
}
}
//Init();
return true;
}
public bool RemoveItems(params ITag[] items)
{
foreach (var item in items)
{
_server.RemoveItemIndex(item.GetTagName());
_items.Remove(item.ID);
}
return true;
}
public bool SetActiveState(bool active, params short[] items)
{
return true;
}
public ITag FindItemByAddress(DeviceAddress addr)
{
ITag tag;
if (_items.TryGetValue((short)addr.Start, out tag))
return tag;
return _server[(short)addr.Start];
}
public HistoryData[] BatchRead(DataSource source, bool isSync, params ITag[] itemArray)
{
if (itemArray.Length == 0 || _tcpSend == null || !_tcpSend.Connected) return null;
short len = (short)itemArray.Length;
byte[] bt = new byte[2];
byte[] data = new byte[5 + len * 2];
int j = 0;
data[j++] = FCTCOMMAND.fctHead;
data[j++] = FCTCOMMAND.fctReadMultiple;
data[j++] = source == DataSource.Cache ? (byte)0 : (byte)1;
bt = BitConverter.GetBytes(len);
data[j++] = bt[0];
data[j++] = bt[1];
for (int i = 0; i < len; i++)
{
bt = BitConverter.GetBytes(itemArray[i].ID);
data[j++] = bt[0];
data[j++] = bt[1];
}
SocketError error;
lock (sendasync)
{
_tcpSend.Send(data, 0, data.Length, SocketFlags.None, out error);
int result = _tcpSend.Receive(tcpBuffer, 0, tcpBuffer.Length, SocketFlags.None, out error);
}
//!!!!此处的协议应注意,如批量读入的数据量超过缓存,必须分批合并;在协议头应加入总字节数,以和result比较是否需要循环读入。
j = 5;
if (error == SocketError.Success)
{
DateTime time = DateTime.Now;
HistoryData[] values = new HistoryData[len];
for (int i = 0; i < len; i++)
{
short id = BitConverter.ToInt16(tcpBuffer, j);
j += 2;
ITag tag = _server[id];
byte length = tcpBuffer[j++];
if (tag != null && length > 0)
{
switch (tag.Address.VarType)
{
case DataType.BOOL:
values[i].Value.Boolean = BitConverter.ToBoolean(tcpBuffer, j);
break;
case DataType.BYTE:
values[i].Value.Byte = tcpBuffer[j];
break;
case DataType.WORD:
values[i].Value.Word = BitConverter.ToUInt16(tcpBuffer, j);
break;
case DataType.SHORT:
values[i].Value.Int16 = BitConverter.ToInt16(tcpBuffer, j);
break;
case DataType.DWORD:
values[i].Value.DWord = BitConverter.ToUInt32(tcpBuffer, j);
break;
case DataType.INT:
values[i].Value.Int32 = BitConverter.ToInt32(tcpBuffer, j);
break;
case DataType.FLOAT:
values[i].Value.Single = BitConverter.ToSingle(tcpBuffer, j);
break;
case DataType.STR:
StringTag strTag = tag as StringTag;
if (strTag != null)
{
strTag.String = Encoding.ASCII.GetString(tcpBuffer, j, length).Trim();
}
break;
}
if (values[i].Value != tag.Value)
tag.Update(values[i].Value, time, QUALITIES.QUALITY_GOOD);
}
j += length;
}
return values;
}
else
return null;
}
public int BatchWrite(SortedDictionary<ITag, object> items, bool isSync = true)
{
if (_tcpSend == null || !_tcpSend.Connected) return -1;
List<byte> list = new List<byte>(new byte[] { FCTCOMMAND.fctHead, FCTCOMMAND.fctWriteMultiple });
list.AddRange(BitConverter.GetBytes((short)items.Count));
foreach (var item in items)
{
ITag tag = item.Key;
list.AddRange(BitConverter.GetBytes(tag.ID));
var addr = tag.Address;
if (addr.VarType != DataType.STR)
list.Add((byte)(addr.DataSize));//此处存疑
switch (addr.VarType)
{
case DataType.BOOL:
list.Add(Convert.ToBoolean(item.Value) ? (byte)1 : (byte)0);
break;
case DataType.BYTE:
list.Add(Convert.ToByte(item.Value));
break;
case DataType.WORD:
list.AddRange(BitConverter.GetBytes(Convert.ToUInt16(item.Value)));
break;
case DataType.SHORT:
list.AddRange(BitConverter.GetBytes(Convert.ToInt16(item.Value)));
break;
case DataType.DWORD:
list.AddRange(BitConverter.GetBytes(Convert.ToUInt32(item.Value)));
break;
case DataType.INT:
list.AddRange(BitConverter.GetBytes(Convert.ToInt32(item.Value)));
break;
case DataType.FLOAT:
list.AddRange(BitConverter.GetBytes(Convert.ToSingle(item.Value)));
break;
case DataType.STR:
var bts = Encoding.ASCII.GetBytes(Convert.ToString(item.Value));
list.Add((byte)bts.Length);
list.AddRange(bts);
break;
}
}
SocketError error;
lock (sendasync)
{
_tcpSend.Send(list.ToArray(), 0, list.Count, SocketFlags.None, out error);
_tcpSend.Receive(tcpBuffer, 0, 2, SocketFlags.None, out error);
}
if (error == SocketError.Success)
return tcpBuffer[1];
else
{
return (int)error;
}
}
public IEnumerable<HistoryData> SendHdaRequest(DateTime start, DateTime end)
{
if (_tcpSend == null || !_tcpSend.Connected) yield break;
byte[] hdaReq = new byte[18];
hdaReq[0] = FCTCOMMAND.fctHead;
hdaReq[1] = FCTCOMMAND.fctHdaRequest;
byte[] startbuffer = BitConverter.GetBytes(start.ToFileTime());
startbuffer.CopyTo(hdaReq, 2);
byte[] endbuffer = BitConverter.GetBytes(end.ToFileTime());
endbuffer.CopyTo(hdaReq, 10);
SocketError error;
HistoryData data = HistoryData.Empty;
short tempid = short.MinValue;
byte[] temp = new byte[14];
ITag tag = null;
int index = 0;
int size = 0;
int result = 0;
short id = 0;
lock (sendasync)
{
_tcpSend.Send(hdaReq);
do
{
result = _tcpSend.Receive(tcpBuffer, 0, tcpBuffer.Length, SocketFlags.None, out error);
if (error == SocketError.ConnectionReset || error == SocketError.Interrupted || error == SocketError.HostDown || error == SocketError.NetworkDown || error == SocketError.Shutdown)
{
_tcpSend.Dispose();
yield break;
}
if (index != 0)
{
Array.Copy(tcpBuffer, 0, temp, 14 - index, index);
id = BitConverter.ToInt16(temp, 0);
tempid = id;
tag = _server[id];
if (tag == null) yield break;
data.ID = id;
size = tag.Address.DataSize;
index -= (4 - size);
switch (tag.Address.VarType)
{
case DataType.BOOL:
data.Value.Boolean = BitConverter.ToBoolean(temp, 2);
break;
case DataType.BYTE:
data.Value.Byte = tcpBuffer[index];
break;
case DataType.WORD:
data.Value.Word = BitConverter.ToUInt16(temp, 2);
break;
case DataType.SHORT:
data.Value.Int16 = BitConverter.ToInt16(temp, 2);
break;
case DataType.DWORD:
data.Value.DWord = BitConverter.ToUInt32(temp, 2);
break;
case DataType.INT:
data.Value.Int32 = BitConverter.ToInt32(temp, 2);
break;
case DataType.FLOAT:
data.Value.Single = BitConverter.ToSingle(temp, 2);
break;
}
long fileTime = BitConverter.ToInt64(temp, 2 + size);
if (fileTime == -1) yield break;
data.TimeStamp = DateTime.FromFileTime(fileTime);
yield return data;
}
while (result >= index + 2)
{
id = BitConverter.ToInt16(tcpBuffer, index);
if (tempid != id)
{
tempid = id;
tag = _server[id];
if (tag == null) yield break;
size = tag.Address.DataSize;
}
if (index + 10 + size > result)
break;
data.ID = id;
index += 2;
switch (tag.Address.VarType)
{
case DataType.BOOL:
data.Value.Boolean = BitConverter.ToBoolean(tcpBuffer, index);
break;
case DataType.BYTE:
data.Value.Byte = tcpBuffer[index];
break;
case DataType.WORD:
data.Value.Word = BitConverter.ToUInt16(tcpBuffer, index);
break;
case DataType.SHORT:
data.Value.Int16 = BitConverter.ToInt16(tcpBuffer, index);
break;
case DataType.DWORD:
data.Value.DWord = BitConverter.ToUInt32(tcpBuffer, index);
break;
case DataType.INT:
data.Value.Int32 = BitConverter.ToInt32(tcpBuffer, index);
break;
case DataType.FLOAT:
data.Value.Single = BitConverter.ToSingle(tcpBuffer, index);
break;
}
index += size;
long fileTime = BitConverter.ToInt64(tcpBuffer, index);
if (fileTime == -1) yield break;
data.TimeStamp = DateTime.FromFileTime(fileTime);
index += 8;
yield return data;
}
if (index == result)
index = 0;
else
{
Array.Copy(tcpBuffer, index, temp, 0, result - index);
index += 14 - result;
}
} while (result > 0);
}
yield break;
}
public IEnumerable<HistoryData> SendHdaRequest(DateTime start, DateTime end, short id)
{
if (_tcpSend == null || !_tcpSend.Connected) yield break;
ITag tag = _server[id];
if (tag == null) yield break;
byte[] hdaReq = new byte[20];
hdaReq[0] = FCTCOMMAND.fctHead;
hdaReq[1] = FCTCOMMAND.fctHdaIdRequest;
byte[] startbuffer = BitConverter.GetBytes(start.ToFileTime());
startbuffer.CopyTo(hdaReq, 2);
byte[] endbuffer = BitConverter.GetBytes(end.ToFileTime());
endbuffer.CopyTo(hdaReq, 10);
byte[] idbuffer = BitConverter.GetBytes(id);
idbuffer.CopyTo(hdaReq, 18);
SocketError error;
int index = 0;
HistoryData data = HistoryData.Empty;
data.ID = id;
int result = 0;
lock (sendasync)
{
_tcpSend.Send(hdaReq);
switch (tag.Address.VarType)
{
case DataType.FLOAT:
do
{
result = _tcpSend.Receive(tcpBuffer, 0, tcpBuffer.Length, SocketFlags.None, out error);
if (error == SocketError.ConnectionReset || error == SocketError.Interrupted || error == SocketError.HostDown || error == SocketError.NetworkDown || error == SocketError.Shutdown)
{
_tcpSend.Dispose();
yield break;
}
while (index + 12 <= result)
{
data.Value.Single = BitConverter.ToSingle(tcpBuffer, index);//未来可考虑量程转换和其他数据类型
index += 4;
long fileTime = BitConverter.ToInt64(tcpBuffer, index);
if (fileTime == -1) yield break;
data.TimeStamp = DateTime.FromFileTime(fileTime);
index += 8;
yield return data;
}
if (index == result)
index = 0;
else
index += 12 - result;//丢弃一个值
} while (result > 0);
break;
case DataType.SHORT:
do
{
result = _tcpSend.Receive(tcpBuffer, 0, tcpBuffer.Length, SocketFlags.None, out error);
if (error == SocketError.ConnectionReset || error == SocketError.Interrupted || error == SocketError.HostDown || error == SocketError.NetworkDown || error == SocketError.Shutdown)
{
_tcpSend.Dispose();
yield break;
}
while (index + 10 <= result)
{
data.Value.Int16 = BitConverter.ToInt16(tcpBuffer, index);//未来可考虑量程转换和其他数据类型
index += 2;
long fileTime = BitConverter.ToInt64(tcpBuffer, index);
if (fileTime == -1) yield break;
data.TimeStamp = DateTime.FromFileTime(fileTime);
index += 8;
yield return data;
}
if (index == result)
index = 0;
else
index += 10 - result;//丢弃一个值
} while (result > 0);
break;
case DataType.WORD:
do
{
result = _tcpSend.Receive(tcpBuffer, 0, tcpBuffer.Length, SocketFlags.None, out error);
if (error == SocketError.ConnectionReset || error == SocketError.Interrupted || error == SocketError.HostDown || error == SocketError.NetworkDown || error == SocketError.Shutdown)
{
_tcpSend.Dispose();
yield break;
}
while (index + 10 <= result)
{
data.Value.Word = BitConverter.ToUInt16(tcpBuffer, index);//未来可考虑量程转换和其他数据类型
index += 2;
long fileTime = BitConverter.ToInt64(tcpBuffer, index);
if (fileTime == -1) yield break;
data.TimeStamp = DateTime.FromFileTime(fileTime);
index += 8;
yield return data;
}
if (index == result)
index = 0;
else
index += 10 - result;//丢弃一个值
} while (result > 0);
break;
case DataType.DWORD:
do
{
result = _tcpSend.Receive(tcpBuffer, 0, tcpBuffer.Length, SocketFlags.None, out error);
if (error == SocketError.ConnectionReset || error == SocketError.Interrupted || error == SocketError.HostDown || error == SocketError.NetworkDown || error == SocketError.Shutdown)
{
_tcpSend.Dispose();
yield break;
}
while (index + 12 <= result)
{
data.Value.DWord = BitConverter.ToUInt32(tcpBuffer, index);//未来可考虑量程转换和其他数据类型
index += 4;
long fileTime = BitConverter.ToInt64(tcpBuffer, index);
if (fileTime == -1) yield break;
data.TimeStamp = DateTime.FromFileTime(fileTime);
index += 8;
yield return data;
}
if (index == result)
index = 0;
else
index += 12 - result;//丢弃一个值
} while (result > 0);
break;
case DataType.INT:
do
{
result = _tcpSend.Receive(tcpBuffer, 0, tcpBuffer.Length, SocketFlags.None, out error);
if (error == SocketError.ConnectionReset || error == SocketError.Interrupted || error == SocketError.HostDown || error == SocketError.NetworkDown || error == SocketError.Shutdown)
{
_tcpSend.Dispose();
yield break;
}
while (index + 12 <= result)
{
data.Value.Int32 = BitConverter.ToInt32(tcpBuffer, index);//未来可考虑量程转换和其他数据类型
index += 4;
long fileTime = BitConverter.ToInt64(tcpBuffer, index);
if (fileTime == -1) yield break;
data.TimeStamp = DateTime.FromFileTime(fileTime);
index += 8;
yield return data;
}
if (index == result)
index = 0;
else
index += 12 - result;//丢弃一个值
} while (result > 0);
break;
case DataType.BOOL://布尔量一个都不能少
{
byte[] temp = new byte[9];
do
{
result = _tcpSend.Receive(tcpBuffer, 0, tcpBuffer.Length, SocketFlags.None, out error);
if (error == SocketError.ConnectionReset || error == SocketError.Interrupted || error == SocketError.HostDown || error == SocketError.NetworkDown || error == SocketError.Shutdown)
{
_tcpSend.Dispose();
yield break;
}
if (index != 0)
{
Array.Copy(tcpBuffer, 0, temp, 9 - index, index);
data.Value.Boolean = BitConverter.ToBoolean(temp, 0);
long fileTime = BitConverter.ToInt64(temp, 1);
if (fileTime == -1) yield break;
data.TimeStamp = DateTime.FromFileTime(fileTime);
yield return data;
}
while (index + 9 <= result)
{
data.Value.Boolean = BitConverter.ToBoolean(tcpBuffer, index);
index += 1;
long fileTime = BitConverter.ToInt64(tcpBuffer, index);
if (fileTime == -1) yield break;
data.TimeStamp = DateTime.FromFileTime(fileTime);
index += 8;
yield return data;
}
if (index == result)
index = 0;
else
{
Array.Copy(tcpBuffer, index, temp, 0, result - index);
index += 9 - result;
}
} while (result > 0);
}
break;
}
}
}
public int SendResetRequest()
{
if (_tcpSend != null && _tcpSend.Connected)
{
var ipaddr = (_tcpSend.LocalEndPoint as IPEndPoint).Address;
byte[] resetReq = new byte[6];
resetReq[0] = FCTCOMMAND.fctHead;
resetReq[1] = FCTCOMMAND.fctReset;
ipaddr.GetAddressBytes().CopyTo(resetReq, 2);
lock (sendasync)
{
return _tcpSend.Send(resetReq);
}
}
return -1;
}
public int SendAlarmRequest(DateTime? start, DateTime? end)
{
if (_tcpSend != null && _tcpSend.Connected)
{
byte[] alarmReq = new byte[18];
alarmReq[0] = FCTCOMMAND.fctHead;
alarmReq[1] = FCTCOMMAND.fctAlarmRequest;
if (start.HasValue)
{
byte[] startbuffer = BitConverter.GetBytes(start.Value.ToFileTime());
startbuffer.CopyTo(alarmReq, 2);
}
if (end.HasValue)
{
byte[] endbuffer = BitConverter.GetBytes(end.Value.ToFileTime());
endbuffer.CopyTo(alarmReq, 10);
}
SocketError error;
lock (sendasync)
{
_tcpSend.Send(alarmReq);
_tcpSend.Receive(tcpBuffer, 0, 2, SocketFlags.None, out error);
}
return (int)error;
}
return -1;
}
public ItemData<int> ReadInt32(DeviceAddress address, DataSource source = DataSource.Cache)
{
var data = ReadSingleData(address, source);
return data == null ? new ItemData<int>(0, 0, QUALITIES.QUALITY_BAD) :
new ItemData<int>(BitConverter.ToInt32(data, 0), 0, QUALITIES.QUALITY_GOOD);
}
public ItemData<uint> ReadUInt32(DeviceAddress address, DataSource source = DataSource.Cache)
{
var data = ReadSingleData(address, source);
return data == null ? new ItemData<uint>(0, 0, QUALITIES.QUALITY_BAD) :
new ItemData<uint>(BitConverter.ToUInt32(data, 0), 0, QUALITIES.QUALITY_GOOD);
}
public ItemData<ushort> ReadUInt16(DeviceAddress address, DataSource source = DataSource.Cache)
{
var data = ReadSingleData(address, source);
return data == null ? new ItemData<ushort>(0, 0, QUALITIES.QUALITY_BAD) :
new ItemData<ushort>(BitConverter.ToUInt16(data, 0), 0, QUALITIES.QUALITY_GOOD);
}
public ItemData<short> ReadInt16(DeviceAddress address, DataSource source = DataSource.Cache)
{
var data = ReadSingleData(address, source);
return data == null ? new ItemData<short>(0, 0, QUALITIES.QUALITY_BAD) :
new ItemData<short>(BitConverter.ToInt16(data, 0), 0, QUALITIES.QUALITY_GOOD);
}
public ItemData<byte> ReadByte(DeviceAddress address, DataSource source = DataSource.Cache)
{
var data = ReadSingleData(address, source);
return data == null ? new ItemData<byte>(0, 0, QUALITIES.QUALITY_BAD) :
new ItemData<byte>(data[0], 0, QUALITIES.QUALITY_GOOD);
}
public unsafe ItemData<float> ReadFloat(DeviceAddress address, DataSource source = DataSource.Cache)
{
var data = ReadSingleData(address, source);
if (data == null)
return new ItemData<float>(0.0f, 0, QUALITIES.QUALITY_BAD);
else
{
int value = BitConverter.ToInt32(data, 0);
return new ItemData<float>(*(((float*)&value)), 0, QUALITIES.QUALITY_GOOD);
}
}
public ItemData<bool> ReadBool(DeviceAddress address, DataSource source = DataSource.Cache)
{
var data = ReadSingleData(address, source);
return data == null ? new ItemData<bool>(false, 0, QUALITIES.QUALITY_BAD) :
new ItemData<bool>(BitConverter.ToBoolean(data, address.Bit), 0, QUALITIES.QUALITY_GOOD);
}
public ItemData<string> ReadString(DeviceAddress address, DataSource source = DataSource.Cache)
{
var data = ReadSingleData(address, source);
return data == null ? new ItemData<string>(string.Empty, 0, QUALITIES.QUALITY_BAD) :
new ItemData<string>(Encoding.ASCII.GetString(data, 0, Math.Min((int)address.DataSize, 254)).Trim((char)0), 0, QUALITIES.QUALITY_GOOD);
}
public int WriteInt32(DeviceAddress address, int value)
{
return WriteSingleData(address, BitConverter.GetBytes(value));
}
public int WriteUInt32(DeviceAddress address, uint value)
{
return WriteSingleData(address, BitConverter.GetBytes(value));
}
public int WriteUInt16(DeviceAddress address, ushort value)
{
return WriteSingleData(address, BitConverter.GetBytes(value));
}
public int WriteInt16(DeviceAddress address, short value)
{
return WriteSingleData(address, BitConverter.GetBytes(value));
}
public int WriteFloat(DeviceAddress address, float value)
{
return WriteSingleData(address, BitConverter.GetBytes(value));
}
public int WriteString(DeviceAddress address, string value)
{
return WriteSingleData(address, Encoding.ASCII.GetBytes(value));
}
public int WriteBit(DeviceAddress address, bool value)
{
return WriteSingleData(address, new byte[] { (byte)(value ? 1 : 0) });
}
public int WriteBits(DeviceAddress address, byte value)
{
return WriteSingleData(address, new byte[] { value });
}
public event DataChangeEventHandler DataChange;
public void Dispose()
{
if (_items != null)
{
_items.Clear();
}
_items = null;
}
}
}
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

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

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

取消
提交

简介

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

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/zl_java/SharpSCADA.git
git@gitee.com:zl_java/SharpSCADA.git
zl_java
SharpSCADA
SharpSCADA
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

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