示例#1
0
        public bool OpcodePartialMatch()
        {
            var opcodeFile = NetworkController.Instance.LoadOpcodeCheck;

            NetworkController.Instance.LoadOpcodeCheck = null;
            var    file = new System.IO.StreamReader(opcodeFile);
            string line;
            bool   matched = true;

            while ((line = file.ReadLine()) != null)
            {
                line = line.Replace("=", "");
                var match = Regex.Match(line, @"(?i)^\s*([a-z_0-9]+)\s+(\d+)\s*$");
                Enum.TryParse(match.Groups[1].Value, out OpcodeEnum opcodeName);
                OpcodeId opcodeId = OpcodeId.Parse(match.Groups[2].Value);

                if (KnownOpcode.ContainsKey(opcodeId) && KnownOpcode[opcodeId] != opcodeName)
                {
                    Console.WriteLine("Incorrect match type 1 for " + KnownOpcode[opcodeId] + " : " + opcodeId);
                    matched = false;
                }
                else if (ReverseKnownOpcode.ContainsKey(opcodeName) && ReverseKnownOpcode[opcodeName] != opcodeId)
                {
                    Console.WriteLine("Incorrect match type 2 for " + opcodeName + " : " + opcodeId);
                    matched = false;
                }
                else if (!ReverseKnownOpcode.ContainsKey(opcodeName) && !KnownOpcode.ContainsKey(opcodeId))
                {
                    // Stay silent if the parser didn't found every opcode.
                    // TODO: add option for strict match: aka -> this case generate error
                    if (NetworkController.Instance.StrictCheck)
                    {
                        Console.WriteLine("Missing match for " + opcodeName + " : " + opcodeId);
                        matched = false;
                    }
                }
                else
                {
                    Console.WriteLine("Correct match for " + KnownOpcode[opcodeId] + " : " + opcodeId);
                }
            }

            file.Close();
            return(matched);
        }
示例#2
0
        public void OpcodeLoadKnown()
        {
            var opcodeFile = NetworkController.Instance.LoadOpcodeForViewing;

            NetworkController.Instance.LoadOpcodeForViewing = null;
            KnownOpcode.Clear();
            ReverseKnownOpcode.Clear();
            NetworkController.Instance.UiUpdateKnownOpcode.Clear();
            var    file = new System.IO.StreamReader(opcodeFile);
            string line;

            while ((line = file.ReadLine()) != null)
            {
                line = line.Replace("=", "");
                var match = Regex.Match(line, @"(?i)^\s*([a-z_0-9]+)\s+(\d+)\s*$");
                Enum.TryParse(match.Groups[1].Value, out OpcodeEnum opcodeName);
                OpcodeId opcodeId = OpcodeId.Parse(match.Groups[2].Value);
                SetOpcode(opcodeId, opcodeName);
            }
            file.Close();
            _viewOnly = true;
        }
示例#3
0
        public static void Main(string[] args)
        {
            TPUDSStatus    Status;
            TPUDSCANHandle Channel;
            int            nbErr      = 0;
            uint           param      = 0x0;
            byte           serverAddr = (byte)TPUDSAddress.PUDS_ISO_15765_4_ADDR_ECU_1;
            StringBuilder  buff       = new StringBuilder();

            // Show version information
            UDSApi.GetValue(0, TPUDSParameter.PUDS_PARAM_API_VERSION, buff, 50);
            Console.Write($"PCAN-UDS API Version : {buff}\n");

            // Sets the default PCAN-Channel to use (PCAN-USB Channel 2)
            //
            Channel = UDSApi.PUDS_USBBUS2;

            // Sets server address and channel from application arguments if specified
            if (args.Length == 3)
            {
                Channel    = (TPCANHandle)(UDSApi.PUDS_USBBUS1 - 1 + TPCANHandle.Parse(args[1]));
                serverAddr = byte.Parse(args[2]);
            }

            // Initializing of the UDS Communication session
            //
            Status = UDSApi.Initialize(Channel, TPUDSBaudrate.PUDS_BAUD_250K, 0, 0, 0);
            Console.Write($"Initialize UDS: {Status} (chan. 0x{Channel:x2})\n");

            // Define server address and filtered address
            //
            param  = serverAddr;
            Status = UDSApi.SetValue(Channel, TPUDSParameter.PUDS_PARAM_SERVER_ADDRESS, ref param, (uint)Marshal.SizeOf(param));
            Console.Write($"Set ServerAddress: {Status}\n");
            Status = UDSApi.GetValue(Channel, TPUDSParameter.PUDS_PARAM_SERVER_ADDRESS, out param, (uint)Marshal.SizeOf(param));
            Console.Write($"ServerAddress = 0x{param}\n");
            // listen to the standard OBD functional address
            param  = (uint)TPUDSAddress.PUDS_ISO_15765_4_ADDR_OBD_FUNCTIONAL | UDSApi.PUDS_SERVER_FILTER_LISTEN;
            Status = UDSApi.SetValue(Channel, TPUDSParameter.PUDS_PARAM_SERVER_FILTER, ref param, (uint)Marshal.SizeOf(param));
            Console.Write($"Set Filtered Address: {Status}\n");

            Console.Write("\nNote: press 'c' or 'C' to clear screen,");
            Console.Write("\nNote: press 'q', 'Q' or '<Escape>' to quit...\n\n");

            // Message Polling
            //
            TPUDSMsg Message;
            bool     bStop = false;

            while (!bStop)
            {
                // check message
                Status = UDSApi.Read(Channel, out Message);
                if (Status == TPUDSStatus.PUDS_ERROR_OK)
                {
                    // display message
                    displayMessage(ref Message, Message.NETADDRINFO.SA != serverAddr);
                    // check if an automatic reply should be sent
                    if (Message.NETADDRINFO.SA != serverAddr &&         // do not reply to Tx Confirmation from this server
                        Message.RESULT == TPUDSResult.PUDS_RESULT_N_OK) // and reply if there is no Network error
                    {
                        // do not reply if it was requested not to
                        if (Message.NO_POSITIVE_RESPONSE_MSG == UDSApi.PUDS_SUPPR_POS_RSP_MSG_INDICATION_BIT)
                        {
                            Console.Write("\n   ...Skipping response...\n");
                        }
                        // do not reply to incoming message notification
                        else if (Message.MSGTYPE != TPUDSMessageType.PUDS_MESSAGE_TYPE_INDICATION)
                        {
                            transmitResponse(Channel, serverAddr, ref Message);
                        }
                    }
                }
                Thread.Sleep(1);
                // check exit request
                if (Console.KeyAvailable)
                {
                    switch (Console.ReadKey().KeyChar)
                    {
                    case 'q':
                    case 'Q':
                    case (char)27:        //Escape
                        bStop = true;
                        break;

                    case 'c':
                    case 'C':
                        Console.Clear();
                        Console.Write($"ServerAddress = 0x{serverAddr:x2}\n");
                        Console.Write("\nNote: press 'c' or 'C' to clear screen,");
                        Console.Write("\nNote: press 'q', 'Q' or '<Escape>' to quit...\n\n");
                        break;
                    }
                }
            }

            // Display a small report
            if (nbErr > 0)
            {
                Console.Write($"\nERROR : {nbErr} errors occured.\n\n");
                Console.Write("\n\nPress <Enter> to quit...");
                Console.ReadKey(true);
            }

            // Release channel
            UDSApi.Uninitialize(Channel);
        }
