AddressSection SetCurrentAddress(SerialPort serialPort, LoggerInformation loggerInformation, List <Hex> hexes)
        {
            var length = MaxReadingLength;
            var msg    = ReadBytes(serialPort);

            var timenow = "0000000000000000";
            var time    = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() / 1000;
            var hextime = time.ToString("x02");

            timenow = timenow.Substring(0, (timenow.Length - hextime.Length)) + hextime;
            hexes.Add(new Hex("FF0000", timenow));

            if (msg.Length > 8)
            {
                if (loggerInformation.LoggerType == 1)
                {
                    hexes.Add(new Hex("FE0000", msg.ToString(0, (msg.Length - 6))));
                    return(new AddressSection(0, 0, 1, 0x00, 0x00, 0x00));
                }
                else if (msg.ToString().Substring(0, 4) == "0004")
                {
                    hexes.Add(new Hex("FE0000", msg.ToString(0, (msg.Length - 6))));
                    var memoryAddress = (recievemsg[loggerInformation.MemoryHeaderPointer + 1] & 0xFF) << 8 | (recievemsg[loggerInformation.MemoryHeaderPointer] & 0xFF);
                    var memoryAddMSB  = recievemsg[loggerInformation.MemoryHeaderPointer + 1];
                    var memoryAddLSB  = recievemsg[loggerInformation.MemoryHeaderPointer];
                    var lengthMSB     = (byte)((length >> 8) & 0xFF);
                    var lengthLSB     = (byte)(length & 0xFF);
                    return(new AddressSection(lengthLSB, lengthMSB, 0, memoryAddLSB, memoryAddMSB, memoryAddress));
                }
            }

            return(null);
        }
        bool ReadBytesSetRead(SerialPort serialPort, AddressSection currentAddress, LoggerInformation loggerInformation, List <Hex> hexes)
        {
            var msg = ReadBytes(serialPort);

            lengthToRead = MaxReadingLength;

            if (msg.Equals("System.TimeoutException"))
            {
                return(false);
            }

            if ((recievemsg.Count > 8) && (recievemsg[0] == 0x00))
            {
                switch (loggerInformation.LoggerType)
                {
                //MonT2
                case 1:
                    loggerInformation.MemoryStart[8] = 0x0000;
                    loggerInformation.MemoryMax[8]   = ((recievemsg[13] & 0xFF) << 8 | (recievemsg[12] & 0xFF)) * 2;
                    if (MonT2Overflown)
                    {
                        loggerInformation.MemoryMax[8] = 0x81c0;
                    }
                    break;

                //MonT
                case 3:
                    loggerInformation.MemoryStart[0] = 0x0000;
                    loggerInformation.MemoryMax[0]   = 0x2000;
                    break;

                //G4
                case 6:
                    loggerInformation.MemoryStart[4] = (recievemsg[loggerInformation.RequestMemoryStartPointer + 1]) << 8 | (recievemsg[loggerInformation.RequestMemoryStartPointer]);
                    loggerInformation.MemoryMax[4]   = (recievemsg[loggerInformation.RequestMemoryMaxPointer + 1]) << 8 | (recievemsg[loggerInformation.RequestMemoryMaxPointer]);

                    if (loggerInformation.MemoryStart[4] > loggerInformation.MemoryMax[4])
                    {
                        hexes.Add(new Hex("FD0000", loggerInformation.MemoryStart[4].ToString("x04")));
                        loggerInformation.MemoryStart[4] = 0x0000;
                        loggerInformation.MemoryMax[4]   = 0x8000;
                    }
                    else
                    {
                        hexes.Add(new Hex("FD0000", "0000"));
                    }

                    if (loggerInformation.MemoryMax[4] < 80)
                    {
                        loggerInformation.MemoryMax[4] = 80;
                    }
                    break;
                }

                currentAddress.LengthMSB = (byte)((lengthToRead >> 8) & 0xff);
                currentAddress.LengthLSB = (byte)(lengthToRead & 0xff);
                return(true);
            }
            return(false);
        }
        void SetLoggerInformation(List <byte> messageReceived, LoggerInformation loggerInformation)
        {
            byte[] serial;
            MonT2Overflown = false;

            switch (messageReceived[2])
            {
            case 1:
                loggerInformation.LoggerName  = "Mon T2";
                loggerInformation.LoggerType  = 1;
                loggerInformation.JsonFile    = "MonT2.json";
                loggerInformation.MaxMemory   = 0x08;                                                                                 //MON-T2
                loggerInformation.MemoryStart = new int[] { 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 }; //MON-T2
                loggerInformation.MemoryMax   = new int[] { 0x0000, 0x0074, 0x0200, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x8000 }; //MON-T2

                loggerInformation.SerialNumber = GetMonT2Serialnumber(messageReceived);

                if ((recievemsg[22] >> 3 & 1) == 0)
                {
                    MonT2Overflown = true;
                }
                break;

            case 3:
                loggerInformation.LoggerName                = "Mon T";
                loggerInformation.LoggerType                = 3;
                loggerInformation.JsonFile                  = "MonT.json";
                loggerInformation.MaxMemory                 = 0x02;                                                 //MON-T
                loggerInformation.MemoryHeaderPointer       = 19;                                                   //MON-T
                loggerInformation.MemoryStart               = new int[] { 0x0000, 0x0020, 0x0000, 0x0000, 0x0000 }; //MON-T
                loggerInformation.MemoryMax                 = new int[] { 0x2000, 0x0100, 0x0000, 0x0000, 0x2000 }; //MON-T
                loggerInformation.RequestMemoryStartPointer = 3;
                loggerInformation.RequestMemoryMaxPointer   = 1;

                serial = new byte[] { messageReceived[5], messageReceived[6], messageReceived[7], messageReceived[8] };
                loggerInformation.SerialNumber = GetSerialnumber(serial);

                break;

            case 6:
                loggerInformation.LoggerName                = "G4";
                loggerInformation.LoggerType                = 6;
                loggerInformation.JsonFile                  = "G4.json";
                loggerInformation.MaxMemory                 = 0x04;                                                 //G4
                loggerInformation.MemoryHeaderPointer       = 13;                                                   //G4
                loggerInformation.MemoryStart               = new int[] { 0x0000, 0x0020, 0x0000, 0x0000, 0x0000 }; //G4
                loggerInformation.MemoryMax                 = new int[] { 0x353C, 0x0100, 0x0000, 0x0000, 0x8000 }; //G4
                loggerInformation.RequestMemoryStartPointer = 3;
                loggerInformation.RequestMemoryMaxPointer   = 1;

                serial = new byte[] { messageReceived[5], messageReceived[6], messageReceived[7], messageReceived[8] };
                loggerInformation.SerialNumber = GetSerialnumber(serial);

                break;
            }
        }
