public async Task CollectData(uint dataCollectionPositionDelta, int numberOfSamples, string resultfolderPath, CancellationToken token) { PositionControllerResponse response = await this.positionController.GetPosition().ConfigureAwait(false); // we assume line sensor is positioned in the middle of the position controller and image is awlays in the same place uint startPosition = response.Position; uint currentPosition = startPosition; if (Directory.Exists(resultfolderPath)) { Directory.Delete(resultfolderPath); } Directory.CreateDirectory(resultfolderPath); while (!token.IsCancellationRequested) { double currentRealPosition = currentPosition; currentRealPosition -= startPosition; currentRealPosition *= PositionController.DISTANCE_PER_TICK_MM; if (currentRealPosition > SENSOR_LENGTH) { break; } LineSensorReading lineSensorReading = await getLineSensorData(currentRealPosition, numberOfSamples, token).ConfigureAwait(false); await saveLineSensorDataToFile(lineSensorReading, resultfolderPath).ConfigureAwait(false); currentPosition = await moveToNextPosition(dataCollectionPositionDelta).ConfigureAwait(false); } }
private async Task saveLineSensorDataToFile(LineSensorReading lineSensorReading, string resultfolderPath) { string positionString = string.Format("{0:000.00}", lineSensorReading.Position); string resultFilePath = $"{resultfolderPath}\\{positionString}.csv"; try { using (FileStream fs = new FileStream(resultFilePath, FileMode.Create, FileAccess.Write)) { using (TextWriter tw = new StreamWriter(fs)) { foreach (ushort[] lsv in lineSensorReading.SensorValues) { await tw.WriteLineAsync(ConvertLineSensorValuesToString(lsv)).ConfigureAwait(false); } } } } catch { File.Delete(resultFilePath); throw; } }