示例#4
0
        public static bool TryConvert(string data, DataType flag, out object val)
        {
            val = null;

            if (flag == DataType.Null)
            {
                return(false);
            }

            try
            {
                var numStyle = Insensitive.StartsWith(data.Trim(), "0x") ? NumberStyles.HexNumber : NumberStyles.Any;

                if (numStyle == NumberStyles.HexNumber)
                {
                    data = data.Substring(data.IndexOf("0x", StringComparison.OrdinalIgnoreCase) + 2);
                }

                switch (flag)
                {
                case DataType.Bool:
                    val = Boolean.Parse(data);
                    return(true);

                case DataType.Char:
                    val = Char.Parse(data);
                    return(true);

                case DataType.Byte:
                    val = Byte.Parse(data, numStyle);
                    return(true);

                case DataType.SByte:
                    val = SByte.Parse(data, numStyle);
                    return(true);

                case DataType.Short:
                    val = Int16.Parse(data, numStyle);
                    return(true);

                case DataType.UShort:
                    val = UInt16.Parse(data, numStyle);
                    return(true);

                case DataType.Int:
                    val = Int32.Parse(data, numStyle);
                    return(true);

                case DataType.UInt:
                    val = UInt32.Parse(data, numStyle);
                    return(true);

                case DataType.Long:
                    val = Int64.Parse(data, numStyle);
                    return(true);

                case DataType.ULong:
                    val = UInt64.Parse(data, numStyle);
                    return(true);

                case DataType.Float:
                    val = Single.Parse(data, numStyle);
                    return(true);

                case DataType.Decimal:
                    val = Decimal.Parse(data, numStyle);
                    return(true);

                case DataType.Double:
                    val = Double.Parse(data, numStyle);
                    return(true);

                case DataType.String:
                    val = data;
                    return(true);

                case DataType.DateTime:
                    val = DateTime.Parse(data, CultureInfo.CurrentCulture, DateTimeStyles.AllowWhiteSpaces);
                    return(true);

                case DataType.TimeSpan:
                    val = TimeSpan.Parse(data);
                    return(true);

                default:
                    return(false);
                }
            }
            catch
            {
                return(false);
            }
        }
示例#5
0
        public static bool TryParse(string data, DataType flag, out SimpleType value)
        {
            try
            {
                switch (flag)
                {
                case DataType.Null:
                    value = new SimpleType(null);
                    break;

                case DataType.Bool:
                    value = new SimpleType(Boolean.Parse(data));
                    break;

                case DataType.Char:
                    value = new SimpleType(Char.Parse(data));
                    break;

                case DataType.Byte:
                    value = new SimpleType(Byte.Parse(data));
                    break;

                case DataType.SByte:
                    value = new SimpleType(SByte.Parse(data));
                    break;

                case DataType.Short:
                    value = new SimpleType(Int16.Parse(data));
                    break;

                case DataType.UShort:
                    value = new SimpleType(UInt16.Parse(data));
                    break;

                case DataType.Int:
                    value = new SimpleType(Int32.Parse(data));
                    break;

                case DataType.UInt:
                    value = new SimpleType(UInt32.Parse(data));
                    break;

                case DataType.Long:
                    value = new SimpleType(Int64.Parse(data));
                    break;

                case DataType.ULong:
                    value = new SimpleType(UInt64.Parse(data));
                    break;

                case DataType.Float:
                    value = new SimpleType(Single.Parse(data));
                    break;

                case DataType.Decimal:
                    value = new SimpleType(Decimal.Parse(data));
                    break;

                case DataType.Double:
                    value = new SimpleType(Double.Parse(data));
                    break;

                case DataType.String:
                    value = new SimpleType(data);
                    break;

                case DataType.DateTime:
                    value = new SimpleType(DateTime.Parse(data));
                    break;

                case DataType.TimeSpan:
                    value = new SimpleType(TimeSpan.Parse(data));
                    break;

                default:
                    value = new SimpleType(null);
                    break;
                }

                return(true);
            }
            catch
            {
                value = new SimpleType(null);
                return(false);
            }
        }