예제 #4
0
        private void LogInfo()
        {
            LoggerInformation logInfo = new LoggerInformation();

            logInfo.UserName        = this.Request.LogonUserIdentity.Name;
            logInfo.RequestUrl      = this.Request.Url.AbsoluteUri;
            logInfo.Browser         = this.Request.Browser.Browser;
            logInfo.RequestType     = this.Request.RequestType;
            logInfo.UserHostAddress = this.Request.UserHostAddress;
            logger.RecordLog(logInfo);
        }
        List <Hex> ReadLogger(SerialPort serialPort, LoggerInformation loggerInformation, List <Hex> Hexes)
        {
            var isReaderConnected = true;
            var isSuccessfulRead  = false;

            if (SerialPort.GetPortNames().Contains(serialPort.PortName) == false || serialPort.IsOpen == false)
            {
                serialPort.Open();
            }

            recoverCount = 0;

            //wake up the logger
            WriteBytes(new WakeUpByteWritter(), serialPort);
            var currentAddress = ReadBytesWakeUp(serialPort, loggerInformation, recievemsg, Hexes);

            //Set up logger information
            isReaderConnected = WriteBytes(new SetReadByteWritter(loggerInformation.LoggerType), serialPort);
            if (isReaderConnected)
            {
                isSuccessfulRead = ReadBytesSetRead(serialPort, currentAddress, loggerInformation, Hexes);
            }

            //read logger data
            while (isReaderConnected && currentAddress != null && (currentAddress.MemoryNumber <= loggerInformation.MaxMemory))
            {
                if (loggerInformation.LoggerType != 1 && isSuccessfulRead == true)
                {
                    currentAddress = RetrieveMaxAddress(loggerInformation, currentAddress);
                }

                isReaderConnected = WriteBytes(new ReadLoggerByteWritter(currentAddress, loggerInformation.LoggerType), serialPort);
                isSuccessfulRead  = ReadBytesReadLogger(serialPort, loggerInformation, currentAddress, Hexes);

                if (loggerInformation.LoggerType == 1 && isSuccessfulRead == true)
                {
                    currentAddress = RetrieveMaxAddress(loggerInformation, currentAddress);
                }

                if (isSuccessfulRead == true)
                {
                    currentAddress = GetNextAddress(currentAddress, loggerInformation);
                }

                if (recoverCount > 20)
                {
                    Console.WriteLine("Hex has been cleared ");
                    Hexes.Clear();
                    return(Hexes);
                }
            }
            serialPort.Close();
            return(Hexes);
        }
 AddressSection RetrieveMaxAddress(LoggerInformation loggerInformation, AddressSection currentAddress)
 {
     if (lengthToRead >= (loggerInformation.MemoryMax[currentAddress.MemoryNumber] - currentAddress.MemoryAddress) && currentAddress.MemoryNumber != 8)
     {
         recoverCount = 0;
         lengthToRead = loggerInformation.MemoryMax[currentAddress.MemoryNumber] - currentAddress.MemoryAddress;
         currentAddress.MemoryAddress = loggerInformation.MemoryMax[currentAddress.MemoryNumber];
         currentAddress.LengthMSB     = (byte)((lengthToRead >> 8) & 0xff);
         currentAddress.LengthLSB     = (byte)(lengthToRead & 0xff);
     }
     return(currentAddress);
 }
        AddressSection GetNextAddress(AddressSection currentAddress, LoggerInformation loggerInformation)
        {
            if (currentAddress.MemoryAddress < loggerInformation.MemoryMax[currentAddress.MemoryNumber])
            {
                currentAddress.MemoryAddress  = ((currentAddress.MemoryAddMSB & 0xff) << 8) | (currentAddress.MemoryAddLSB & 0xff);
                currentAddress.MemoryAddress += lengthToRead;

                if (currentAddress.MemoryAddress >= loggerInformation.MemoryMax[currentAddress.MemoryNumber])
                {
                    lengthToRead = lengthToRead - currentAddress.MemoryAddress + loggerInformation.MemoryMax[currentAddress.MemoryNumber];
                    currentAddress.MemoryAddMSB = (byte)((loggerInformation.MemoryMax[currentAddress.MemoryNumber] >> 8) & 0xff);
                    currentAddress.MemoryAddLSB = (byte)(loggerInformation.MemoryMax[currentAddress.MemoryNumber] & 0xff);
                }
                else
                {
                    currentAddress.MemoryAddMSB = (byte)((currentAddress.MemoryAddress >> 8) & 0xff);
                    currentAddress.MemoryAddLSB = (byte)((currentAddress.MemoryAddress) & 0xff);
                }

                return(currentAddress);
            }
            else
            {
                while (currentAddress.MemoryNumber < loggerInformation.MaxMemory)
                {
                    currentAddress.MemoryNumber++;

                    if (loggerInformation.MemoryMax[currentAddress.MemoryNumber] != 0)
                    {
                        if (loggerInformation.MemoryMax[currentAddress.MemoryNumber] > loggerInformation.MemoryStart[currentAddress.MemoryNumber])
                        {
                            lengthToRead = MaxReadingLength;
                            currentAddress.MemoryAddress = loggerInformation.MemoryStart[currentAddress.MemoryNumber];
                            currentAddress.MemoryAddMSB  = (byte)((currentAddress.MemoryAddress >> 8) & 0xff);
                            currentAddress.MemoryAddLSB  = (byte)(currentAddress.MemoryAddress & 0xff);
                            return(currentAddress);
                        }
                    }
                }
            }
            currentAddress.MemoryNumber++;
            return(currentAddress);
        }
        public bool GenerateHexFile(SerialPort serialPort, LoggerInformation loggerInformation)
        {
            var Hexes = new List <Hex>();

            Hexes = ReadLogger(serialPort, loggerInformation, Hexes);

            if (Hexes.Count > 0)
            {
                var saveFilePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Temprecord\\TempLite\\";
                var path         = saveFilePath + loggerInformation.SerialNumber + ".hex";
                var sw           = new StreamWriter(path);
                foreach (var hex in Hexes)
                {
                    sw.WriteLine(hex.ToString());
                }
                sw.Close();
                return(false);
            }

            return(true);
        }
