/// <summary> /// Retrieves Phase Diagnostic for MC5N meter /// </summary> /// <param name="token">Cancellation token that is polled to see if the user wishes to exit this function</param> /// <returns>Phase Diagnostic for meter in string format</returns> public async Task <string> PhaseDiagnostic(CancellationToken token) { // Reads serial number after each PhaseDiagnostic read to determine if // still attached to the same meter await WriteToSerial("attn -d"); await ReadBuffer(true); string newSerial = ""; if (string.IsNullOrEmpty(SerialBuffer)) { string oldSerialNo = SerialNo; if (await Login()) { if (oldSerialNo != SerialNo) { return("switch"); } else { newSerial = SerialNo; } } else { return("failed"); } } else { foreach (string line in SerialBuffer.Split(new char[1] { '\n' })) { if (Regex.IsMatch(line.TrimEnd('\r'), @"^[0-9 ]+$")) { newSerial = line.Split(new char[0])[0]; break; } } } if (string.IsNullOrEmpty(newSerial)) { Console.WriteLine("Unable to get meter serial number"); return("failings"); } // New Serial number so propagate message up to switch meter else if (SerialNo != newSerial) { SerialNo = newSerial; return("switch"); } // Initiates phase diagnostic command and waits for response if (NewFirmware) { string totalBuffer = ""; foreach (int i in System.Linq.Enumerable.Range(0, 12)) { await WriteToSerial(String.Format("md -p -#2 -@{0}", i + 32)); if (!(await ReadBuffer(true, token))) { if (string.IsNullOrEmpty(SerialBuffer)) { return("failed"); } else { return(""); } } totalBuffer += SerialBuffer; } // Set value of buffer only if correct number of lines were returned if (totalBuffer.Count(c => c == '\n') == 72) { SerialBuffer = totalBuffer; } else { Console.WriteLine("Corrupt data received from serial port"); return("invalid"); } } else { await WriteToSerial("mscan -Gp"); // Store serial contents in buffer if (!(await ReadBuffer(true, token))) { if (string.IsNullOrEmpty(SerialBuffer)) { return("failed"); } else { return(""); } } } #if DEBUG Console.Write(SerialBuffer + ' '); #endif return(SerialBuffer); }
public void Init(int[] channelData, string config) { Buffer = new SerialBuffer(serialPort); this.channelData = channelData; this.config = config; }
/// <summary> /// Logins into the meter head for a MC5N meter using pre supplied passwords /// </summary> /// <returns>Boolean indicating success of login attempt</returns> public async Task <bool> Login() { // Makes 10 attempts to get serial number of currently connected meter int attempts = 0; carriageReturn = false; while (true) { // Modifies communication method if this is first login attempt // and 2 previous attempts at retrieving serial number failed if (attempts == 2) { carriageReturn = true; } // Exits function 10 unsuccessful attempts else if (attempts == 10) { #if DEBUG Console.WriteLine("Experiencing an issue communicating with meter head."); #endif return(false); } // Writes command to serial to request meter serial number await WriteToSerial("attn -D"); if (await ReadBuffer()) { break; } // If specified number of bytes was not received, throw away anything that was received // increment counter and try again attempts++; } #if DEBUG // Writes buffer to the command line and retrieves serial number Console.WriteLine(SerialBuffer); #endif string[] split = SerialBuffer.Split(new char[0], StringSplitOptions.RemoveEmptyEntries); if (SerialBuffer.Contains("attn")) { SerialNo = split[2]; } else { SerialNo = split[0]; } // Attempts to use all of the stored passwords once to login into the meter // Waits for command prompt if this is a successive attempt to login to a meter // Exits function by returning true if login was successful foreach (string s in pwds) { await WriteToSerial(String.Format("attn -S{0} {1}", SerialNo, s)); await ReadBuffer(true); #if DEBUG Console.Write(SerialBuffer + " "); #endif if (prompt.IsMatch(SerialBuffer)) { return(true); } #if DEBUG Console.Write("\r\n"); #endif } // Login was unsuccessful if this code is reached so return false return(false); }
public static void Main(string[] args) { Console.WriteLine("Hello!"); string path = @"..\..\..\Databases\"; // Мы будем XML-файл 0001.xml превращать в последовательность специально организованных записей PType tp_seqrec = new PTypeSequence(new PTypeRecord( new NamedType("id", new PType(PTypeEnumeration.sstring)), new NamedType("type", new PType(PTypeEnumeration.sstring)), new NamedType("fields", new PTypeSequence(new PTypeRecord( new NamedType("predicate", new PType(PTypeEnumeration.sstring)), new NamedType("data", new PType(PTypeEnumeration.sstring)), new NamedType("lang", new PType(PTypeEnumeration.sstring))))), new NamedType("direct", new PTypeSequence(new PTypeRecord( new NamedType("predicate", new PType(PTypeEnumeration.sstring)), new NamedType("obj", new PType(PTypeEnumeration.sstring))))))); XElement db = XElement.Load(path + "0001.xml"); XName rdfabout = "{http://www.w3.org/1999/02/22-rdf-syntax-ns#}about"; XName rdfresource = "{http://www.w3.org/1999/02/22-rdf-syntax-ns#}resource"; XName xmllang = "{http://www.w3.org/XML/1998/namespace}lang"; //string rdftypestring = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"; var query = db.Elements() .Where(el => el.Attribute(rdfabout) != null); // Для проверки, сделаем объект object[] db_obj = query .Select(el => new object[] { el.Attribute(rdfabout).Value, el.Name.NamespaceName + el.Name.LocalName, el.Elements() .Where(ee => ee.Attribute(rdfresource) == null) .Select(ee => new object[] { ee.Name.NamespaceName + ee.Name.LocalName, ee.Value, ee.Attribute(xmllang) == null? "": ee.Attribute(xmllang).Value }).ToArray(), el.Elements() .Where(ee => ee.Attribute(rdfresource) != null) .Select(ee => new object[] { ee.Name.NamespaceName + ee.Name.LocalName, ee.Attribute(rdfresource).Value }).ToArray() }).ToArray(); Console.WriteLine(db_obj.Length); DateTime tt0 = DateTime.Now; // Создадим ячейку PaCell cell; cell = new PaCell(tp_seqrec, path + "records_fromObject.pac", false); cell.Clear(); cell.Fill(db_obj); cell.Close(); Console.WriteLine("======Fill ok. duration=" + (DateTime.Now - tt0).Ticks / 10000L); tt0 = DateTime.Now; // Это - для приема потока ввода ISerialFlow input; // Теперь будем вводить через поток cell = new PaCell(tp_seqrec, path + "records_fromFlow.pac", false); cell.Clear(); input = cell; input.StartSerialFlow(); input.S(); foreach (var rec in db_obj) { input.V(rec); } input.Se(); input.EndSerialFlow(); cell.Close(); Console.WriteLine("======Fill flow ok. duration=" + (DateTime.Now - tt0).Ticks / 10000L); tt0 = DateTime.Now; // Будем мельчить поток (у меня получилось 4.8 сек.) cell = new PaCell(tp_seqrec, path + "records_fromFlow.pac", false); cell.Clear(); input = cell; input.StartSerialFlow(); input.S(); foreach (object[] rec in db_obj) { input.R(); input.V(rec[0]); input.V(rec[1]); //input.V(rec[2]); { input.S(); foreach (object[] fields in (object[])rec[2]) { input.R(); input.V(fields[0]); input.V(fields[1]); input.V(fields[2]); input.Re(); } input.Se(); } //input.V(rec[3]); { input.S(); foreach (object[] directs in (object[])rec[3]) { input.R(); input.V(directs[0]); input.V(directs[1]); input.Re(); } input.Se(); } input.Re(); } input.Se(); input.EndSerialFlow(); cell.Close(); Console.WriteLine("======Fill small flow ok. duration=" + (DateTime.Now - tt0).Ticks / 10000L); tt0 = DateTime.Now; // Теперь предыдущий мелкий поток пропустим через буфер (у меня получилось 2.2 сек.) cell = new PaCell(tp_seqrec, path + "records_fromFlow.pac", false); cell.Clear(); input = new SerialBuffer(cell, 1); input.StartSerialFlow(); input.S(); foreach (object[] rec in db_obj) { input.R(); input.V(rec[0]); input.V(rec[1]); //input.V(rec[2]); { input.S(); foreach (object[] fields in (object[])rec[2]) { input.R(); input.V(fields[0]); input.V(fields[1]); input.V(fields[2]); input.Re(); } input.Se(); } //input.V(rec[3]); { input.S(); foreach (object[] directs in (object[])rec[3]) { input.R(); input.V(directs[0]); input.V(directs[1]); input.Re(); } input.Se(); } input.Re(); } input.Se(); input.EndSerialFlow(); cell.Close(); Console.WriteLine("======Fill bufferred small flow ok. duration=" + (DateTime.Now - tt0).Ticks / 10000L); tt0 = DateTime.Now; System.IO.File.Delete(path + "records_fromObject.pac"); System.IO.File.Delete(path + "records_fromFlow.pac"); }