I am working on to send request to VSP200 device, my device is connected to com port8 of windows machine. I am using fopen() of PHP to open the com port, but I am getting an error
Warning: fopen(COM8:) [function.fopen]: failed to open stream
can you please tell me, what is wrong in my code,
$fp = fopen ("COM8:", "w+");
if (!$fp) {
echo 'not open';
}
else{
echo 'port is open for write<br/>';
$string .= '<STX>C30C10178C10100C103110606C103081000C10100C10101C100<ETX>';
fputs ($fp, $string );
echo $string;
fclose ($fp);
}
$fp = fopen ("COM8:", "r+");
if (!$fp) {
echo 'not open for read';
}
else{
echo '<br/> port is open for read<br/>';
$buffer = fread($fp, 128 );
echo $buffer;
fclose ($fp);
}
You should not include the trailing colon in the port name:
$fp = fopen ("COM8", "w+");
Related
i've a text file on 000webhost server with url [http://file_server.comxa.com/remote_server/temp.txt][1]
i want to read first value till delimiter,here is the code
<?php
include 'dbconfig.php';
$file = fopen ("http://file_server.comxa.com/remote_server/temp.txt", "r");
if (!$file) {
echo "<p>Unable to open remote file.\n";
exit;
}
while (!feof ($file)) {
$line = fgets ($file, 1024);
if (preg_match ("#\<title\>(.*)\</title\>#i", $line, $out)) {
$title = $out[1];
break;
}
$sql = "INSERT INTO `temprature` (`id`, `temprature`) VALUES (NULL, $line)";
$result = mysqli_query($conn,$sql) or die(mysqli_error($conn));
if($result)
{
echo "temprature inserted";
}
else
{
echo "failure";
}
}
fclose($file);
?>
but this code is throwing error
Warning: fopen(http://file_server.comxa.com/remote_server/temp.txt): failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found in C:\xampp\htdocs\demo\server_read.php on line 3
Unable to open remote file.
Let me know where i'm wrong
P.S: its working fine when i'm changing path to my localhost file
I have this piece of code:
$fp = fopen("/path/to/file", "a+");
if (!$fp) {
fwrite($fp, "somedata\n");
fclose($fp);
} else {
echo "cannot open";
}
and the result is an empty file and the message cannot open. I have SELinux disabled and the permissions seem OK, otherwise the file wouldn't be created, I'd say.
Any ideas?
Obvious mistake:
if (!$fp) {
should be
if ($fp) {
I have a page on my website that have a really long execution time, i solved the problem of the fact that it was too slow by flushing contents while writing, but now i need to load it from another website, if i had to do it from my website a include would have been enough, but since i need to load it from another website, i thought of using file_get_contents, the problem is that in this way the flushs don't work, and the page take a lot of time to load, i heard that the solution was using fsockopen and fread, so i tried to use some examples, and i get a thing like this
<?php
ob_start();
$fp = fsockopen("www.mysite.com", 80);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
fwrite($fp, "Data sent by socket");
$content = "";
while (!feof($fp)) {
$content .= fread($fp, 1024);
ob_flush();
}
fclose($fp);
echo $content;
}
?>
The problem is that if i do this it work, but when i try something like
<?php
ob_start();
$fp = fsockopen("www.mysite.com/subfolder/index.php", 80);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
fwrite($fp, "Data sent by socket");
$content = "";
while (!feof($fp)) {
$content .= fread($fp, 1024);
ob_flush();
}
fclose($fp);
echo $content;
}
?>
It says me something like:
Warning: fsockopen(): php_network_getaddresses: getaddrinfo failed: No such host is known. on line 2
What am I mistaking?
UPDATE: Solved by myself: the correct code is
<?php
function HTTP_Post($URL) {
ob_start();
$URL_Info=parse_url($URL);
$fp = fsockopen($URL_Info["host"],80);
fwrite($fp, "GET ".$URL_Info["path"]." HTTP/1.0\r\n" );
fwrite($fp, "Host: ".$URL_Info["host"]."\r\n");
fwrite($fp, "Connection: Close\r\n\r\n");
while(!feof($fp)) {
echo fgets($fp, 1024);
ob_flush();
flush();
}
fclose($fp);
}
ini_set('max_execution_time', 300);
HTTP_Post("http://www.corriere.it/cronache/");
?>
Reading COM (Serial Modem) in PHP
I'd need a COM interface (Windows,COM2) to read with PHP.
This Demo is going on.
Reading is a problem, it's running sometimes.
Is there an other way (no dio,no C++) maybe w32api_register_function() is better?
function rs232init($com,$bautrate)
{
`mode $com: BAUD=$bautrate PARITY=N data=8 stop=1 xon=off`;
}
function send($comport,$char)
{
$fp = fopen ("$comport", "w+");
if (!$fp)
{
echo "not open for read";
}
else {
fputs ($fp, $char);
fclose ($fp);
}
}
function read($comport2,$sek)
{
$buffer = "";
$fp2 = fopen ("$comport2", "r+");
if (!$fp2)
{
echo "port is open for read";
}
else
{
sleep($sek);
$buffer .= fgets($fp2, 4096);
}
return $buffer;
fclose ($fp2);
}
rs232init("com2","9600");
send("com2","3");
$a = read("com2","2");
echo $a;
The com2 device should be referenced as 'COM2:'
I should point out that there is a PHP serial class already available at http://www.phpclasses.org/package/3679-PHP-Communicate-with-a-serial-port.html.
I don't know what methods it uses internally, but perhaps it will make this a bit easier to get started.
I am getting problem while using fwrite in php. the following code works in my local computer but gives error in server.
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if(!$fp) {
echo 'Error: '.$errno.', '.$errstr;
} else {
fwrite($fp, 'kool');
}
There is no error with fsockopen. it passes and gives no error. fwrite is not being able to write. it fails and returns no error only false
This is a permissions issue with the Apache/Nobody user accessing a remote file that it doesn't have permission to modify/read/write/execute.
You should also print the error message(s) for debugging
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if(!$fp) {
echo "Error No: ".$errno."<br />\n";
echo "Error Str: ".$errstr."<br />\n";
} else {
fwrite($fp, 'kool');
}
If you're on a shared host, most likely your server does not allow outbound connections on port 80. Usually only inbound connections are allowed.