Пример #1
0
    /// <summary>
    /// Main for 1-Wire Memory utility
    /// </summary>
    public static void Main1(string[] args)
    {
        List <OneWireContainer> owd_vect = new List <OneWireContainer>(5);
        SwitchContainer         sw       = null;
        int           ch;
        bool          done    = false;
        DSPortAdapter adapter = null;

        byte[] state;

        Stream stream = loadResourceFile("Switch.input.txt");

        dis = new StreamReader(stream);

        Debug.WriteLine("");
        Debug.WriteLine("1-Wire Switch utility console application: Version 0.00");
        Debug.WriteLine("");

        try
        {
            // get the default adapter
            adapter = OneWireAccessProvider.DefaultAdapter;

            // adapter driver info
            Debug.WriteLine("=========================================================================");
            Debug.WriteLine("== Adapter Name: " + adapter.AdapterName);
            Debug.WriteLine("== Adapter Port description: " + adapter.PortTypeDescription);
            Debug.WriteLine("== Adapter Version: " + adapter.AdapterVersion);
            Debug.WriteLine("== Adapter support overdrive: " + adapter.canOverdrive());
            Debug.WriteLine("== Adapter support hyperdrive: " + adapter.canHyperdrive());
            Debug.WriteLine("== Adapter support EPROM programming: " + adapter.canProgram());
            Debug.WriteLine("== Adapter support power: " + adapter.canDeliverPower());
            Debug.WriteLine("== Adapter support smart power: " + adapter.canDeliverSmartPower());
            Debug.WriteLine("== Adapter Class Version: " + adapter.ClassVersion);

            // get exclusive use of adapter
            adapter.beginExclusive(true);

            // force first select device
            int main_selection = MAIN_SELECT_DEVICE;

            // loop to do menu
            do
            {
                // Main menu
                switch (main_selection)
                {
                case MAIN_DISPLAY_INFO:
                    // display Switch info
                    printSwitchInfo(sw);
                    break;

                case MAIN_CLEAR_ACTIVITY:
                    sw.clearActivity();
                    state = sw.readDevice();
                    sw.writeDevice(state);
                    // display Switch info
                    printSwitchInfo(sw);
                    break;

                case MAIN_SET_LATCH:
                    state = sw.readDevice();
                    Debug.Write("Enter the channel number: ");
                    ch = getNumber(0, sw.getNumberChannels(state) - 1);
                    if (menuSelect(stateMenu) == STATE_ON)
                    {
                        sw.setLatchState(ch, true, false, state);
                    }
                    else
                    {
                        sw.setLatchState(ch, false, false, state);
                    }
                    sw.writeDevice(state);
                    // display Switch info
                    printSwitchInfo(sw);
                    break;

                case MAIN_SELECT_DEVICE:
                    // find all parts
                    owd_vect = findAllSwitchDevices(adapter);
                    // select a device
                    sw = (SwitchContainer)selectDevice(owd_vect);
                    // display device info
                    printDeviceInfo((OneWireContainer)sw);
                    // display the switch info
                    printSwitchInfo(sw);
                    break;

                case MAIN_QUIT:
                    done = true;
                    break;
                }

                if (!done)
                {
                    main_selection = menuSelect(mainMenu);
                }
            } while (!done);
        }
        catch (Exception e)
        {
            Debug.WriteLine(e);
        }
        finally
        {
            if (adapter != null)
            {
                // end exclusive use of adapter
                adapter.endExclusive();

                // free the port used by the adapter
                Debug.WriteLine("Releasing adapter port");
                try
                {
                    adapter.freePort();
                }
                catch (OneWireException e)
                {
                    Debug.WriteLine(e);
                }
            }
        }

        Debug.WriteLine("");
        return;
    }
