Reply
fopen.. and keeping it that way!
Old 09-25-2007, 11:54 AM fopen.. and keeping it that way!
Zork's Avatar
Extreme Talker

Posts: 200
Hi all,

Thank you for looking at my post. I haven't much to offer this community but I'll help whenever I can. Right now I have a rather complicated problem.

Im using php open a com port, write to it twice, then close the port and navigate away. The problem is, the com device I have requires NINE seconds after it is opened to be "Ready" to be written too. So, my code looks like this:

PHP Code:
    `mode com1: BAUD=9600 PARITY=N data=8 stop=1 xon=off`;
    
$fp fopen ("COM1:""w+");
    if (!
$fp) {
        echo 
"Uh-oh. Port not opened.";
    } else {
        
sleep(9);
        
$string  "string"
        
fputs ($fp$string );
         
fclose ($fp);
    }
header'Location: http://frontdist.net/gc/getcard.html' ) ; 
So, here's what is happening.
OPEN COM1
WAIT 9 SECONDS
WRITE STRING TO COM1
CLOSE COM1

Once com1 is open, I can send commands as often as I like, but that 9 seconds is a long time when you're stuck waiting! I'd like to be able to open COM1 once, then just write to it as I need. This script is run everytime I need to write to COM1 (Its a remote power switch), so waiting 9 seconds is not an option. Eventually this script, in conjunction with a card-swipe and some database scripting will allow be to access restricted area's using a card.

Thanks so much for your help!
__________________
"Computer games don't affect kids. I mean, if Pacman affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music..."
Zork is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
     
Old 09-25-2007, 12:25 PM Re: fopen.. and keeping it that way!
Zork's Avatar
Extreme Talker

Posts: 200
Is it possible $fp = fopen("com1:","w+") in an iframe, and leave it open, then, in another frame, set $fp to the same resource id as the first $fp, then write to it as if it were opened on the same page?

That way I could open the com port once in the first iframe, then set $fp in the other frame and write to the com port whenever I needed.

Hope that makes sense!
__________________
"Computer games don't affect kids. I mean, if Pacman affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music..."
Zork is offline
Reply With Quote
View Public Profile
 
Old 09-25-2007, 06:03 PM Re: fopen.. and keeping it that way!
dansgalaxy's Avatar
Eat, Sleep, Code

Posts: 5,888
Name: Dan
Location: Swindon
No i doubt that is possible with iframes. because they are physically seperate pages just displayed as one by the browser...

honestly, im not a 100% sure about what it is, but i think i get your problem.
and i think the answer might be to have the fopen() in a file have it included in all the pages which use it.

and maybe have a if/else if port open return $fp else open port and return $fp

and i would think as long as you dont fclose.. it should stay open.

Im not sure but have a try with that idea unless someone else would like to add more

If it works Talkupation apprieciated and if it dont still apprieciated

Dan
__________________
Personal UK Webhosting
Get 25% of ANY shared package for life ~ Promo: webmaster-talk (only for members!)
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 09-25-2007, 06:46 PM Re: fopen.. and keeping it that way!
tripy's Avatar
Fetchez la vache!

Posts: 1,851
Name: Thierry
Location: In the void
Why don't you run the process like a "deamon" ?
Launch the process from a shell, and make it open a tcp port to receive commands.
PHP Code:
/*
This forces the script to keep running.
But you'd better run it from a shell, as I think a browser will timeout anyway.
Don't know if the php process stays up in that case...
*/
ini_set('max_execution_time',0);
 
define('initStr','mode com1: BAUD=9600 PARITY=N data=8 stop=1 xon=off');
$doRun=TRUE;
$listenSocket=TRUE;
$errorCnt=0;
 
do{
  
$fp fopen ("COM1:""w+");
  if (!
$fp) {
    echo 
"Uh-oh. Port not opened.";
    
$errorCnt++;
    if(
$errorCnt>=2){
      
$doRun=FALSE;
    }
    
sleep(1);
  } 
  else {
    
sleep(9);
    
fputs ($fpinitStr);
    
//We don't close the connection, as we want it to stay open
    //fclose ($fp);
 
    /*
    but we flush the stream, to be sure the fputs() is sent to the process
    and not catched in a buffer...
    */
    
fflush($fp);
 
    
/*
    And now, we listen on the local port 60000 and send anything written
    there straight to the com port of your power switch
    you can use telnet to send commands on that port (telnet server_ip 60000)
    but check that the port is not closed by a firewall
    */
    
$sockfsockopen('127.0.0.1'60000$errno$errstr5);
    do{
      
$command=trim(fgets($sock));
 
      
/*
      In this case, I imagine maybe creating a series of shortcut, to avoid typing a long string into telnet
      */
      
switch($command){
        case 
'quit':
          
$listenSocket=FALSE;  
          
$command=NULL;
          break;
        case 
'reset':
          
//Issue a reset to the switch...
          
$command="reset yourself!";
          break;
      }
 
      if(
$command!==NULL){
        
$ret=fputs($fp$command);
        
fflush($sock);
      }
    }
    while(
$listenSocket===TRUE);
  }
}while(
$doRun===true); 
I'm writing this out of memory, it could need to be worked on, but this would be a good start I believe.
That, and you are resourceful
__________________
Listen to the ducky: "This is awesome!!!"


Last edited by tripy : 09-26-2007 at 09:18 AM.
tripy is offline
Reply With Quote
View Public Profile
 
Old 09-26-2007, 08:46 AM Re: fopen.. and keeping it that way!
dansgalaxy's Avatar
Eat, Sleep, Code

Posts: 5,888
Name: Dan
Location: Swindon
Sorry, Im not quite needing to do this sorry i couldnt help more.
__________________
Personal UK Webhosting
Get 25% of ANY shared package for life ~ Promo: webmaster-talk (only for members!)
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 09-26-2007, 09:35 AM Re: fopen.. and keeping it that way!
Ultra Talker

Posts: 478
tripy's right: for PHP, at least, you would need to set up a separate process/daemon and communicate with the port through that daemon.
__________________
Free PHP Obfuscator
TwistMyArm is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to fopen.. and keeping it that way!
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.14157 seconds with 13 queries