Introduction: Save Arduino Sensor Data Without Blocking

Saving your Arduino sensor data is only part of the problem. Whilst you are saving the data you may well want to access that data as well. In many cases you will be shown an error saying the file is in use. In this Instructable we show you how to save your sensor data without blocking other applications access to the data.

As always, the quickest way to understand and implement this Instructable is to watch the very short video!

Some people have had a little problem with implementing the previous Instructables because they have not watched the videos.

Step 1: Prior Knowledge

If you have not gone through my second Instructable you should watch this video first. It shows you everything you need to know before we dive into the small section of saving the data without blocking.

In essence we create a serial connection to our Arduino, then collect the temperature sensor's data and display it on a graph. Next we save the data to a file.

Step 2: Checkbox Addition

We have added a checkbox to our graphical user interface in the bottom left. This allows you to see the difference between blocking and non-blocking saving methods.

When the program is running and the checkbox is checked you will not be able to open the file where the data is being saved.

However, if you uncheck the checkbox you will now be able to open the file and see the data. You will not be able to write to the data just read it.

So, now you can have your program running 24 hours a day and still be able to process the data in another program if you wish.

On to the Code...

Step 3: The Code

Remember to change the "COM" port in the C# code to the com port your Arduino is connected to.

private bool _saveWithBlocking = true;
private void cbBlockingSave_CheckedChanged(object sender, EventArgs e) { if (cbBlockingSave.Checked) _saveWithBlocking = true; else { _saveWithBlocking = false; } }

When we check and un-check the checkbox the above code is run. All we are doing here is setting a global boolean value to true if checked and untrue if unchecked. If it is checked we will save with blocking and if it is unchecked we will save the data in such a way that you can access it even when the data is being saved.

// File saved to desktop
private StreamWriter _sw = File.AppendText(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "ArduinoData.txt")); private readonly string _savePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "ArduinoData.txt"); private void SaveData(string data) { if (_saveWithBlocking) { if(_sw.BaseStream == null) _sw = File.AppendText(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "ArduinoData.txt")); _sw.WriteLineAsync(data + Environment.NewLine); } // may write out of turn but the TimeStamp will allow us to sort it and put it back together correctly else { _sw?.Close(); FileStream fs = null; StreamWriter sw = null; try { fs = new FileStream(_savePath, FileMode.Append, FileAccess.Write, FileShare.Read); sw = new StreamWriter(fs); sw.WriteLine(data + Environment.NewLine); sw?.Close(); fs?.Close(); } catch (Exception ex) { SetText(rtbConOut, "ERROR: " + ex.Message); } finally { sw?.Close(); fs?.Close(); } } }

In this example we have hard coded the program to save our sensor data to our Desktop, in a file called "ArduinoData.txt". The StreamWriter "_sw" blocks our access to the data. But, the StreamWriter sw does not. You will see when we create the FileStream for "sw" we set some parameters. The last being FileShare.Read. This tells the operating system that other applications can access our data file for reading... but not for writing.

We do not get the opportunity to set these parameters when creating the StreamWriter "_sw". It is this difference that decides whether or not we can have multiple programs access our data and what they can do with that data.

Step 4: In Conclusion

So, now you know how to connect to your Arduino through the serial port, collect the sensor data and write your Arduino sensor data to a file in a way that will prevent it from being accessed by other programs or, if you desire, allowing other programs to access your data file as it is being written to!

The files can be found in GitHub: https://github.com/7ASecond/ArduinoNet

Specifically look in the TempCSV folder.

For more tutorials have a look at my YouTube Channel: https://www.youtube.com/channel/UCw0RquafdM8U-0hfq...