Use PHP to control MODBUS-RTU devices, phpmodbus-rtu Devices

Source: Internet
Author: User

Use PHP to control MODBUS-RTU devices, phpmodbus-rtu Devices

 

In industrial control, Iot, intelligent projects, MODBUS-RTU equipment is one of the most common types, it uses RS485/232 bus communication mode, MODBUS-RTU Protocol has a long history of mature and reliable. What we are talking about today is the use of PHP language application OHSCE framework for universal control of MODBUS-RTU equipment.
I. Preparation
PHP 5.4 and above

OHSCE V0.1.26 (FIX20170120) and above

One computer (acting as the host computer)

MODBUS Controller

Related parts

2. Write a Control Program

First, make sure that your OHSCE has enabled the CLOUD_API and MODBUS_CLOUD_DRIVER functions, which are enabled by default in V0.1.26 and later versions. Make sure that your PC can use API. CLOUD. OHSCE. COM for reliable connections. OHSCE introduced CLOUD_API and CLOUD_DRIVER functions after V0.1.25 _ B2. in this way, the OHSCE main program can be upgraded frequently without frequent upgrade, you can choose to upgrade CLOUD_API and install CLOUD_DRIVER to use the latest functions and drivers without upgrading the main program (provided that the required functions are supported to the running OHSCE at the lowest level ). main version ). To achieve the upgrade frequency: OHSCE <OHSCE_CLOUD_API <OHSCE_CLOUD_DRIVER. With the recommendation of normal testing, a wide range of protocol support and private driver support for specific devices will be released one after another.

Initialize your OHSCE framework environment at the beginning of your program. It is easy to introduce the automatic file Loading Function of OHSCE.

<? Php/* OHSCE_V0.1.26_ B high-reliability PHP communication framework. HTTP: // WWW. OHSCE. ORG @ Author: Lin youzhe 393562235@QQ.COM author reserves all rights, please use in accordance with the authorization protocol. */Ini_set ('memory _ limit ', "64"); // reset the memory size that php can use to 64Mset_time_limit (0); // the program does not Time Out ob_implicit_flush (1 ); include ('loadohsce. php '); // introduce OHSCE to automatically load files

OHSCE automatic file loading allows you to save OHSCE as a folder named ohsce and introduce the file into the parent directory to make your program code more clean and tidy.

| U PRO

|-/OHSCE/

|-Loadohsce. php

Check the device manual to find out the register/coil address of the data to be read/written, so that the device to be controlled can be added to its own program logic.

In this example, we chose an AOAIDODI all-powerful Composite Control collector. However, we only demonstrated single-path control for the relay (DO1) with a H length of 1. That is, the path to the light in the figure.

In this example, COM7 is used for communication. In fact, we convert a USB (Serial) to RS485 bus for communication. The host address is 01, the baud rate is 9600, and the verification does not (N ), data bit 8, stop 1.

Ohsce_eng_serial_creat ($ hscecom, "com7", 9600, 'n',); // we created a serial port resource and saved it to the $ hscecom variable. The serial port is com7, the permission is 1 (write and read), the method is 0 (default), the baud rate is 9600, no verification, data bit 8, stop bit 1

For more details about this function, see the manual Ohsce_eng_serial_creat.

[Note that some WINDOWS friends must check that the serial port timeout mechanism has been enabled and set a suitable timeout period. Otherwise, the function will be blocked for a long time.]

[In LINUX:/dev/ttyXX]

The next step is to confirm our logic. In the demo, we will simply demonstrate two breaths, namely, closed, open, and closed.

Ohsce_eng_serial_open ($ hscecom); // open and use serial port resources $ test = null; ohsce_drive_cloud_modbus ($ hscecom, "RTU", "write", '01', "00000 ", "FF00"); // call cloud MODBUS driver method 1 use this method to perform a closed sleep (1); Ohsce_eng_serial_write ($ hscecom, ohsce_drive_cloud_modbus ($ test, "RTU ", "write", '01', "00000", "0000") ['data'], false ); // call cloud MODBUS driver method 2 use this method to enable sleep (1) at a time; Ohsce_eng_serial_write ($ hscecom, ohsce_drive_cloud_modbus ($ test, "RTU", "write ", '01', "00000", "FF00") ['data'], false); // call cloud MODBUS driver method 2 use this method to close sleep once (1 ); ohsce_drive_cloud_modbus ($ hscecom, "RTU", "write", '01', "00000", "0000 "); // call cloud MODBUS driver method 1 use this method to enable Ohsce_eng_serial_close ($ hscecom) at a time; // do not forget to disable serial port resources

In this example, we demonstrated two methods of MODBUS cloud-driven invocation in OHSCE. One is a fully automatic invocation method that includes sending, receiving feedback, verifying, and judging the results, method 2: One-Step call. For example, the device feedback is not considered as successful or not.

MODBUS_CLOUD_DRIVER is located in/component & plug-in/DRIVER/of the manual: Quick use

Complete program code:

<? Php/* OHSCE_V0.1.26_ B high-reliability PHP communication framework. HTTP: // WWW. OHSCE. ORG @ Author: Lin youzhe 393562235@QQ.COM author reserves all rights, please use in accordance with the authorization protocol. */Ini_set ('memory _ limit ', "88 M"); // reset the memory size that php can use to 64Mset_time_limit (0); ob_implicit_flush (1 ); include ('loadohsce. php '); sleep (10); echo' OK? '; Sleep (2); Evaluate ($ hscecom, "com7"); Ohsce_eng_serial_open ($ hscecom); $ test = null; ohsce_drive_cloud_modbus ($ hscecom, "RTU ", "write", '01', "00000", "FF00"); // call the cloud MODBUS driver method 1 sleep (1); Ohsce_eng_serial_write ($ hscecom, ohsce_drive_cloud_modbus ($ test, "RTU", "write", '01', "00000", "0000") ['data'], false ); // call cloud MODBUS driver method 2 sleep (1); Ohsce_eng_serial_write ($ hscecom, ohsce_drive_cloud_modbus ($ test, "RTU", "write", '01 ', "00000 ", "FF00") ['data'], false); sleep (1); ohsce_drive_cloud_modbus ($ hscecom, "RTU", "write", '01', "00000 ", "0000"); Ohsce_eng_serial_close ($ hscecom );

Save it as test. php

Iii. Run the test

Take WINDOWS as an example (most of the host computers are WINDOWS)

Write a BAT (TEST. BAT)

php %cd%/test.php

Confirm that the serial port is idle and the device is powered on, and run TEST. BAT

 

(You can see this picture if you cannot view the video)

Iv. Appendix

During the test, CLOUD_API/CLOUD_DRIVER may have a lot of adjustments, so we try to upgrade these two components as much as possible. Because our IDC resources are currently relatively limited, there are restrictions on calling APIs using tokens such as OHSCETRY, GUEST, FREE, and OPEN. Frequently users can contact us for localization services. Localization will lose the advantage of simultaneous upgrades to the cloud and improvement of real-time correction. Each follow-up request requires localization.

Http://www.ohsce.org & http://www.ohsce.com

Developer QQ: 374756165

GITHUB: https://github.com/OpenIBC/Ohsce

GIT @ OSC: https://git.oschina.net/SFXH/Ohsce

Manual address: http://www.ohsce.com/index.php/book/

Cooperation & Sponsorship: 393562235 (393562235@qq.com)

Donate & Support OHSCE: http://www.ohsce.com/index.php/company/

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.