Tag archive for: C#

How to write a C# program to communicate with an ESP32 development board via Bluetooth Serial

Coupled with Bluetooth Serial and GPIO pins, ESP32 can augment a Windows machine with the ability to read from sensors.

So with an ESP32 development board, you can turn your old Windows machine into an IOT gateway that can sense its operating environment.

Given these points, let us look at how we can write a C# program to communicate with an ESP32 development board via Bluetooth Serial.

How to use C# to read sensor data from Arduino or ESPx via serial connection

When you have an old Windows machine, you can convert it into an IOT gateway.

So how can we read sensor data with our windows machine? Since our windows machine probably do not have GPIO pins, we can read sensor data via an intermediary device.

For example, we can first attach an Arduino Uno or ESP32 development board as the intermediary device to our windows machine via USB. Given that connection, the intermediary device will read the sensor data and send those values via serial. On our Windows machine, we can then run a program to read those values from serial.

So how can we read sensor data from Arduino or ESPx via serial connection on our Windows machine? Since C# is a programming language for creating windows application, we can use C# to read those data from serial.

How to raise and consume custom events in C#

When something happens asynchronously, the events mechanisms in C# is a good way for different components of our C# programs to notify one another about it.

For example, when the report generation component had the finance report ready, it can raise an event to components that had registered interest in that finance report.

This post documents how we can raise and consume custom events in C#.

How to get information about all the printers that are installed in my windows machine from a C# program via WMI

I need to create a windows service that is able to send some print jobs to one of the installed printers. As this is a windows service, it has to be able to find the printer with a preset printer name. This post details some prototyping codes that I had created to determine whether I am able to locate information of all the printers that are installed in the windows host where my windows service will run.

How to ensure that your user only runs one instance of your C# program

There are times when we want to limit our users to run an instance of our C# program at any one time. Such situations can be applicable for network monitoring tools, messaging applications, web server monitoring application and etc. In this post, I discuss how we can achieve that via the mutual exclusive (Mutex) mechanism available in C#.

How to execute codes periodically in C#

While some applications passively wait for files to be available for processing, there are many others that need to periodically execute codes to fulfill business requirements. For instance, some may constantly trigger other applications via the command line to monitor the network while some others may constantly access databases to generate graphical reports for business analysts.

C#.Net is one programming language that you can use to create applications that need to do work periodically, especially in a windows environment. In this post, I document the usage of the System.Timers.Timer class to execute codes periodically.

How to monitor a folder for new files from your C# application

Suppose that you need to write a C# application to run in an environment where there are many batch applications running and creating files as their output.

Your application is one of them and is required to process files produced by others. How are you going to know from your C# application when these files are ready?

In this post, I document the use of a .Net facility that can help you achieve that.

How to save and load objects to and from file in C#

Persistency is almost always a requirement for applications that are meant for serious usage. Perhaps we want the data that our C# program had harvested or generated to be available everytime it runs. Or for that load of data that we are unable to send to a server to be remembered, so that we can try sending at a later time.

Because most of the data that is held by a C# application at runtime is in the form of objects, it is convenient to be able to save and load objects to file directly. Such capability is dubbed object serialization, and like many other programming languages, C# has the facilities to perform object serialization for developers.