Пример #2
0
    /// <summary>
    /// Main for 1-Wire File Shell (OWFish)
    /// </summary>
    public static void Main1(string[] args)
    {
        List <OneWireContainer> owd_vect = new List <OneWireContainer>(5);

        OneWireContainer[] owd = null;
        DSPortAdapter      adapter = null;
        int        selection, len;
        long       start_time, end_time;
        Stopwatch  stopWatch = new Stopwatch();
        FileStream fos;
        Stream     fis;
//	  FileDescriptor fd;
        OWFileOutputStream owfos;
        OWFileInputStream  owfis;
        OWFileDescriptor   owfd;
        OWFile             owfile, new_owfile;

        byte[] block = new byte[32];

        Debug.WriteLine("");
        Debug.WriteLine("1-Wire File Shell (OWFish): Version 0.00");
        Debug.WriteLine("");

        // load the simulated console input stream
        Stream stream = loadResourceFile("OWFish.input.txt");

        dis = new StreamReader(stream);

        stopWatch.Start();

        try
        {
            // get the default adapter
            adapter = OneWireAccessProvider.DefaultAdapter;

            // adapter driver info
            Debug.WriteLine("=========================================================================");
            Debug.WriteLine("== Adapter Name: " + adapter.AdapterName);
            Debug.WriteLine("== Adapter Port description: " + adapter.PortTypeDescription);
            Debug.WriteLine("== Adapter Version: " + adapter.AdapterVersion);
            Debug.WriteLine("== Adapter support overdrive: " + adapter.canOverdrive());
            Debug.WriteLine("== Adapter support hyperdrive: " + adapter.canHyperdrive());
            Debug.WriteLine("== Adapter support EPROM programming: " + adapter.canProgram());
            Debug.WriteLine("== Adapter support power: " + adapter.canDeliverPower());
            Debug.WriteLine("== Adapter support smart power: " + adapter.canDeliverSmartPower());
            Debug.WriteLine("== Adapter Class Version: " + adapter.ClassVersion);

            // get exclusive use of adapter
            adapter.beginExclusive(true);

            // loop to do menu
            selection = MAIN_SELECT_DEVICE;
            do
            {
                start_time = 0;
                try
                {
                    switch (selection)
                    {
                    case MAIN_SELECT_DEVICE:
                        // find all parts
                        owd_vect = findAllDevices(adapter);
                        // select a device
                        owd = selectDevice(owd_vect);
                        // check for quite
                        if (owd == null)
                        {
                            selection = MAIN_QUIT;
                        }
                        else
                        {
                            // display device info
                            Debug.WriteLine("");
                            Debug.WriteLine("  Device(s) selected: ");
                            printDeviceInfo(owd, false);
                        }
                        break;

                    case MAIN_FORMAT:
                        if (menuSelect(verifyMenu) == VERIFY_YES)
                        {
                            // start time of operation
                            start_time = stopWatch.ElapsedMilliseconds;
                            // create a 1-Wire file at root
                            owfile = new OWFile(owd, "");
                            // format Filesystem
                            owfile.format();
                            // get 1-Wire File descriptor to flush to device
                            owfd = owfile.FD;
                            syncFileDescriptor(owfd);
                            // close the 1-Wire file to release
                            owfile.close();
                        }
                        break;

                    case MAIN_LIST:
                        Debug.Write("Enter the directory to list on (/ for root): ");
                        // get the directory and create a file on it
                        owfile = new OWFile(owd, getString(1));
                        Debug.WriteLine("");
                        // start time of operation
                        start_time = stopWatch.ElapsedMilliseconds;
                        // list the files without recursion
                        listDir(owfile, 1, false);
                        // close the 1-Wire file to release
                        owfile.close();
                        break;

                    case MAIN_RLIST:
                        // start time of operation
                        start_time = stopWatch.ElapsedMilliseconds;
                        // get the directory and create a file on it
                        owfile = new OWFile(owd, "");
                        Debug.WriteLine("");
                        // recursive list
                        listDir(owfile, 1, true);
                        // close the 1-Wire file to release
                        owfile.close();
                        break;

                    case MAIN_MKDIR:
                        Debug.Write("Enter the directory to create (from root): ");
                        // get the directory and create a file on it
                        owfile = new OWFile(owd, getString(1));
                        // start time of operation
                        start_time = stopWatch.ElapsedMilliseconds;
                        // make the directories
                        if (owfile.mkdirs())
                        {
                            Debug.WriteLine("Success!");
                        }
                        else
                        {
                            Debug.WriteLine("-----------------------------------------------");
                            Debug.WriteLine("Could not create directories, out of memory or invalid directory/file");
                            Debug.WriteLine("-----------------------------------------------");
                        }
                        // get 1-Wire File descriptor to flush to device
                        owfd = owfile.FD;
                        syncFileDescriptor(owfd);
                        // close the 1-Wire file to release
                        owfile.close();
                        break;

                    case MAIN_COPYTO:
                        // system SOURCE file
                        Debug.Write("Enter the path/file of the SOURCE file on the system: ");
//TODO					 fis = new FileStream(getString(1), FileMode.Open, FileAccess.Read);
                        fis = loadResourceFile(getString(1));
                        // 1-Wire DESTINATION file
                        Debug.Write("Enter the path/file of the DESTINATION on 1-Wire device: ");
                        owfos = new OWFileOutputStream(owd, getString(1));
                        // start time of operation
                        start_time = stopWatch.ElapsedMilliseconds;
                        // loop to copy block from SOURCE to DESTINATION
                        do
                        {
                            len = fis.Read(block, 0, block.Length);
                            if (len > 0)
                            {
                                owfos.write(block, 0, len);
                            }
                        } while (len > 0);
                        // get 1-Wire File descriptor to flush to device
                        owfd = owfos.FD;
                        syncFileDescriptor(owfd);
                        // close the files
                        owfos.Close();
                        fis.Dispose();
                        break;

                    case MAIN_COPYFROM:
                        // 1-Wire SOURCE file
                        Debug.Write("Enter the path/file of the SOURCE file on 1-Wire device: ");
                        owfis = new OWFileInputStream(owd, getString(1));
                        // system DESTINATION file
                        Debug.Write("Enter the path/file of the DESTINATION on system: ");
                        StorageFolder localFolder = ApplicationData.Current.LocalFolder;
                        string        outp        = localFolder.Path + "\\" + getString(1);
                        fos = new FileStream(outp, FileMode.Create, FileAccess.Write);
                        // start time of operation
                        start_time = stopWatch.ElapsedMilliseconds;
                        // loop to copy block from SOURCE to DESTINATION
                        do
                        {
                            len = owfis.read(block);
                            if (len > 0)
                            {
                                fos.Write(block, 0, len);
                            }
                        } while (len > 0);
                        // get 1-Wire File descriptor to flush to device
                        fos.Flush();
                        // close the files
                        owfis.close();
                        fos.Dispose();
                        break;

                    case MAIN_CAT:
                        // 1-Wire file
                        Debug.Write("Enter the path/file of the file to display: ");
                        owfis = new OWFileInputStream(owd, getString(1));
                        // start time of operation
                        start_time = stopWatch.ElapsedMilliseconds;
                        Debug.WriteLine("");
                        Debug.WriteLine("---FILE START---");
                        // loop to read and display file
                        do
                        {
                            len = owfis.read(block);
                            if (len > 0)
                            {
                                Debug.Write(System.Text.Encoding.UTF8.GetString(block));
                                com.dalsemi.onewire.debug.Debug.debug("file cat", block, 0, len);
                            }
                        } while (len > 0);
                        Debug.WriteLine("");
                        Debug.WriteLine("---FILE END---");
                        // close the file
                        owfis.close();
                        break;

                    case MAIN_DELETE:
                        Debug.Write("Enter the directory/file delete: ");
                        // get the directory and create a file on it
                        owfile = new OWFile(owd, getString(1));
                        Debug.WriteLine("");
                        // start time of operation
                        start_time = stopWatch.ElapsedMilliseconds;
                        // delete the directory/file
                        if (owfile.delete())
                        {
                            Debug.WriteLine("Success!");
                        }
                        else
                        {
                            Debug.WriteLine("-----------------------------------------------");
                            Debug.WriteLine("Could not delete, if it is a directory make sure it is empty");
                            Debug.WriteLine("-----------------------------------------------");
                        }
                        // get 1-Wire File descriptor to flush to device
                        owfd = owfile.FD;
                        syncFileDescriptor(owfd);
                        // close the 1-Wire file to release
                        owfile.close();
                        break;

                    case MAIN_RENAME:
                        Debug.Write("Enter the OLD directory/file name: ");
                        // get the directory and create a file on it
                        owfile = new OWFile(owd, getString(1));
                        Debug.WriteLine("");
                        Debug.Write("Enter the NEW directory/file name: ");
                        // get the directory and create a file on it
                        new_owfile = new OWFile(owd, getString(1));
                        Debug.WriteLine("");
                        // start time of operation
                        start_time = stopWatch.ElapsedMilliseconds;
                        // rename the directory/file
                        if (owfile.renameTo(new_owfile))
                        {
                            Debug.WriteLine("Success!");
                        }
                        else
                        {
                            Debug.WriteLine("-----------------------------------------------");
                            Debug.WriteLine("Could not rename, make sure parents of new directory exist");
                            Debug.WriteLine("-----------------------------------------------");
                        }
                        // get 1-Wire File descriptor to flush to device
                        owfd = owfile.FD;
                        syncFileDescriptor(owfd);
                        // close the 1-Wire file to release
                        owfile.close();
                        new_owfile.close();
                        break;

                    case MAIN_DETAILS:
                        Debug.Write("Enter the directory/file to view details: ");
                        // get the directory and create a file on it
                        owfile = new OWFile(owd, getString(1));
                        Debug.WriteLine("");
                        // start time of operation
                        start_time = stopWatch.ElapsedMilliseconds;
                        // show the file details
                        showDetails(owfile);
                        // close the 1-Wire file to release
                        owfile.close();
                        break;

                    case MAIN_FREEMEM:
                        // start time of operation
                        start_time = stopWatch.ElapsedMilliseconds;
                        // create a 1-Wire file at root
                        owfile = new OWFile(owd, "");
                        // get free memory
                        Debug.WriteLine("");
                        Debug.WriteLine("  free memory: " + owfile.FreeMemory + " (bytes)");
                        // get the devices participating
                        owd = owfile.OneWireContainers;
                        Debug.WriteLine("");
                        Debug.WriteLine("  Filesystem consists of: ");
                        printDeviceInfo(owd, true);
                        // close the 1-Wire file to release
                        owfile.close();
                        break;
                    }
                    ;
                }
                catch (IOException e)
                {
                    Debug.WriteLine("");
                    Debug.WriteLine("-----------------------------------------------");
                    Debug.WriteLine(e);
                    Debug.WriteLine("-----------------------------------------------");
                }

                end_time = stopWatch.ElapsedMilliseconds;
                Debug.WriteLine("");
                if (start_time > 0)
                {
                    Debug.WriteLine((end_time - start_time) + "ms");
                }
                Debug.WriteLine("");

                if (selection != MAIN_QUIT)
                {
                    selection = menuSelect(mainMenu);
                }
                Debug.WriteLine("");
            } while (selection != MAIN_QUIT);
        }
        catch (Exception e)
        {
            Debug.WriteLine(e);
        }
        finally
        {
            if (adapter != null)
            {
                // end exclusive use of adapter
                adapter.endExclusive();

                // free the port used by the adapter
                Debug.WriteLine("Releasing adapter port");
                try
                {
                    adapter.freePort();
                }
                catch (OneWireException e)
                {
                    Debug.WriteLine(e);
                }
            }
        }

        Debug.WriteLine("");
        return;
    }
