0

Possible Duplicate:
PHP preg_match to get a word after space, which is where it a word KG

I'm developing web application with PHP,My apps is communicate with scale digital, i'm using help with phpserial class. My problem is data is output in my browser mistmatch with output in scale digital. When in scale digital is 16KG, so in browser output is NT,ST+16KG and then i refresh again the browser get other, like ~!NT,ST+16KG, 16KG. My question, how to get data in browser just 16KG.

This is script to read data:

<?php
$serial = new phpSerial();
$serial->deviceSet("/dev/ttyUSB1");
$serial->confBaudRate(9600); //Baud rate: 9600 
$serial->confParity("none"); //Parity (this is the "N" in "8-N-1") 
$serial->confCharacterLength(8); //Character length (this is the "8" in "8-N-1") 
$serial->confStopBits(1); //Stop bits (this is the "1" in "8-N-1") 
$serial->confFlowControl("none"); 

$serial->deviceOpen();

$serial->sendMessage(chr(1));
$tes = $serial->readPort();
print_r($tes);
// get result ~!NT,ST+16KG, 16KG or NT,ST+16KG
$serial->deviceClose();
?>

And this is code function readPort from class phpSerial.

function readPort ($count = 0)  {
        if ($this->_dState !== SERIAL_DEVICE_OPENED)
        {
            trigger_error("Device must be opened to read it", E_USER_WARNING);
            return false;
        }

            $content = ""; $i = 0;

            if ($count !== 0)
            {
                do {
                    if ($i > $count) $content .= fread($this->_dHandle, ($count - $i));
                    else $content .= fread($this->_dHandle, 128);
                } while (($i += 128) === strlen($content));
            }
            else
            {
                do {
                    $content .= fread($this->_dHandle, 128);
                } while (($i += 128) === strlen($content));
            }

            return $content;
    }

I'm developing use linux, port scale digital detect in /dev/ttyUSB1, i mean this problem is ASCII, so any help me with my problem. Thanks.

Community
  • 1
  • 1
Loren Ramly
  • 1,091
  • 4
  • 13
  • 21
  • Band-aid fix: `preg_replace("/(\d+KG)\z/", "$1", $str);` – alex Dec 24 '12 at 00:00
  • Will it always be in KG? – kittycat Dec 24 '12 at 00:01
  • Hi alex thanks for reply, if using parsing the data as it can not, because the result fickle if i refresh. example data result is NT,ST+16KG, and then i refresh browser again and then get result NT,ST+16KG-6K, and then i refresh again ~NT,ST+16KG~, and then i refresh again example NT,ST+16KG+16KG+16KG, and so forth, but there is between 16KG. – Loren Ramly Dec 24 '12 at 00:06
  • Hi cryptic, thanks for reply. yes, there is always between KG, is there a moment in your regex issue. or better yet directly alter the function readPort. – Loren Ramly Dec 24 '12 at 00:10
  • I love google translate .. – dbf Dec 24 '12 at 00:51

0 Answers0