PHP methods for connecting devices, communicating, and sending commands

Source: Internet
Author: User
Tags usleep
This article describes how to use PHP to connect devices, communicate with each other, and send commands. It involves socket-based php device connection and data communication related skills and has some reference value, for more information, see

This article describes how to use PHP to connect devices, communicate with each other, and send commands. It involves socket-based php device connection and data communication related skills and has some reference value, for more information, see

This example describes how PHP can connect devices, communicate with each other, and send commands. Share it with you for your reference. The details are as follows:

The BS Architecture Software (PHP) needs to communicate with the device. Please record it here. You are welcome to correct it:

1. Use php socket technology to connect devices using TCP/IP

Parameter $ service_port connection Port
Parameter $ address sending IP address
Parameter $ in sending command

Function Send_socket_connect ($ service_port, $ address, $ in) {// create a TCP/IP socket $ socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP) or die ("cocould not create socket! "); // Set the timeout value $ timeout = 2; $ time = time (); // set the non-blocking mode @ socket_set_nonblock ($ socket); // determine the timeout value while (! @ Socket_connect ($ socket, $ address, $ service_port) {$ err = socket_last_error ($ socket); // The connection is successful. if ($ err = 10056) {break;} // connection failure. Determine the timeout time. Stop if (time ()-$ time) >=$ timeout) {socket_close ($ socket ); print ('network exception. Check the network connection !! '); Exit () ;}// refresh frequency (250 ms) usleep (250000) ;}// sets the blocking mode @ socket_set_block ($ socket ); // send the command to the device socket_write ($ socket, $ in, strlen ($ in); // close the connection socket_close ($ socket );}

Send_socker_xdcoder $ buffer is returned value

Function Send_socket_xdcoder ($ service_port, $ address, $ in) {// create a TCP/IP socket $ socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP) or die ("cocould not create socket! "); // Set the timeout value $ timeout = 2; $ time = time (); // set the non-blocking mode @ socket_set_nonblock ($ socket); // determine the timeout value while (! @ Socket_connect ($ socket, $ address, $ service_port) {$ err = socket_last_error ($ socket); // if ($ err = 10056) {break ;} // connection failed. Determine the timeout time. Stop if (time ()-$ time) >=$ timeout) {socket_close ($ socket ); echo "script alert ('network exception, please check the network connection !! '); Script "; exit ();} // refresh frequency (250 ms) usleep (250000);} // sets the blocking mode @ socket_set_block ($ socket ); // send the command to the device socket_write ($ socket, $ in, strlen ($ in); // receive the data returned by the device command $ buffer = socket_read ($ socket, 1024, PHP_NORMAL_READ); // close socket_close ($ socket); // return the returned value of return $ buffer ;}

2. Use php socket technology to connect devices using UDP protocol to implement communication and sending commands

Parameter $ service_port connection Port
Parameter $ address sending IP address
Parameter $ in sending command

Function Send_socket_connect_udp ($ service_port, $ address, $ in) {// Use php socket technology to connect devices using the UDP protocol $ socket = socket_create (AF_INET, SOCK_DGRAM, SOL_UDP ); @ socket_set_option ($ socket, SOL_SOCKET, SO_RCVTIMEO, array ("sec" => 2, "usec" => 0); // send command @ socket_sendto ($ socket, $ in, strlen ($ in), 0, $ address, $ service_port); @ socket_recvfrom ($ socket, $ buffer, 1024, MSG_WAITALL, $ address, $ service_port ); // close the connection if (Empty ($ buffer) {echo "script alert ('network exception. Check the network connection !! '); Script ";}}

Send_socket_xdcoder_udp $ buffer is returned value

Function Send_socket_xdcoder_udp ($ service_port, $ address, $ in) {// use the php socket technology to connect to the device using the UDP protocol $ socket = socket_create (AF_INET, SOCK_DGRAM, SOL_UDP ); @ socket_set_option ($ socket, SOL_SOCKET, SO_RCVTIMEO, array ("sec" => 2, "usec" => 0); // send command @ socket_sendto ($ socket, $ in, strlen ($ in), 0, $ address, $ service_port); @ socket_recvfrom ($ socket, $ buffer, 1024, MSG_WAITALL, $ address, $ service_port ); // close the so connection Cket_close ($ socket); if (! Empty ($ buffer) {return $ buffer;} else {echo "script alert ('network exception, please check the network connection !! '); Script ";}}

3. Use RS232 serial port technology to connect devices for communication and sending commands

Parameter $ com serial port number
Parameter $ baudrasd baud rate
Parameter $ in sending command

Function rs232_connect ($ com, $ baudrate, $ in) {@ set_time_limit (10); // use 'set the exit parameter in windows '; exec ("mode ". $ com. "BAUD = ". $ baudrate. "PARITY = n DATA = 8 STOP = 1 odsr = off"); // "open the port serial port $ com"; $ f = @ fopen ($ com, 'W + '); // determines whether the serial port is enabled normally if (! $ F) {// echo "script alert ('error when open $ com !! '); Script ""; die ("Error when open $ com");} // send data to the serial port; fwrite ($ f, $ in ); // close the port fclose ($ f); // end of the serial port operation}

There is also a hexadecimal conversion command:

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 will help you with php programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.