Exemplo n.º 1
0
        private async Task <string> GetVIN()
        {
            var result = await SendAndReceive("0902\r");

            if (result.StartsWith("49"))
            {
                while (!result.Contains("49 02 05"))
                {
                    string tmp = await ReceiveAsync();

                    result += tmp;
                }
            }
            return(ObdExtension.ParseVINMsg(result));
        }
Exemplo n.º 2
0
        public Dictionary <string, string> Read()
        {
            var    ret = new Dictionary <string, string>();
            string s;

            if (simulatormode)
            {
                s = "SIMULATORANDROID1";
                ret.Add("vin", s);
                foreach (var cmd in piDs.Keys)
                {
                    var key = piDs[cmd];
                    s = ObdExtension.GetEmulatorValue(cmd);
                    ret.Add(key, s);
                }
                return(ret);
            }

            if (!simulatormode && bluetoothSocket == null)
            {
                //if there is no connection
                return(null);
            }

            lock (_lock)
            {
                foreach (var key in data.Keys)
                {
                    ret.Add(key, data[key]);
                }
                foreach (var v in piDs.Values)
                {
                    data[v] = DefValue;
                }
            }
            return(ret);
        }
Exemplo n.º 3
0
        public async Task <bool> Init(bool simulatormode = false)
        {
            running = true;
            //initialize _data
            data = new Dictionary <string, string> {
                { "vin", DefValue }
            };
            //VIN
            piDs = ObdExtension.GetPIDs();
            foreach (var v in piDs.Values)
            {
                data.Add(v, DefValue);
            }

            this.simulatormode = simulatormode;
            if (simulatormode)
            {
                PollObd();

                return(true);
            }
            bool isObdReaderAvailable = false;

            foreach (var netInterface in NetworkInterface.GetAllNetworkInterfaces())
            {
                if (netInterface.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 ||
                    netInterface.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
                {
                    foreach (var addrInfo in netInterface.GetIPProperties().UnicastAddresses)
                    {
                        var ipaddr = addrInfo.Address;
                        if (ipaddr.ToString().StartsWith("192.168.0"))
                        {
                            isObdReaderAvailable = true;
                            break;
                        }
                    }
                }
            }
            if (!isObdReaderAvailable)
            {
                socket    = null;
                running   = false;
                connected = false;
                return(false);
            }

            if (!ConnectSocket())
            {
                socket    = null;
                running   = false;
                connected = false;
                return(false);
            }


            if (connected)
            {
                //initialize the device
                string s;
                s = await SendAndReceive("ATZ\r");

                s = await SendAndReceive("ATE0\r");

                s = await SendAndReceive("ATL1\r");

                s = await SendAndReceive("ATSP00\r");

                PollObd();

                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemplo n.º 4
0
        private async Task <string> RunCmd(string cmd)
        {
            var result = await SendAndReceive(cmd + "\r");

            return(ObdExtension.ParseObd01Msg(result));
        }
Exemplo n.º 5
0
 private async void PollObd()
 {
     try
     {
         string s;
         if (simulatormode)
         {
             s = "SIMULATORIPHONE12";
         }
         else
         {
             s = await GetVIN();
         }
         lock (_lock)
         {
             data["vin"] = s;
         }
         while (true)
         {
             foreach (var cmd in piDs.Keys)
             {
                 var key = piDs[cmd];
                 if (simulatormode)
                 {
                     s = ObdExtension.GetEmulatorValue(cmd);
                 }
                 else
                 {
                     s = await RunCmd(cmd);
                 }
                 if (s != "ERROR")
                 {
                     lock (_lock)
                     {
                         data[key] = s;
                     }
                 }
                 if (!running)
                 {
                     return;
                 }
             }
             await Task.Delay(Interval);
         }
     }
     catch (Exception ex)
     {
         System.Diagnostics.Debug.WriteLine(ex.Message);
         running = false;
         if (stream != null)
         {
             stream.Close();
             stream = null;
         }
         if (socket != null)
         {
             socket.Close();
             socket = null;
         }
     }
 }
Exemplo n.º 6
0
        public async Task <bool> Init(bool simulatormode = false)
        {
            running = true;
            //initialize _data
            data = new Dictionary <string, string> {
                { "vin", DefValue }
            };
            //VIN
            piDs = ObdExtension.GetPIDs();

            foreach (var v in piDs.Values)
            {
                data.Add(v, DefValue);
            }

            this.simulatormode = simulatormode;
            if (simulatormode)
            {
                return(true);
            }

            bluetoothAdapter = BluetoothAdapter.DefaultAdapter;
            if (bluetoothAdapter == null)
            {
                System.Diagnostics.Debug.WriteLine("Bluetooth is not available");
                return(false);
            }
            try
            {
                var ba = bluetoothAdapter.BondedDevices;
                foreach (var bd in ba)
                {
                    if (bd.Name.ToLower().Contains("obd"))
                    {
                        bluetoothDevice = bd;
                    }
                }
                if (bluetoothDevice == null)
                {
                    return(false);
                }
                bluetoothSocket = bluetoothDevice.CreateRfcommSocketToServiceRecord(SppUuid);

                await bluetoothSocket.ConnectAsync();

                connected = true;
            }
            catch (Java.IO.IOException ex)
            {
                // Close the socket
                try
                {
                    connected = false;
                    bluetoothSocket.Close();
                }
                catch (Java.IO.IOException)
                {
                }
                catch (Exception)
                {
                }

                return(false);
            }
            catch (Exception ex)
            {
            }
            if (connected)
            {
                reader = bluetoothSocket.InputStream;
                writer = bluetoothSocket.OutputStream;

                string s;
                s = await SendAndReceive("ATZ\r");

                s = await SendAndReceive("ATE0\r");

                s = await SendAndReceive("ATL1\r");

                s = await SendAndReceive("ATSP00\r");

                PollObd();

                return(true);
            }
            else
            {
                return(false);
            }
        }