예제 #9
0
 public void RecordLog(LoggerInformation logInfo)
 {
     objContext.LoggerInformations.Add(logInfo);
     objContext.SaveChanges();
 }
        bool ReadBytesReadLogger(SerialPort serialPort, LoggerInformation loggerInformation, AddressSection currentAddress, List <Hex> Hexes)
        {
            lengthToRead = MaxReadingLength;
            var msg         = ReadBytes(serialPort);
            var addressRead = currentAddress.MemoryNumber.ToString("x02") + currentAddress.MemoryAddMSB.ToString("x02") + currentAddress.MemoryAddLSB.ToString("x02");

            if (msg.ToString() == "System.TimeoutException")
            {
                return(false);
            }

            if (msg.Length >= lengthToRead || currentAddress.MemoryAddress == loggerInformation.MemoryMax[currentAddress.MemoryNumber])
            {
                var finalmsg = string.Empty;

                if (msg.Length > 124)
                {
                    if (loggerInformation.LoggerType == 1)
                    {
                        finalmsg = msg.ToString(12, 116);
                    }
                    else
                    {
                        finalmsg = msg.ToString(2, 116);
                    }
                }

                else if ((msg.Length < (currentAddress.LengthLSB * 2) + 6) && currentAddress.MemoryNumber == 4)
                {
                    return(false);
                }

                else
                {
                    if (loggerInformation.LoggerType == 1)
                    {
                        finalmsg = msg.ToString(12, msg.Length - 12);
                    }
                    else
                    {
                        finalmsg = msg.ToString(2, msg.Length - 8);
                    }
                }

                Hexes.Add(new Hex(addressRead, finalmsg));
            }

            else if (currentAddress.MemoryNumber == 8)
            {
                var sendMessage = new byte[2];
                sendMessage[0] = 0x00;
                sendMessage[1] = 0x55;
                serialPort.Write(sendMessage, 0, sendMessage.Length);
                return(false);
            }

            else
            {
                WriteBytes(new WakeUpByteWritter(), serialPort);
                ReadBytes(serialPort);
                return(false);
            }

            currentAddress.LengthMSB = (byte)((lengthToRead >> 8) & 0xff);
            currentAddress.LengthLSB = (byte)(lengthToRead & 0xff);
            return(true);
        }
 AddressSection ReadBytesWakeUp(SerialPort serialPort, LoggerInformation loggerInformation, List <byte> messageReceived, List <Hex> hexes)
 {
     SetLoggerInformation(messageReceived, loggerInformation);
     return(SetCurrentAddress(serialPort, loggerInformation, hexes));
 }