Example #1
0
        private async Task <bool> SharedInitialization()
        {
            // This will only be used for device-independent initialization.
            ElmDeviceImplementation sharedImplementation = new ElmDeviceImplementation(null, null, this.Port, this.Logger);

            return(await sharedImplementation.Initialize());
        }
Example #2
0
        /// <summary>
        /// Use the related classes to discover which type of device is currently connected.
        /// </summary>
        public override async Task <bool> Initialize()
        {
            try
            {
                this.Logger.AddDebugMessage("ElmDevice initialization starting.");

                SerialPortConfiguration configuration = new SerialPortConfiguration();
                configuration.BaudRate = 115200;
                configuration.Timeout  = 1200;

                await this.Port.OpenAsync(configuration);

                await this.Port.DiscardBuffers();

                if (!await this.SharedInitialization())
                {
                    return(false);
                }

                AllProDeviceImplementation allProDevice = new AllProDeviceImplementation(
                    this.Enqueue,
                    () => this.ReceivedMessageCount,
                    this.Port,
                    this.Logger);
                if (await allProDevice.Initialize())
                {
                    this.implementation = allProDevice;
                }
                else
                {
                    ScanToolDeviceImplementation scanToolDevice = new ScanToolDeviceImplementation(
                        this.Enqueue,
                        () => this.ReceivedMessageCount,
                        this.Port,
                        this.Logger);

                    if (await scanToolDevice.Initialize())
                    {
                        this.implementation = scanToolDevice;
                    }
                }

                // These are shared by all ELM-based devices.
                if (!await this.implementation.SendAndVerify("AT AL", "OK") ||                                 // Allow Long packets
                    !await this.implementation.SendAndVerify("AT SP2", "OK") ||                                // Set Protocol 2 (VPW)
                    !await this.implementation.SendAndVerify("AT DP", "SAE J1850 VPW") ||                      // Get Protocol (Verify VPW)
                    !await this.implementation.SendAndVerify("AT AR", "OK") ||                                 // Turn Auto Receive on (default should be on anyway)
                    !await this.implementation.SendAndVerify("AT AT0", "OK") ||                                // Disable adaptive timeouts
                    !await this.implementation.SendAndVerify("AT SR " + DeviceId.Tool.ToString("X2"), "OK") || // Set receive filter to this tool ID
                    !await this.implementation.SendAndVerify("AT H1", "OK") ||                                 // Send headers
                    !await this.implementation.SendAndVerify("AT ST 20", "OK")                                 // Set timeout (will be adjusted later, too)
                    )
                {
                    return(false);
                }

                this.MaxSendSize    = this.implementation.MaxSendSize;
                this.MaxReceiveSize = this.implementation.MaxReceiveSize;
                this.Supports4X     = this.implementation.Supports4X;
            }
            catch (Exception exception)
            {
                this.Logger.AddDebugMessage("Unable to initalize " + this.ToString());
                this.Logger.AddDebugMessage(exception.ToString());
                return(false);
            }

            return(true);
        }