| | |
| | | using DocumentFormat.OpenXml.Bibliography; |
| | | using DocumentFormat.OpenXml.Drawing; |
| | | using Elastic.Clients.Elasticsearch; |
| | | using Furion.Logging; |
| | | using IoTClient; |
| | | using IoTClient.Clients.Modbus; |
| | | using IoTClient.Clients.PLC; |
| | |
| | | case PLCDataTypeEnum.String: |
| | | { |
| | | string dpos = IncrementCode(Pos); |
| | | Result<byte[]> result1 = _client.ReadString(Pos, 1); |
| | | Result<byte[]> result1 = _client.ReadString(Pos, 2); |
| | | if (result1.IsSucceed) |
| | | { |
| | | Result<byte[]> result2 = _client.ReadString(dpos, (ushort)(result1.Value[0])); |
| | | //wxw改 |
| | | if ((ushort)(result1.Value[1]) > (ushort)(result1.Value[0])) |
| | | { |
| | | break; |
| | | } |
| | | ushort let = (ushort)(result1.Value[1]);//(ushort)(result1.Value[0]) 备注:result1.Value[0]取的是设置的最大长度,result1.Value[0]取的是实际值长度 |
| | | |
| | | Result<byte[]> result2 = _client.ReadString(dpos, let); |
| | | Result<string> result3 = new Result<string>(result2); |
| | | if (result3.IsSucceed) |
| | | { |
| | | result3.Value = Encoding.ASCII.GetString(result2.Value, 0, result1.Value[0]).Replace("\0", ""); |
| | | result3.Value = Encoding.ASCII.GetString(result2.Value, 0, let).Replace("\0", ""); |
| | | result = result3; |
| | | } |
| | | } |
| | |
| | | } |
| | | } |
| | | /// <summary> |
| | | /// 循环写入PLC值(并读取值判断和写入的值是否一致,写入控制字不能用此方法) |
| | | /// </summary> |
| | | public IoTClient.Result SetPlcDBValueRepeat(PLCDataTypeEnum PosType, string DbNumber, string Pos, string Value) |
| | | { |
| | | IoTClient.Result _result = new IoTClient.Result(); |
| | | |
| | | int setCount = 0;//写入次数 |
| | | while (setCount < 5) |
| | | { |
| | | string address; |
| | | if (DbNumber.StartsWith("DB")) |
| | | address = DbNumber + "." + Pos; |
| | | else |
| | | address = DbNumber + Pos; |
| | | _result = this.SetPlcDBValue(PosType, address, Value);//写入值 |
| | | |
| | | //累计写入次数 |
| | | setCount++; |
| | | |
| | | if (_result.IsSucceed) |
| | | { |
| | | //读取写入的值 |
| | | var (res, val) = GetPlcDBValue(PosType, DbNumber, Pos); |
| | | if (val.ToString() == Value) |
| | | { |
| | | //读取的值和写入的值一致,直接跳出循环 |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | //写入日志 |
| | | //Log.Information($"66666666写入PLC,DbNumber:{DbNumber},Pos:{Pos},Value:{Value},setCount:{setCount}"); |
| | | |
| | | return _result; |
| | | } |
| | | /// <summary> |
| | | /// 写入PLC值 |
| | | /// </summary> |
| | | /// <param name="PosType"></param> |
| | | /// <param name="DbNumber"></param> |
| | | /// <param name="Pos"></param> |
| | | /// <param name="Value"></param> |
| | | /// <returns></returns> |
| | | public IoTClient.Result SetPlcDBValue(PLCDataTypeEnum PosType, string DbNumber, string Pos, string Value) |
| | | { |
| | | //Log.Information($"66666666写入PLC,DbNumber:{DbNumber},Pos:{Pos},Value:{Value}"); |
| | | |
| | | string address; |
| | | if (DbNumber.StartsWith("DB")) |
| | | address = DbNumber + "." + Pos; |
| | |
| | | } |
| | | } |
| | | /// <summary> |
| | | /// 写入PLC值 |
| | | /// 批量写入PLC值 |
| | | /// </summary> |
| | | public IoTClient.Result SetPlcBatchDBValue(Dictionary<string, KeyValuePair<string, PLCDataTypeEnum>> listaddress) |
| | | { |