<?php

function microtime_float()
{
    list($usec, $sec) = explode(" ", microtime());

    return ((float) $usec + (float) $sec);
}

if(isset($_POST['the_input'])){$the_input  = $_POST['the_input'];} else {$the_input  = '';}


include 'PhpSerial.php';

// Let's start the class
$serial = new PhpSerial;

// First we must specify the device. This works on both linux and windows (if
// your linux serial device is /dev/ttyS0 for COM1, etc)
 $serial->deviceSet("COM1");
//$serial->deviceSet("/dev/cu.usbserial-FTDY7ID6");

// We can change the baud rate, parity, length, stop bits, flow control
$serial->confBaudRate(2400);
$serial->confParity("none");
$serial->confCharacterLength(7);
$serial->confStopBits(1);
$serial->confFlowControl("none");

// Then we need to open it
$serial->deviceOpen();

// To write into
$serial->sendMessage("I".$the_input);

// Or to read from
$read = '';
$theResult = '';
$start = microtime_float();

while ( ($read == '') && (microtime_float() <= $start + 0.5) ) {
    $read = $serial->readPort();
    if ($read != '') {
        $theResult .= $read;
        $read = '';
    }
}

// If you want to change the configuration, the device must be closed
$serial->deviceClose();

// etc...

$theResult1=  explode('kg', $theResult);
$theResult2= preg_split("/[^0-9.]/", $theResult1[0]);
//$theResult1=   preg_replace("/[^0-9.]/", "", $theResult1);
/*$result2=substr($theResult1[1],0,-2);
$lengthstr= strlen($result2);
$offset=(int)($lengthstr/2);*/
if(preg_match('/ (.*?) kg/', $theResult, $weight1)==1){
echo (int)substr($weight1[1],-5);
}


?>""