Home > Backend Development > PHP Problem > How to use serial communication with PHP

How to use serial communication with PHP

醉折花枝作酒筹
Release: 2023-03-11 17:36:01
forward
4571 people have browsed it

PHP occasionally communicates directly with the serial port in some applications and needs to communicate with data on the rs232 and rs485 interfaces. Searching for many solutions on the Internet, many problems arise. Here is a summary of the methods of php and serial port communication.

How to use serial communication with PHP

There are basically two ways for php to communicate with the serial port

1. Expand dio through php.

Download the dio extension: http://www.cyberspice.org.uk/downloads/dio-0.0.4rc3.tgz

Open the dio extension in php.ini.

After dio is turned on, communication can be carried out through functions such as dio_open dio_read. But dio is only under Linux by default. It seems that pecl also has compiled php_dio.dll, but when I tested it under win, it actually couldn't be used normally. Maybe my PHP version is too high (5.3.3).

If the dio extension can be opened, there are naturally many examples on the Internet

<?php
$c = stream_context_create(array(&#39;dio&#39; =>
array(‘data_rate’ => 115200,
‘data_bits’ => 8,
‘stop_bits’ => 1,
‘parity’ => 0,
‘is_canonical’ => 1)));
if (PATH_SEPARATOR != “;”) {
$filename = “dio.serial:///dev/ttyS0″;
} else {
$filename = “dio.serial://COM1″;
}
?>
Copy after login

2.PHP_SER extension

This is a third-party extension that can be used in php5 .3.3 is used normally, and my test results are relatively stable, providing single-byte stream communication and asynchronous communication. Regarding single-byte stream communication, I specifically consulted the author and found that the data stream can be read through loop.

It should be noted that the data of ser_write() is character type, but the data of ser_read() is decimal. Therefore, it is best to convert it to hex when sending, and then convert it from decimal to hexadecimal for normal use after receiving.

There is also php_ser as a third-party extension, and the author charges a fee. The free version has no function restrictions, except that the number of bytes sent and received at a time cannot exceed 1024 bytes.

function x_get_cgqbh() {
$str = ‘at+ver?’;
$str = hexToStr($str);
ser_open( “COM1″, 9600, 8, “None”, “1″, “None” );
if (ser_isopen()) {
return “opened, post data ……”;
}else{
return false;
}
ser_write(“$str”);
sleep(1);
$str = ser_read();
ser_close();
return $str;
}
Copy after login

php_ser address: Forgot, please fill it in later

==========================

Do you want to control serial and parallel port devices such as video, audio or computers by writing PHP language? Now it can become a reality. It can be easily implemented using the open source project php-serial.

For example, you only need to call the deviceSet() method and pass the serial port name in. Then you can use simple reading and writing methods, such as:

#p_code img { border: 0px none ; }
<?
deviceSet( &#39; COM2 &#39; );
$seria --> deviceOpen();
$serial -> sendMessage( &#39; Sending a message to the port! &#39; );
$serial -> deviceClose();
?>
Copy after login

Recommended learning:php video tutorial

The above is the detailed content of How to use serial communication with PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template