NI Home > Community > NI Discussion Forums

LabVIEW

Showing results for 
Search instead for 
Do you mean 
Reply
Member
PedroTeixeira1985
Posts: 13
0 Kudos

Labview connect over sockets with php

Hello, i´ve a simply led.

 

I would control value of led over sockets using PHP programming language. My question is, where are documentation and examples? I found many examples between 2 or more Labview applications but not in PHP. I don´t found documentation that explins information to send to Labview application from PHP sockets...

 

It´s possible? 

 

Thanks!

Trusted Enthusiast
Mark_Yedinak
Posts: 3,055
0 Kudos

Re: Labview connect over sockets with php

There is no native supprt in LabVIEW for the PHP protocol. You would have to use the native TCP VIs and implement the PHP protocol yourself. PHP uses HTTP as the underlying protocol and LabVIEWS HTTP implementation is primarily for use with the LabVIEW application acting as a web service. It is not really designed to drive an external web page. The HTTP VIs are part of the Internet Toolkit. I suggest you also take a look at those since they may help some.


Mark Yedinak

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot

Member
PedroTeixeira1985
Posts: 13
0 Kudos

Re: Labview connect over sockets with php

[ Edited ]

Yes i know this. I would implement led management over sockets TCP because my version of Labview (8.5) don´t support WebServices, but i don´t found documentation for this.

 

You said that i have to implement protocol, i will investigate this.

 

Thanks.

 

 

Message Edited by PedroTeixeira1985 on 12-15-2009 03:39 AM
Trusted Enthusiast
Mark_Yedinak
Posts: 3,055
0 Kudos

Re: Labview connect over sockets with php

The Internet Toolkit has had limited HTTP support since version 7.1 or possibly even earlier. I would take a look at that since it may help. Otherwise you would have to implement not only the PHP protocol but some subset of the HTTP protocol as well.


Mark Yedinak

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot

Member
PedroTeixeira1985
Posts: 13
0 Kudos

Re: Labview connect over sockets with php

Hello, in this moment i did an example that read value of string labview from PHP.

 

Now, i need send data from php tolabview but i don´t could makethis... I´ve this schema on labview:

 

labview.png

 

In PHP i´ve this:

 

        $socket = socket_create(AF_INET, SOCK_STREAM, 0);
        socket_bind($socket, "127.0.0.1");
        socket_connect($socket, "127.0.0.1", 4005);
       
        $msg = "Test Message";
       
        $sock_data = socket_write($socket, $msg, strlen($msg));
   
        $res = "Data sended with response $sock_data.";

 

        echo $res;

 

 

Any idea?

 

Thanks.

Member
PedroTeixeira1985
Posts: 13

Re: Labview connect over sockets with php

Hello, it work! I changed the number of bytes that TCP READ read. When string is equals "a", it set led to true:

 

lb_circuit.png

 

PHP Code:

         $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
        socket_bind($socket, "127.0.0.1");
        socket_connect($socket, "127.0.0.1", 4005);
        
        if(strcmp($data,"")==0){
            $msg = "Default msg";
        } else {
            $msg = $data;
        }
        $size = strlen($msg);
        
        $sock_data = socket_write($socket, $msg, strlen($msg));
    
        $res = "Data sended ($size) with response $sock_data.";

 

 

But now, a get this error when TCP close:

 

lb_error.png

 

But i don´t understand because i´ve defined my IP in VI Server:Machine Access .

 

Any idea?

Member
Islero
Posts: 14
0 Kudos

Re: Labview connect over sockets with php

Has you found a solution to solve this error? I am very interested in your VI because I am doing something similar

 

Thanks

Member
PedroTeixeira1985
Posts: 13
0 Kudos

Re: Labview connect over sockets with php

Hello Islero, you need to do a VI as image of my last post and you can use the PHP code.

 

Just change lenght of the String received on VI, for example if you send a string with "a" change value 16 to 1.

Member
Islero
Posts: 14
0 Kudos

Re: Labview connect over sockets with php

Great!! it works. Thanks
Member
Gaulinmp
Posts: 13
0 Kudos

Re: Labview connect over sockets with php

Hey Pedro,

 

I'm using Apache, PHP 5.3 and LabVIEW 2009. I'm doing mostly the same thing you are and have it working, but then again you do too. Error 66 means the client closed the connection, which is what you want. So in your LabVIEW code, you can watch for error 66 and filter it out because it means everything worked well (the PHP script called socket_close or exited).

 

As a sidebar, does anyone have any good architectural ideas for a communication scheme between a webserver and LabVIEW? Right now I'm using session IDs and a socket communication protocol that goes something like this:

 

 

  1. PHP opens socket and sends session ID and command
  2. LabVIEW reads command and replies with either data or executes some functionality and returns the results
  3. PHP reads back reply and closes socket
  4. LabVIEW should end up with error 66 signifying PHP script called socket_close($socket);
But that would require lots of sockets being opened and closed, and I don't know how it will handle simultaneous connections gracefully. If anyone has an idea for better architecture, I'd love some suggestions. 

 

Thanks,

Mac