Пример #3
0
    /// <summary>
    /// Main for OWDump
    /// </summary>
    public static void Main1(string[] args)
    {
        Debug.WriteLine("");
        Debug.WriteLine("OneWire Memory Dump console application: Version 0.01");
        Debug.WriteLine("");

        // check for correct command line parameters
        bool argsOK = false;

        if ((args.Length >= 1) && (args.Length <= 2))
        {
            argsOK = true;

            // check for valid flag
            if ((args [0].IndexOf("r", StringComparison.Ordinal) == -1) && (args [0].IndexOf("k", StringComparison.Ordinal) == -1) && (args [0].IndexOf("p", StringComparison.Ordinal) == -1))
            {
                Debug.WriteLine("Unsupported flag: " + args [0]);

                argsOK = false;
            }
        }

        if (!argsOK)
        {
            Debug.WriteLine("");
            Debug.WriteLine("syntax: OWDump ('r' 'p' 'k') <TIME_TEST>");
            Debug.WriteLine("   Dump an iButton/1-Wire Device's memory contents");
            Debug.WriteLine("   'r' 'p' 'k' - required flag: (Raw,Page,pacKet) type dump");
            Debug.WriteLine("   <TIME_TEST> - optional flag if present will time each read ");
            Debug.WriteLine("                 of the memory banks and not display the contents");
            return;
        }

        try
        {
            // get the default adapter
            DSPortAdapter adapter = OneWireAccessProvider.DefaultAdapter;

            // adapter driver info
            Debug.WriteLine("=========================================================================");
            Debug.WriteLine("== Adapter Name: " + adapter.AdapterName);
            Debug.WriteLine("== Adapter Port description: " + adapter.PortTypeDescription);
            Debug.WriteLine("== Adapter Version: " + adapter.AdapterVersion);
            Debug.WriteLine("== Adapter support overdrive: " + adapter.canOverdrive());
            Debug.WriteLine("== Adapter support hyperdrive: " + adapter.canHyperdrive());
            Debug.WriteLine("== Adapter support EPROM programming: " + adapter.canProgram());
            Debug.WriteLine("== Adapter support power: " + adapter.canDeliverPower());
            Debug.WriteLine("== Adapter support smart power: " + adapter.canDeliverSmartPower());
            Debug.WriteLine("== Adapter Class Version: " + adapter.ClassVersion);

            // get exclusive use of adapter
            adapter.beginExclusive(true);

            // clear any previous search restrictions
            adapter.setSearchAllDevices();
            adapter.targetAllFamilies();
            adapter.Speed = DSPortAdapter.SPEED_REGULAR;

            // enumerate through all the iButtons found
            for (System.Collections.IEnumerator owd_enum = adapter.AllDeviceContainers; owd_enum.MoveNext();)
            {
                // get the next owd
                OneWireContainer owd = (OneWireContainer)owd_enum.Current;

                Debug.WriteLine("");
                Debug.WriteLine("*************************************************************************");
                Debug.WriteLine("* 1-Wire Device Name: " + owd.Name);
                Debug.WriteLine("* 1-Wire Device Other Names: " + owd.AlternateNames);
                Debug.WriteLine("* 1-Wire Device Address: " + owd.AddressAsString);
                Debug.WriteLine("* 1-Wire Device Max speed: " + ((owd.MaxSpeed == DSPortAdapter.SPEED_OVERDRIVE) ? "Overdrive" : "Normal"));
                Debug.WriteLine("* 1-Wire Device Description: " + owd.Description);

                // set owd to max possible speed with available adapter, allow fall back
                if (adapter.canOverdrive() && (owd.MaxSpeed == DSPortAdapter.SPEED_OVERDRIVE))
                {
                    owd.setSpeed(owd.MaxSpeed, true);
                }

                // dump raw contents of all memory banks
                if (args [0].IndexOf("r", StringComparison.Ordinal) != -1)
                {
                    dumpDeviceRaw(owd, (args.Length == 1));
                }

                // dump page packets of non-volatile general-purpose memory banks
                else if (args [0].IndexOf("k", StringComparison.Ordinal) != -1)
                {
                    dumpDevicePackets(owd, (args.Length == 1));
                }

                // dump pages of memory bank
                else if (args [0].IndexOf("p", StringComparison.Ordinal) != -1)
                {
                    dumpDevicePages(owd, (args.Length == 1));
                }
                else
                {
                    Debug.WriteLine("No action taken, unsupported flag");
                }
            }

            // end exclusive use of adapter
            adapter.endExclusive();

            // free the port used by the adapter
            Debug.WriteLine("Releasing adapter port");
            adapter.freePort();
        }
        catch (Exception e)
        {
            Debug.WriteLine("Exception: " + e);
            Debug.WriteLine(e.ToString());
            Debug.Write(e.StackTrace);
        }

        Debug.WriteLine("");
        return;
    }