/// <summary> /// Create a connection to the ADCP serial port with /// the given options. If no options are given, return null. /// </summary> /// <param name="options">Options to connect to the serial port.</param> /// <returns>Adcp Serial Port based off the options</returns> public AdcpSerialPort ConnectSerial(SerialOptions options) { // If there is a connection, disconnect if (_serialPort != null) { DisconnectSerial(); } if (options != null) { // Set the connection //Status.Status = eAdcpStatus.Connected; // Create the connection and connect _serialPort = new AdcpSerialPort(options); _serialPort.Connect(); // Publish that the ADCP serial port is new //PublishAdcpSerialConnection(); DisplayStatus(string.Format("Connect Serial: {0}", _serialPort.ToString())); // Set flag //IsAdcpFound = true; return(_serialPort); } return(null); }
/// <summary> /// Create a connection to the ADCP serial port with /// the given options. If no options are given, return null. /// </summary> /// <param name="options">Options to connect to the serial port.</param> /// <returns>Adcp Serial Port based off the options</returns> public AdcpSerialPort ConnectAdcpSerial(SerialOptions options) { // If there is a connection, disconnect if (_adcpSerialPort != null) { DisconnectAdcpSerial(); } if (options != null) { // Set the connection //Status.Status = eAdcpStatus.Connected; // Create the connection and connect _adcpSerialPort = new AdcpSerialPort(options); _adcpSerialPort.Connect(); // Subscribe to receive ADCP data _adcpSerialPort.ReceiveAdcpSerialDataEvent += new AdcpSerialPort.ReceiveAdcpSerialDataEventHandler(ReceiveAdcpSerialData); // Publish that the ADCP serial port is new //PublishAdcpSerialConnection(); DispalyStatus(string.Format("ADCP Connect: {0}", _adcpSerialPort.ToString())); // Set flag //IsAdcpFound = true; return(_adcpSerialPort); } return(null); }
/// <summary> /// Add the entry to the maintence log. This will /// create a list of entries and write it to the log. /// </summary> /// <param name="adcp">Adcp to write the entry to.</param> /// <param name="entry">Entry to write.</param> public void AddEntry(AdcpSerialPort adcp, MaintenceEntry entry) { // Create a list var maintList = new List <MaintenceEntry>(); maintList.Add(entry); // Append the entry WriteLog(adcp, maintList, true); }
/// <summary> /// Disconnect the serial port. /// </summary> private void Disconnect() { if (_serialPort != null) { _serialPort.ReceiveRawSerialDataEvent -= _serialPort_ReceiveRawSerialDataEvent; _serialPort.Disconnect(); _serialPort = null; } // Set status message DisplaySerialPortStatusMsg(string.Format("Disconnect from ADCP on {0} - {1}", _serialOption.Port, _serialOption.BaudRate)); }
/// <summary> /// Connect the serial port. /// </summary> private void Connect() { // If still connected, disconnect if (_serialPort != null) { Disconnect(); } // Connect and begin receiving data _serialPort = new AdcpSerialPort(_serialOption); _serialPort.Connect(); _serialPort.ReceiveRawSerialDataEvent += _serialPort_ReceiveRawSerialDataEvent; if (_serialPort.IsAvailable()) { DisplaySerialPortStatusMsg(string.Format("Connected to ADCP on {0} - {1}", _serialOption.Port, _serialOption.BaudRate)); } }
/// <summary> /// Write the list of maintence Entries to the /// Maintence log. This will convert the list /// to a JSON object. It will then create a temp /// file and write the JSON data /// to the file. It will then upload the data to the /// ADCP. /// /// Append will allow the maintence file to be complete /// overwritten. If Append = false, only the data in list /// will be written to the maintence log and anything /// previously in the log will be lost. /// /// </summary> /// <param name="adcp">Connection to the ADCP.</param> /// <param name="list">List to write to the ADCP.</param> /// <param name="append">TRUE = Add to end of file. False = Overwrite file.</param> public void WriteLog(AdcpSerialPort adcp, List <MaintenceEntry> list, bool append = true) { // Create a list var maintList = new List <MaintenceEntry>(); // If we are appending, add the data already in the // log // If we are not appending, the entire log file will // be overwritten with the new list if (append) { // Get the current log and append to it // Get the log file string maintLog = DownloadLog(System.IO.Path.GetTempPath(), adcp); // Decode the log file of all its content // and add it to the list List <MaintenceEntry> decodedLog = DecodeLog(maintLog); if (decodedLog != null) { maintList.AddRange(decodedLog); } } // Add the new list to the one from the file maintList.AddRange(list); // Generate a temp file // Then write the list a JSON object to the file string fileName = System.IO.Path.GetTempPath() + @"\" + MAINT_FILE_NAME; using (FileStream fs = File.Open(fileName, FileMode.OpenOrCreate)) using (StreamWriter sw = new StreamWriter(fs)) using (JsonWriter jw = new JsonTextWriter(sw)) { jw.Formatting = Formatting.Indented; JsonSerializer serializer = new JsonSerializer(); serializer.Serialize(jw, maintList); } // Upload the log to the ADCP adcp.XModemUpload(fileName); }
/// <summary> /// Get a list of all the entries from the /// maintence log. This will connect to the ADCP. /// Download the file. Read whats in the file /// and store it to the list. /// /// If the serial port conneciton given is not /// connected, it will not attempt to get the file. /// If the serial port is in any mode but ADCP, it will /// not attempt to get the file. /// /// If you want to download the file and store it to a specific /// location, set the dir. If you do not want to store the file, /// leave it null. /// </summary> /// <param name="adcp">Adcp serial port.</param> /// <param name="dir">Directory to store the log. If NULL is given, the file will be stored to the temp path.</param> /// <returns>A list of all the MaitenceEntry in the file.</returns> public List <MaintenceEntry> GetMaintenceLog(AdcpSerialPort adcp, string dir = null) { var list = new List <MaintenceEntry>(); // If no directory was given, write the file // to a temp location if (dir == null) { dir = System.IO.Path.GetTempPath(); } if (adcp.Mode == AdcpSerialPort.AdcpSerialModes.ADCP && adcp.IsAvailable()) { // Get the log file string maintLog = DownloadLog(dir, adcp); // Decode the log file of all its content list = DecodeLog(maintLog); // The list will be null if the file is empty // Then create an empty list so NULL will not be returned if (list == null) { list = new List <MaintenceEntry>(); } // If the list was empty, then the log downloaded // was empty or bad. Delete the file if (list.Count == 0) { try { File.Delete(dir + @"\" + MAINT_FILE_NAME); } catch (Exception e) { log.Error(string.Format("Error deleting maintence file {0}", dir + @"\" + MAINT_FILE_NAME), e); } } } return(list); }
/// <summary> /// Clear the maintence log of all information. /// This will only clear the log of user information /// but will still maintain the factory information. /// </summary> /// <param name="adcp">Adcp serial port connection.</param> public void ClearLog(AdcpSerialPort adcp) { // Get the current log and store to temp file string maintLog = DownloadLog(System.IO.Path.GetTempPath(), adcp); // Decode the log file of all its content var maintList = DecodeLog(maintLog); for (int x = 0; x < maintList.Count; x++) { if (maintList[x].ID != MaintenceEntry.EntryId.FactoryCompassCal) { maintList.Remove(maintList[x]); } } // Write back the new list to the ADCP // Setting false will overwrite the file WriteLog(adcp, maintList, false); }
/// <summary> /// Execute the upload process. This should be called /// from the async command. /// </summary> /// <param name="fileName">File name to upload.</param> private void ExecuteUpdateFirmware(object fileName) { // Convert the object to a string array var files = fileName as string[]; if (files != null) { // Stop the ADCP pinging if its pinging AdcpSerialPort.StopPinging(); // Upload all the selected files foreach (var file in files) { // Upload the file to the ADCP AdcpSerialPort.XModemUpload(file); // Wait for the update to complete Thread.Sleep(AdcpSerialPort.WAIT_STATE * 2); // Load the firmware to NAND if (file.ToLower().Contains("rtisys")) { AdcpSerialPort.SendDataWaitReply("FMCOPYS"); } // Load the boot code to NAND if (file.ToLower().Contains("boot")) { AdcpSerialPort.SendDataWaitReply("FMCOPYB"); } } // Reboot the ADCP to use the new firmware AdcpSerialPort.Reboot(); // Validate the files uploaded // By downloading it and compairing it against // the original file } }
/// <summary> /// Initialize the display. /// </summary> public void Init() { // Init the serial options _serialOption = new SerialOptions(); _serialPort = null; _AdcpCommandHistory = new ObservableCollection <string>(); StatusMsg = ""; RecordingSize = ""; _IsRecording = false; NotifyOfPropertyChange(() => IsRecording); _rawFileSize = 0; _rawAdcpRecordDir = ""; _rawAdcpRecordFile = null; RawAdcpRecordFileName = ""; _rawAdcpRecordFileLock = new object(); _IsConnected = false; SelectedBaudRate = 115200; SelectedDataBit = 8; SelectedParity = System.IO.Ports.Parity.None; SelectedStopBit = System.IO.Ports.StopBits.One; CommPortList = SerialOptions.PortOptions; BaudRateList = SerialOptions.BaudRateOptions; DataBitList = SerialOptions.DataBitsOptions; ParityList = SerialOptions.ParityOptions; StopBitList = SerialOptions.StopBitsOptions; NotifyOfPropertyChange(() => CommPortList); NotifyOfPropertyChange(() => BaudRateList); NotifyOfPropertyChange(() => DataBitList); NotifyOfPropertyChange(() => ParityList); NotifyOfPropertyChange(() => StopBitList); NotifyOfPropertyChange(() => IsConnected); }
/// <summary> /// Cancel uploading the file to the ADCP. /// </summary> private void OnCancelUpdateFirmware() { AdcpSerialPort.CancelUpload(); }
/// <summary> /// Download the log file and return /// the Results of the file. /// /// It is assumed the connection is already made to the ADCP /// serial port. /// </summary> /// <param name="dir">Director to store the log file.</param> /// <param name="adcp">ADCP serial connection.</param> /// <returns>Content of the file.</returns> private string DownloadLog(string dir, AdcpSerialPort adcp) { // Result string result = ""; // Directory to download the file _isDownloadComplete = false; bool resultDL = adcp.XModemDownload(dir, MAINT_FILE_NAME, false, true); adcp.DownloadCompleteEvent += delegate(string fileName, bool goodDownload) { // Set the flag when download is complete _isDownloadComplete = true; }; // Wait for the download to be completed int timeout = 100; while (!_isDownloadComplete) { // Sleep to wait Thread.Sleep(AdcpSerialPort.WAIT_STATE * 2); // Give a timeout so it does not get stuck timeout--; if (timeout < 0) { break; } } // Check if the download could be completed if (resultDL) { string filePath = dir + @"\" + MAINT_FILE_NAME; // If the file exist read in all the data if (File.Exists(filePath)) { try { result = File.ReadAllText(filePath); } catch (Exception e) { log.Error(string.Format("Error reading maintence file {0}", filePath), e); } // Remove the empty data in the file // The file has a complete buffer written to it // The buffer will contain empty data. This // will find the end of the JSON array. // Add 1 to include the end of the array character result = result.Substring(0, result.IndexOf(']') + 1); try { // Write the cleaned up data to the file File.WriteAllText(filePath, result); } catch (Exception e) { log.Error(string.Format("Error writing maintence file {0}", filePath), e); } } } // Wait for the state to change System.Threading.Thread.Sleep(SerialConnection.WAIT_STATE); return(result); }