1

I'm making some changes for Open Hardware Monitor. I will add the network adapter download and upload speed. But when I calculate the download speed I get a wrong calculation.

I can't use a timer to calculate the correct download speed because of the auto update in OHM. In the source here you can see how I calculate the download speed (in Mb/s).

In the construct of the class i do:

IPv4InterfaceStatistics interfaceStats = netInterfaces.GetIPv4Statistics();

bytesSent = interfaceStats.BytesSent;
bytesReceived = interfaceStats.BytesReceived;

stopWatch = new Stopwatch();
stopWatch.Start();

When the update method is called (in some random times) I do this:

IPv4InterfaceStatistics interfaceStats = netInterfaces.GetIPv4Statistics();

stopWatch.Stop();
long time = stopWatch.ElapsedMilliseconds;

if (time != 0)
{
    long bytes = interfaceStats.BytesSent;
    long bytesCalc = ((bytes - bytesSent)*8);

    usedDownloadSpeed.Value = ((bytesCalc / time) * 1000)/1024;
    bytesSent = bytes;                 
}

Hope someone can see my issue?

enter image description here

Added screenshot

JimmyD
  • 2,089
  • 3
  • 23
  • 47
  • 4
    How do you mean wrong? When you divide two integral types `(bytesCalc/time)` you get a truncated integral result which may be the issue. – clcto Mar 17 '14 at 17:25
  • 1
    Not sure if this is related, but a KB is 1024 bytes, and a MB is 1024 KBs. I see a 1000 and a 1000000 in your calculations. – Jacob Mar 17 '14 at 17:27
  • I have changed my calculation in double bytesCalc = (((double)bytes - (double)bytesSent)*8); usedDownloadSpeed.Value = (float)((double)(bytesCalc / time) * 1000); But it doesn't work. I has the same value than with the old calculation. – JimmyD Mar 17 '14 at 18:21
  • Please, do not include information about a language used in a question title unless it wouldn't make sense without it. Tags serve this purpose. – Ondrej Janacek Mar 18 '14 at 16:45

2 Answers2

1

There where a few conversion issues with my previous code. Now I have this source and it works. Tnx all for answering.

    interfaceStats = netInterfaces.GetIPv4Statistics();

        //Calculate download speed
        downloadSpeed.Value = Convert.ToInt32(interfaceStats.BytesReceived - bytesPreviousReceived) / 1024F;

        bytesPreviousReceived = interfaceStats.BytesReceived;
JimmyD
  • 2,089
  • 3
  • 23
  • 47
0

The following changes should help...

speed = netInterfaces.Speed / 1048576L;

If I recall correctly, the Speed property is a long and when you divide it by an int, you end up with a truncated result. Which brings us to a similar set of changes in your other calculation...

usedDownloadSpeed.Value = ((bytesCalc / time) * 1000L)/1024L;

... assuming that usedDownloadSpeed.Value is also a long to make sure you're not getting any truncated values with implicit conversion of your results or calculations. If you want to be doubly sure you have the casting correctly, use Convert.ToInt64().

gfish3000
  • 1,517
  • 1
  • 11
  • 17