Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8936407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:12:31+00:00 2026-06-15T10:12:31+00:00

I am new to PHP and I am writing a PHP code to get

  • 0

I am new to PHP and I am writing a PHP code to get data from a modbus device using the PHPModbus library. I need to be able to display the data every second and then write the data to a CSV file every minute.

  • I have used the header(‘Refresh:1’) to refresh the page every second.
  • I get the data as raw bytes which I convert to float or int depending on the parameter.
  • Then i store the data in an array ‘energymeter_param’ and implode the contents of the array to get a string which I want to write to the CSV file log.csv every 60 seconds.
  • If you read my code you would see that I am calling the file_put_contents() function every time the value of seconds becomes 0. [ if(date(‘s’) == 0)].
  • But sometimes while refreshing the page the time skips from HH:MM:59 to HH:MM:01 directly and so i miss the data in the log file for that minute.

How do I overcome this?

<?php

// for automatically refreshing the page every one second
header('Refresh: 1'); 
//setting the time zone and getting the date and time
$timezone = "Asia/Calcutta";
if(function_exists('date_default_timezone_set')){
   date_default_timezone_set($timezone);
}
echo date('d-m-Y'). "</br>";
echo date('H:i:s'). "</br>";
//reference to ModbusMaster.php file where the modbus php protocol is defined
require_once dirname(__FILE__) . '/phpmodbus/Phpmodbus/ModbusMaster.php';
// Create Modbus object
$modbus = new ModbusMaster("192.168.1.105", "TCP");

//Energy Meter
// FC3 = Function Code 3 to read holding registers
/*Setting device ID = 5, Starting address as 100 and 
  number of registers to be read as 120
*/
try {
    // FC 3
    $recData = $modbus->readMultipleRegisters(5, 100, 120);
}
catch (Exception $e) {
    // Print error information if any
    echo $modbus;
    echo $e;
    exit;
}


// Print status information
echo "</br>Status:</br>" . $modbus;

// Conversion
echo "<h2>EN8400</h2>\n";
// Chunk the data array to set of 4 bytes
$values = array_chunk($recData, 4);
//Create an array and set first two values as date and time
$energymeter_param = array();
$energymeter_param[0] = date('Y-m-d H:i:s');
//$energymeter_param[1] = date('H:i:s');
$count = 1;
// Get float from REAL interpretation
//echo "<h3>Energy meter</h3>\n";
//get each parmeter from energy meter and store in the array
foreach($values as $bytes){    
    /*Since load hours is unsigned long we are converting it 
    to unsigned long type and scaling to get correct value */
    if($count == 59) {
      $temp = PhpType::bytes2unsignedint($bytes);
      $temp = $temp/3932160;
    }
    else {
      $temp = PhpType::bytes2float($bytes);
      //Converting Wh to Kwh
      if($count == 31) {
        $temp = $temp/1000;
      }
    }
    //store the values in an array
    $energymeter_param[$count] = $temp;
    $count++;
}
//Store the number of energy meter parameters in a variable
$num_energymeter_param = $count;
echo "<h3>Energy meter array</h3>\n";
//print array 
print_r ($energymeter_param)." </br>";
//write the values to a csv file every time seconds becomes 00
if((date('s')) == 00) {
    $temprow = implode(',',$energymeter_param);
    $temprow.="\n";
    $file = 'H:\Appserv\www\Log.csv';
    file_put_contents($file, $temprow, FILE_APPEND );
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:12:31+00:00Added an answer on June 15, 2026 at 10:12 am

    You have two things going on (state and exact match).

    First, apache+PHP (I’m assuming apache, but same thing applies to IIS and other stateless setups) don’t run your script all the time, but only when requested. This causes the inconsistency that you are seeing, where the browser may not make the request exactly when you want it (too many factors as to why this will happen).

    This can be better handled by running a PHP daemon to handle the 1 minute logging.

    However, if you want to continue with this route, then you have to deal with inconsistent queries.

    You can use a way that covers a “greater than” rather than exact match expression. In particular you can check when your CSV file was last changed via the stat function (mtime value): http://php.net/manual/en/function.stat.php and then if it has been longer than 59 seconds append to the file.

    Browsers are known to be unreliable ways of timing, so I still recommend a daemon.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A site I developed has a new requirement to get weather data from the
Writing code in php and jQueryMobile. I am trying to get a div to
in my custom theme-settings.php (zen-subtheme) i put following code to get a new textarea
I am writing a new application in PHP which will be released to the
I am writing a new website that uses PHP and MySQL. I am trying
New to PHP and MySQL, have heard amazing things about this website from Leo
I am writing an PHP application and using the gettext module to translate to
I have tried using a example file using PHPExcel script to extract data from
I'm a little new at PHP, so my script writing is still progressing. I
I'm writing a code in PHP which deletes a file if it's more than

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.