Configuring a device using an external configuration file


This post discusses a very simple scheme that I used to configure a TopCon GPS device by issuing commands from an external configuration file. This has the advantage of not requiring to recompile the app if you need to quickly change something. This pattern can be quite generally applied to configure any device that presents a command line interface.

Here’s the code of a parser that reads the external config file.

    public class TopconConfigParser
    {
        private StreamReader reader;

        public TopconConfigParser(string file)
        {
            reader = new StreamReader(File.Open(file, FileMode.Open));
        }

        /// Read the next command from the config file or null if
        /// no commands left to read
        public string NextCommand()
        {
            return ReadLine();
        }

        private string ReadLine() {
            StringBuilder line = new StringBuilder();
            int c;
            bool stop = false;
            bool inComment = false;
            while (!stop)
            {
                try
                {
                    c = reader.Read();
                }
                catch
                {
                    break;
                }
                switch (c)
                {
                    case '\r':
                    case '\n':
                    case -1:
                        if (line.Length > 0 || reader.EndOfStream)
                        {
                            stop = true;
                        }
                        break;
                    case '\t':
                    case ' ':
                        // ignore
                        break;
                    case '/':
                        if (reader.Peek() == '*')
                        {
                            inComment = true;
                        }
                        else
                        {
                            if (!inComment)
                            {
                                line.Append((char)c);
                            }
                        }
                        break;
                    case '*':
                        if (reader.Peek() == '/')
                        {
                            inComment = false;
                            reader.Read();
                        }
                        else
                        {
                            if (!inComment)
                            {
                                line.Append((char)c);
                            }
                        }
                        break;
                    case '%':
                    case '#':
                        DiscardTillEOL();
                        break;
                    default:
                        if (!inComment)
                        {
                            line.Append((char)c);
                        }
                        break;
                }
            }
            string retVal = line.ToString();
            return retVal == String.Empty ? null : retVal;
        }

        private void DiscardTillEOL() {
            int c = reader.Peek();
            while (c != -1 && c != '\r' && c != '\n')
            {
                reader.Read();
                c = reader.Peek();
            }
        }
    }

Here’s a simple example of a config file. It uses C language syntax for comments. A single line comment beginning with a # char can also be used.

/* reset device configuration */
init,/par/
/* NMEA version */
set,nmea/ver,v2.3
/* schedule periodic reception of data */
em,/dev/ser/a,/msg/nmea/GGA:1.0
Advertisements

Unusual uses of spreadsheets


Spreadsheets have several uses. Microsoft Excel is one of the most used applications for building spreadsheets, the other most probably is a similar application from the OpenOffice.org suite. I’ll be presenting some unusual usage of spreadsheets using Microsoft Excel but it may be possible to apply the same concepts with OpenOffice.org.

Prototyping icons
Icon

Creating timing diagrams
Timing Diagram

Representing 3D surfaces in GIS
3D Surface

Have you used spreadsheets in unusual ways? Share your experience by commenting below.

Triangulated irregular network or TIN


Triangulated irregular network or TIN is used to represent a surface digitally, using non-overlapping triangles, where each node has x, y and z information. For the surface of the Earth, it is common to have the original data in the form of a digital elevation model or DEM, composed of 2D points and their elevation information. This information requires more bandwidth to transmit and store, especially if the terrains are relatively smooth. Converting this information to TIN can reduce storage space.

A TIN can easily be created from scratch in SketchUp or obtained by importing a DEM. Enable the Sandbox tools in SketchUp and create a surface from scratch. The TIN surface can be exported to DXF using the SKP to DXF plugin for SketchUp. Once installed, select the surface and use the plugin to export it as DXF.

If you have digital elevation data (x, y, altitude) then you can easily obtain a TIN by using the 2D triangulation algorithm from Delaunay. Open source implementations are available for several languages. The C# implementation from Ceometric has been used by this author. They also sell a much more efficient version at their site.