PHP implements methods for connecting devices, communicating, and sending commands, and PHP commands
This article describes how PHP implements connection devices, communication, and sending commands. Share to everyone for your reference. Specific as follows:
The development of the BS architecture of the Software (PHP), need to communicate with the device, in this record, welcome you to correct:
1. Using the TCP/IP connection device with PHP socket technology
Parameter $service_port connection port
Parameter $address send IP address
Parameter $in Send command
function Send_socket_connect ($service _port, $address, $in) {//Create TCP/IP Socket$socket = socket_create (Af_inet, Sock_ STREAM, sol_tcp) or die ("could not create socket!"); Set time-out $timeout = 2; $time = time (); Set the non-blocking mode @socket_set_nonblock ($socket); Time-out judgment while (! @socket_connect ($socket, $address, $service _port)) { $err = Socket_last_error ($socket); The connection succeeds, jumping out of the loop if ($err = = = 10056) {break ; } Connection failed, determine timeout, stop if (time ()-$time) >= $timeout) { socket_close ($socket); Print (' Network exception ', please check the network connection!! '); Exit (); } Refresh rate (250 ms) Usleep (250000); } Set the blocking mode @socket_set_block ($socket); Send command to Device Socket_write ($socket, $in, strlen ($in)); Close connection socket_close ($socket);}
Send_socker_xdcoder $buffer as the return value
function Send_socket_xdcoder ($service _port, $address, $in) { //create TCP/IP socket $socket = socket_create (af_ INET, Sock_stream, sol_tcp) or die ("could not create socket!"); Set time-out $timeout = 2; $time = time (); Set the non-blocking mode @socket_set_nonblock ($socket); Time-out judgment while (! @socket_connect ($socket, $address, $service _port)) { $err = Socket_last_error ($socket); Successful connection if ($err = = = 10056) {break ; } Connection failed, determine timeout, stop if (time ()-$time) >= $timeout) { socket_close ($socket); echo ""; Exit (); } Refresh rate (250 ms) Usleep (250000); } Set the blocking mode @socket_set_block ($socket); Send command to Device Socket_write ($socket, $in, strlen ($in)); The receive device command returns data $buffer = Socket_read ($socket, 1024x768, php_normal_read); Close connection socket_close ($socket); Output return value returns $buffer;}
2. Using the PHP socket technology to connect the device with the UDP protocol for communication and sending commands
Parameter $service_port connection port
Parameter $address send IP address
Parameter $in Send command
function send_socket_connect_udp ($service _port, $address, $in) { //Use the UDP protocol with the PHP socket technology to connect the device $socket = socket _create (Af_inet, Sock_dgram, sol_udp); @socket_set_option ($socket, Sol_socket, So_rcvtimeo, Array ("SEC" = 2, "usec" and "= 0)"); Send command @socket_sendto ($socket, $in, strlen ($in), 0, $address, $service _port); @socket_recvfrom ($socket, $buffer, 1024x768, Msg_waitall, $address, $service _port); Close connection if (empty ($buffer)) { echo ""; }}
SEND_SOCKET_XDCODER_UDP $buffer as the return value
function send_socket_xdcoder_udp ($service _port, $address, $in) { //Use the UDP protocol with the PHP socket technology to connect the device $socket = socket _create (Af_inet, Sock_dgram, sol_udp); @socket_set_option ($socket, Sol_socket, So_rcvtimeo, Array ("SEC" = 2, "usec" and "= 0)"); Send command @socket_sendto ($socket, $in, strlen ($in), 0, $address, $service _port); @socket_recvfrom ($socket, $buffer, 1024x768, Msg_waitall, $address, $service _port); Close connection socket_close ($socket); if (!empty ($buffer)) { return $buffer; } else { echo ""; }}
3. Use RS232 serial port technology to connect the device, realize communication and send command
Parameter $com string number
Parameter $baudratr baud rate
Parameter $in Send command
function Rs232_connect ($com, $baudrate, $in) { @set_time_limit; Use ' windows to set channeling parameters '; EXEC ("mode"). $com. "Baud=". $baudrate. "Parity=n data=8 stop=1 Odsr=off"); "Open port serial $com"; $f = @fopen ($com, ' w+ '); Determine if the serial port is open correctly if (! $f) { //echo ""; Die ("Error when Open $com"); } Send data to the serial port; Fwrite ($f, $in); Close Port fclose ($f); End of serial port operation}
There is also a command 16 binary conversion:
function Hextostr ($hex) { $string = ""; for ($i = 0; $i < strlen ($hex) 1; $i +=3) { $string. =CHR (Hexdec ($hex [$i]. $hex [$i + 1]); } return $string;}
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/1060913.html www.bkjia.com true http://www.bkjia.com/PHPjc/1060913.html techarticle PHP implements the way to connect devices, communicate, and send commands, and the PHP command provides an example of how PHP implements connection devices, communicates, and sends commands. Share to everyone for your reference. Specific ...