private dynamic ReadArchive(ArchiveType archiveType, TimeSpan d, DateTime start, DateTime end, DateTime currentDt) { dynamic archive = new ExpandoObject(); archive.success = true; archive.error = string.Empty; archive.errorcode = DeviceError.NO_ERROR; var allRecs = new List <dynamic>(); string type = archiveType == ArchiveType.Hourly ? "Hour" : "Day"; if (T2k == null) { archive.error = "не удалось прочесть память таймера 2к"; archive.success = false; return(archive); } log($"Чтение {archiveType} архива"); for (var date = start.Date.AddHours(start.Hour); date <= end; date.Add(d)) { List <dynamic> recs = new List <dynamic>(); if (cancel()) { archive.success = false; archive.error = "опрос отменен"; break; } if (date >= currentDt) { log(string.Format("данные за {0:dd.MM.yyyy HH:mm} еще не собраны", date)); break; } log($"Запрос даты {date:HH:mm dd.MM.yyyy}"); var answer = Send(MakeBaseRequest(0x0d11, new byte[] { (byte)archiveType, archiveType == ArchiveType.Hourly? IntToBCD(date.Hour):(byte)0x00, archiveType != ArchiveType.Monthly? IntToBCD(date.Day):(byte)0x01, IntToBCD(date.Month), IntToBCD(date.Year - 2000) })); if (!answer.success) { return(answer); } byte[] body = answer.Body as byte[]; if (body.Length == 3) { var num = body[1] << 8 | body[2]; if (num == 0xFFFF) { log("запись не обнаружена"); } else { log($"номер записи: {num} (0x{num:X4})"); //Int64 addr = 0; //var answer0 = SendRequest(0x0f03, new byte[] { 64, (byte)(addr >> 24), (byte)(addr >> 16), (byte)(addr >> 08), (byte)(addr) }); //answer = new byte[256]; List <byte> bytes = new List <byte>(); for (int i = 0; i < 4; i++) { var curanswer = Send(MakeBaseRequest(0x0f03, new byte[] { 64, 0x00, ConvertHelper.ByteHigh(num), ConvertHelper.ByteLow(num) /*answer[1], answer[2]*/, (byte)(i * 0x40) }), 3333, 5); if (!curanswer.success || (curanswer.Body as byte[]).Length != 65) { log("не удалось запросить запись, пропуск"); return(curanswer); } bytes.AddRange((curanswer.Body as byte[]).Skip(1)); } var sysInt = SysInt.Parse(bytes.ToArray(), 0); if (T2k.Systems < 1 || T2k.Systems > 4) { log($"Некорректное число систем: {T2k.Systems}"); answer.error = ""; answer.success = false; return(answer); } recs.Add(MakeDayOrHourRecord(archiveType == ArchiveType.Hourly ? "Hour" : "Day", sysInt.Trab.Parameter, sysInt.Trab.Value[0], sysInt.Trab.MeasuringUnit, sysInt.date)); for (int sys = 0; sys < T2k.Systems; sys++) { var systype = T2k.SysConN[sys].sysType; recs.Add(MakeDayOrHourRecord(type, sysInt.IntV.Parameter, sysInt.IntV.Value[sys], sysInt.IntV.MeasuringUnit, sysInt.date)); recs.Add(MakeDayOrHourRecord(type, sysInt.IntM.Parameter, sysInt.IntM.Value[sys], sysInt.IntM.MeasuringUnit, sysInt.date)); recs.Add(MakeDayOrHourRecord(type, sysInt.IntQ.Parameter, sysInt.IntQ.Value[sys], sysInt.IntQ.MeasuringUnit, sysInt.date)); recs.Add(MakeDayOrHourRecord(type, sysInt.Tnar.Parameter, sysInt.Tnar.Value[sys], sysInt.Tnar.MeasuringUnit, sysInt.date)); for (int i = 0; i < SysCon.GetChannelsPorT(systype); i++) { recs.Add(MakeDayOrHourRecord(type, sysInt.T.Parameter, sysInt.T.Value[sys * 3 + i], sysInt.T.MeasuringUnit, sysInt.date)); recs.Add(MakeDayOrHourRecord(type, sysInt.P.Parameter, sysInt.P.Value[sys * 3 + i], sysInt.P.MeasuringUnit, sysInt.date)); } recs.Add(MakeDayOrHourRecord(type, sysInt.Rshv.Parameter, sysInt.Rshv.Value[sys], sysInt.Rshv.MeasuringUnit, sysInt.date)); } allRecs.AddRange(recs); records(recs); } } else { log("ответ не получен"); } } archive.records = allRecs; return(archive); }
private T2K T2KRead() { var answer = new byte[2048]; for (int i = 0; i < 32; i++) { var curAddr = i * 0x40; var cmd = 0x0f01; var curanswer = Send(MakeBaseRequest(cmd, new byte[] { ConvertHelper.ByteHigh(curAddr), ConvertHelper.ByteLow(curAddr), 0x40 }), cmd); if (!curanswer.success || (curanswer.Body as byte[]).Length != 65) { return(null); } Array.Copy((curanswer.Body as byte[]), 1, answer, 64 * i, 64); } return(T2K.Parse(answer, 0)); }
private byte[] MakeBaseRequest(int cmd, byte[] data = null) { var bytes = new List <byte>() { 0x55, NetworkAddress, (byte)(~NetworkAddress), ConvertHelper.ByteHigh(cmd), ConvertHelper.ByteLow(cmd), 0 }; if (data != null && data.Length > 0) { bytes.Add((byte)data.Length); bytes.AddRange(data); } bytes.Add(CheckSum(bytes.ToArray(), bytes.Count, ChecksumType.Complement1)); return(bytes.ToArray()); }