PHP implements methods for connecting devices, communications, and sending commands _php tips

Source: Internet
Author: User
Tags usleep

This article illustrates the way PHP implements connecting devices, communications, and sending commands. Share to everyone for your reference. Specifically as follows:

The development of the BS Architecture Software (PHP), the need to communicate with the device, in this record, you are welcome to correct:

1. Using PHP socket technology to connect devices with TCP/IP

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 timeout time
  $timeout = 2;
  $time = time ();
  Set the non-blocking mode
  @socket_set_nonblock ($socket);
  Timeout judgment while
  (! @socket_connect ($socket, $address, $service _port)) {
    $err = Socket_last_error ($socket);
    Connection succeeded, jump out of Loop
    if ($err = = 10056) {break
      ;
    }
    The connection fails, the timeout is judged, and the IF (Time
    ()-$time) >= $timeout) {
      socket_close ($socket) is stopped;
      Print (' Network exception, please check the network connection!! ');
      Exit ();
    }
    Refresh frequency (250 milliseconds)
    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 to 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 timeout time
  $timeout = 2;
  $time = time ();
  Set the non-blocking mode
  @socket_set_nonblock ($socket);
  Timeout judgment while
  (! @socket_connect ($socket, $address, $service _port)) {
    $err = Socket_last_error ($socket);
    Connection Success
    if ($err = = 10056) {break
      ;
    }
    The connection fails, the timeout is judged, and the IF (Time
    ()-$time) >= $timeout) {
      socket_close ($socket) is stopped;
      echo "<script>alert (' network exception, check your network connection!! ');</script> ";
      Exit ();
    }
    Refresh frequency (250 milliseconds)
    usleep (250000);
  }
  Set the blocking mode
  @socket_set_block ($socket);
  Send command to Device
  Socket_write ($socket, $in, strlen ($in));
  Receive device command return data
  $buffer = Socket_read ($socket, 1024, php_normal_read);
  Close connection
  socket_close ($socket);
  The output return value returns
  $buffer;
}

2. Use the PHP socket technology to connect the device using the UDP protocol to achieve communication and send commands

Parameter $service_port connection port
Parameter $address send IP address
Parameter $in Send command

function send_socket_connect_udp ($service _port, $address, $in) {
  //PHP Socket technology uses the UDP protocol 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" => 0));
  Send command
  @socket_sendto ($socket, $in, strlen ($in), 0, $address, $service _port);
  @socket_recvfrom ($socket, $buffer, 1024, Msg_waitall, $address, $service _port);
  Close connection
  if (empty ($buffer)) {
    echo <script>alert (' Network exception, check network connection!! ');</script> ";
  }
}

SEND_SOCKET_XDCODER_UDP $buffer to return value

function send_socket_xdcoder_udp ($service _port, $address, $in) {
  //PHP Socket technology uses the UDP protocol 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" => 0));
  Send command
  @socket_sendto ($socket, $in, strlen ($in), 0, $address, $service _port);
  @socket_recvfrom ($socket, $buffer, 1024, Msg_waitall, $address, $service _port);
  Close connection
  socket_close ($socket);
  if (!empty ($buffer)) {return
    $buffer;
  } else {
    echo <script>alert (' network exception, check your network connection!! ');</script> ";
  }
}

3. Use RS232 serial port technology to connect equipment, 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 (a);
  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+ ');
  To determine if the serial port is normal open if
  (! $f) {
    //echo "<script>alert" (' Error when open $com!! ');</script> ";
    Die ("Error when Open $com");
  }
  Sending data to the serial port;
  Fwrite ($f, $in);
  Close Port    
  fclose ($f);
  Serial Operation End
}

There is also one about the command 16 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 will help you with your PHP program design.

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.