Exemplo n.º 1
0
        //checks the checksum against the string
        public bool ValidateChecksum(string Sentence)
        {
            int sum = 0;

            try
            {
                char[] sentenceChars = Sentence.ToCharArray();
                // All character xor:ed results in the trailing hex checksum
                // The checksum calc starts after '$' and ends before '*'
                int inx;
                for (inx = 1; ; inx++)
                {
                    if (inx >= sentenceChars.Length) // No checksum found
                    {
                        return(false);
                    }
                    var tmp = sentenceChars[inx];
                    // Indicates end of data and start of checksum
                    if (tmp == '*')
                    {
                        break;
                    }
                    sum ^= tmp;    // Build checksum
                }
                // Calculated checksum converted to a 2 digit hex string
                string sumStr = String.Format("{0:X2}", sum);

                // Compare to checksum in sentence
                return(sumStr.Equals(Sentence.Substring(inx + 1, 2)));
            }
            catch (Exception e)
            {
                mf.WriteErrorLog("Validate Checksum" + e);
                return(false);
            }
        }
Exemplo n.º 2
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            //fill something in
            if (String.IsNullOrEmpty(tboxFieldName.Text.Trim()))
            {
                Close();
                return;
            }

            //append date time to name
            mf.currentFieldDirectory = tboxFieldName.Text.Trim() + String.Format("{0}", DateTime.Now.ToString("-yyyy.MMM.dd HH_mm", CultureInfo.InvariantCulture));

            //get the directory and make sure it exists, create if not
            string dirNewField = mf.fieldsDirectory + mf.currentFieldDirectory + "\\";

            //if no template set just make a new file.
            if (!isTemplateSet)
            {
                try
                {
                    //start a new job
                    mf.JobNew();

                    //create it for first save
                    string directoryName = Path.GetDirectoryName(dirNewField);

                    if ((!string.IsNullOrEmpty(directoryName)) && (Directory.Exists(directoryName)))
                    {
                        MessageBox.Show("Choose a different name", "Directory Exists", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                        return;
                    }
                    else
                    {
                        //reset the offsets
                        mf.pn.utmEast  = (int)mf.pn.actualEasting;
                        mf.pn.utmNorth = (int)mf.pn.actualNorthing;

                        mf.worldGrid.CreateWorldGrid(0, 0);

                        //make sure directory exists, or create it
                        if ((!string.IsNullOrEmpty(directoryName)) && (!Directory.Exists(directoryName)))
                        {
                            Directory.CreateDirectory(directoryName);
                        }

                        //create the field file header info
                        mf.FileCreateField();
                        mf.FileCreateSections();
                        //mf.FileCreateRecPath();
                        mf.FileCreateContour();
                        //mf.FileSaveABLine();
                    }
                }
                catch (Exception ex)
                {
                    mf.WriteErrorLog("Creating new field " + ex);

                    MessageBox.Show("Error", ex.ToString());
                    mf.currentFieldDirectory = "";
                }
            }
            else
            {
                // create from template
                string directoryName = Path.GetDirectoryName(dirNewField);

                if ((!string.IsNullOrEmpty(directoryName)) && (Directory.Exists(directoryName)))
                {
                    MessageBox.Show("Choose a different name", "Directory Exists", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    return;
                }
                else
                {
                    //create the new directory
                    if ((!string.IsNullOrEmpty(directoryName)) && (!Directory.Exists(directoryName)))
                    {
                        Directory.CreateDirectory(directoryName);
                    }
                }

                string line;
                string offsets;

                using (StreamReader reader = new StreamReader(templateFileAndDirectory))
                {
                    try
                    {
                        line = reader.ReadLine();
                        line = reader.ReadLine();
                        line = reader.ReadLine();
                        line = reader.ReadLine();

                        //read the Offsets  - all we really need from template field file
                        offsets = reader.ReadLine();
                    }
                    catch (Exception ex)
                    {
                        mf.WriteErrorLog("While Opening Field" + ex);

                        var form = new FormTimedMessage(4000, "Field File is Corrupt", "Choose a different field");
                        form.Show();
                        mf.JobClose();
                        return;
                    }

                    const string myFileName = "Field.txt";

                    using (StreamWriter writer = new StreamWriter(dirNewField + myFileName))
                    {
                        //Write out the date
                        writer.WriteLine(DateTime.Now.ToString("yyyy-MMMM-dd hh:mm:ss tt", CultureInfo.InvariantCulture));

                        writer.WriteLine("$FieldDir");
                        writer.WriteLine(mf.currentFieldDirectory.ToString(CultureInfo.InvariantCulture));

                        //write out the easting and northing Offsets
                        writer.WriteLine("$Offsets");
                        writer.WriteLine(offsets);
                    }

                    //create blank Contour and Section files
                    mf.FileCreateSections();
                    mf.FileCreateContour();

                    //copy over the files from template
                    string templateDirectoryName = Path.GetDirectoryName(templateFileAndDirectory);

                    string fileToCopy           = templateDirectoryName + "\\Boundary.txt";
                    string destinationDirectory = directoryName + "\\Boundary.txt";
                    if (File.Exists(fileToCopy))
                    {
                        File.Copy(fileToCopy, destinationDirectory);
                    }

                    fileToCopy           = templateDirectoryName + "\\Headland.txt";
                    destinationDirectory = directoryName + "\\Headland.txt";
                    if (File.Exists(fileToCopy))
                    {
                        File.Copy(fileToCopy, destinationDirectory);
                    }

                    fileToCopy           = templateDirectoryName + "\\Flags.txt";
                    destinationDirectory = directoryName + "\\Flags.txt";
                    if (File.Exists(fileToCopy))
                    {
                        File.Copy(fileToCopy, destinationDirectory);
                    }

                    fileToCopy           = templateDirectoryName + "\\ABLine.txt";
                    destinationDirectory = directoryName + "\\ABLine.txt";
                    if (File.Exists(fileToCopy))
                    {
                        File.Copy(fileToCopy, destinationDirectory);
                    }

                    fileToCopy           = templateDirectoryName + "\\RecPath.txt";
                    destinationDirectory = directoryName + "\\RecPath.txt";
                    if (File.Exists(fileToCopy))
                    {
                        File.Copy(fileToCopy, destinationDirectory);
                    }

                    //now open the newly cloned field
                    mf.FileOpenField(dirNewField + myFileName);
                }
            }

            DialogResult = DialogResult.OK;
            Close();
        }