I have always been doubtful of my billing by my internet providers. I wanted to write some code that could help me get a rough estimate of bandwidth I was using in every session. In this article, I will show you how to create a very basic network usage meter for serial port/USB/mobile phone modems with only C# (no Win API calls, sockets or fancy stuff).
Download source code.
Refer to “ReadmeFirst.txt” for instruction on how to run.
I have always been doubtful of my billing by my internet providers. I wanted to write some code that could help me get a rough estimate of bandwidth I was using in every session. In this article, I will show you how to create a very basic network usage meter for serial port/USB/mobile phone modems with only C# (no Win API calls, sockets or fancy stuff). My program is not as sophisticated as others available on the internet, but it does work well for me. It shows usage in current session and logs it in a file. This code should work with many USB modems which actually setup a COM port for communication (example GPRS/3G/HSIA data cards or mobile phones being used as modems).
The basics
The reason, I call this code ‘dirty’ is, it not well tested and was written in hurry. The program revolves around only two concepts:
- If data usage is available, then read it from performance counters.
- If not, then assume that the network is down and wait for network to be available using the ‘System.Net.NetworkInformation.NetworkChange’ class.
My program has only been tested with HSIA modem, which gets connected to a virtual COM4 port. The program is written in such a way that it handles frequent connection/disconnection and tries to log usage in every session.
The Code
This is a winforms application. In the constructor of the Form we :
- Create two performance counters, one for measuring downloads and another for measuring uploads.
- A Timer to poll the performance counters, so that we can check if they are readable (network/port is available).
- Attach event handler to ‘NetworkAvailabilityChanged’ event of ‘NetworkChange’ class, so that we are updated when the COM4 port is available.
The code is given below:
static PerformanceCounter dataSentCounter; |
In the above code, ‘category’ is set to “RAS Port” and ‘instance’ to “COM4”. The instance needs to be set to correct port number to which your modem is connected to. To get instance name of your COM port you can do something like this:
PerformanceCounterCategory c = new PerformanceCounterCategory("RAS Port"); |
Another important value is “Interval” of the timer. It should be sufficiently high (ideally 1-3 seconds). If you keep it too low the code might now work, because it seems the ‘NetworkAvailabilityChanged’ event takes some time to fire once you are physcially disconnected from the network. (Another reason for calling this code ‘dirty’). Two important pieces are the ‘NetworkAvailabilityChanged’ event handler and code for ‘Elapsed’ event of the timer. When the timer’s ‘Elapsed’ event fires, I try to read from the performance counters. If there is an exception reading, or 0 bytes recorded in the read operation, we first log the prior readings to a file. This indicates that the COM4 port was removed or network connectivity was lost. Notice here that, I am NOT relying on ‘NetworkAvailabilityChanged’ change event to inform me of a connection drop. (Why ..I told you, this is a ‘dirty’ approach).Instead we only use the ‘NetworkAvailabilityChanged’ event to check if a new network is available. When network is available we simply start the timer again.
void networkMonitor_Elapsed(object sender, ElapsedEventArgs e) |
void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e) |
As you can see from the screen shot, I have added a notification icon to the Form. When user moves mouse pointer over this icon, it shows the usage. If the user right clicks, a popup menu comes up with “exit” item. On clicking this, our application closes. The code for this part is given below:
private void notifyIcon1_MouseMove(object sender, MouseEventArgs e) |
The code for function that performs loggin of the usage in a CSV file.
private void WriteUsageToFile() |
Finally!
Hope you found this article useful. Download and try running the code with your serial port/USB modems. Add a shortcut to your “StartUp” folder, so that the program starts automatically everytime the system boots. If the above code does not work ‘as is’ for you, try customizing it according to your needs. Next week, I will try to see if I can get this code working with wireless network and LAN. I will add my findings to this post. Please leave a comments to share your thoughts. Thank you for reading.