Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

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: 201
Trades: 0
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
 
 
Register now for full access!
Old 09-25-2007, 12:25 PM Re: fopen.. and keeping it that way!
Zork's Avatar
Extreme Talker

Posts: 201
Trades: 0
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
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
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
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
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
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
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
__________________
Only a biker knows why a dog sticks his head out the window.

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

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
Sorry, Im not quite needing to do this sorry i couldnt help more.
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
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: 483
Trades: 0
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.
__________________

Please login or register to view this content. Registration is FREE
TwistMyArm is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to fopen.. and keeping it that way!
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB 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.47885 seconds with 12 queries