Page 1 of 1

Fsocket char/string

Posted: Fri Sep 17, 2010 3:58 pm
by mrcastrovinci
All,
I am having trouble with an fsocket connection. I can use the code below without a hitch to grab the actual data. Were the problem lies is......I can only seem to read data from my device using FgetC. So it returns each character one by one because the other methods hang. I am ok with using this method but i would need to some how store each character returned and then make (combine) into one big string so I can manupulate later on. Now I know there are the functions of Socket_recv, which seems like what would be ideal to use. My device does not send a LF command but rather a carriage return only. Can someone post an example of socket_receive that accepts data until a carriage return is sent. I am not opening a file, this is data streaming from a device (ip-rs485). If this is not possible then can someone post an example of how to put the characters all in one string using the code below. Thanks so much!

$current_line = fgetc($fp);
while (!feof($fp)) {
// process current line
$current_line = fgetc($fp);
if($current_line === "\r") break;
echo $current_line;
}
fclose($fp);

Re: Fsocket char/string

Posted: Fri Sep 17, 2010 4:34 pm
by VladSun

Code: Select all


$line = '';
while (!feof($fp)) 
{
    $current_line = fgetc($fp);
    if($current_line === "\r") break;
    $line .= $current_line;
}
fclose($fp);

echo $line;

Re: Fsocket char/string

Posted: Fri Sep 17, 2010 8:35 pm
by requinix
What if you try using fgets after

Code: Select all

ini_set("auto_detect_line_endings", true);
stream_set_blocking($fp, 0);

Re: Fsocket char/string

Posted: Sat Sep 18, 2010 10:26 am
by mrcastrovinci
VladSun wrote:

Code: Select all


$line = '';
while (!feof($fp)) 
{
    $current_line = fgetc($fp);
    if($current_line === "\r") break;
    $line .= $current_line;
}
fclose($fp);

echo $line;

THIS WORKS PERFECTLY!!!! Thank you so much!

Re: Fsocket char/string

Posted: Sat Sep 18, 2010 10:48 am
by mrcastrovinci
tasairis wrote:What if you try using fgets after

Code: Select all

ini_set("auto_detect_line_endings", true);
stream_set_blocking($fp, 0);
tasairis wrote:ini_set("auto_detect_line_endings", true);
stream_set_blocking($fp, 0);

Thanks for the reply. I tried this and it just terminates the socket right away no data received. Weird.....Im not sure why. Any ideas?