In industrial control, IoT, intelligence and other 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're talking about today is using the PHP language to apply the OHSCE framework for general control of MODBUS-RTU devices.
First, prepare
PHP 5.4 and above
Ohsce V0.1.26 (FIX20170120) and above
A computer (acting as an upper machine)
Modbus Controller
Related Accessories
II. Preparation of control procedures
First make sure that your ohsce has turned on the Cloud_api and Modbus_cloud_driver features, which are turned on by default in the V0.1.26 and later versions. and ensure that your host computer can API.CLOUD.OHSCE.COM reliable links. Ohsce introduced the CLOUD_API and Cloud_driver features after the v0.1.25_b_2 version, which allows for the frequency of the upgrade of discrete components without frequent upgrades to the main program, optional upgrades ohsce and installation Cloud_api Driver can use the latest features and drivers without upgrading the main program as long as the network connectivity is guaranteed (provided that the required functionality is supported to the OHSCE major version in operation). To achieve the upgrade frequency: Ohsce<ohsce_cloud_api<ohsce_cloud_driver. With the normal recommendations of the test, there will be a very rich protocol support and private drive support for specific devices.
Initializing your OHSCE framework environment at the beginning of your program is as simple as introducing OHSCE's auto-loading files.
<?php/*OHSCE_V0.1.26_B高可靠性的PHP通信框架。HTTP://WWW.OHSCE.ORG@作者:林友哲 393562235@QQ.COM作者保留全部权利,请依照授权协议使用。*/ini_set(‘memory_limit‘,"64"); //重置php可以使用的内存大小为64Mset_time_limit(0); //程序不超时 ob_implicit_flush(1); include(‘loadohsce.php‘); //引入OHSCE自动加载文件
OHSCE Auto-load file allows you to save Ohsce as a folder named Ohsce and introduce loading files in the parent directory to make your program code more uncluttered.
| U PRO
|-/ohsce/
|-loadohsce.php
Now check the device's manual to find out the address of the register/coil of data that needs to be read/written in order to add the pending device to its own program logic.
In this example we have chosen a aoaidodi all-in-one composite control collector but only demonstrated the single-path control of a relay (DO1) with an address of 00000H 1. That's the way the lights are connected in the picture.
Example use COM7 to communicate, in fact, we will be a USB (through the string) into the RS485 bus to communicate, hang a station address is 01, baud rate 9600, check None (N), data bit 8, stop 1.
Ohsce_eng_serial_creat($hscecom,"com7",1,0,9600,‘n‘,8,1); //我们创建了一个串口资源并存入了$hscecom变量中,串口为com7,权限为1(写读),方式0(默认),波特率9600,无校验,数据位8,停止位1
A more detailed description of the function is described in the manual: Ohsce_eng_serial_creat
[Here is a little attention to some Windows friends please be sure to verify that your serial port timeout mechanism has been turned on and set the appropriate timeout time. Otherwise, the function will be blocked for a long time.]
[Linux is/dev/ttyxx]
The next step is to confirm our logic, demonstrating that we are simply demonstrating two breaths, that is, closing the opening and closing.
Ohsce_eng_serial_open ($hscecom);Open and occupy the serial port resource $test=Null;ohsce_drive_cloud_modbus ($hscecom,"RTU","Write",' 01 ',"00000","FF00");Call the cloud Modbus drive mode One use this method to make a closed sleep (1); Ohsce_eng_serial_write ($hscecom, Ohsce_drive_cloud_modbus ($test,"RTU","Write",' 01 ',"00000","0000") [' Data '),false); //call Cloud Modbus Drive mode two use this method to open sleep (1); Ohsce_eng_serial_ Write ($hscecom, Ohsce_drive_cloud_modbus ($test, " 00000 ", " FF00 ") [ ' data '],false); //call Cloud Modbus Drive mode two use this method to make a closed sleep (1); ohsce_drive_cloud_ Modbus ($hscecom, "write", //don't forget to close the serial port resource
In the example, we demonstrate the OHSCE in the Modbus Cloud Drive two calls, the way a full-automatic call mode with send, receive feedback, check and judge the results as a whole, the way two for a single step, for example, here is near write regardless of whether the device feedback success or not.
Modbus_cloud_driver in Manuals/components & Plugins/driver/: quick to use
Complete program code:
<?php/*ohsce_v0.1.26_b High-reliability PHP communication framework. http://www.ohsce.org@ Author: Lin Youjie 393562235@QQ. The COM author retains all rights, please use it in accordance with the license agreement. */ini_set (' Memory_limit ',"88M");Reset PHP can use a memory size of 64mset_time_limit (0); Ob_implicit_flush (1);Include' loadohsce.php '); Sleep (10);Echo' OK? '; Sleep2); Ohsce_eng_serial_creat ($hscecom,"Com7"); Ohsce_eng_serial_open ($hscecom); $test =Null;ohsce_drive_cloud_modbus ($hscecom,"RTU","Write",' 01 ',"00000","FF00");Call the cloud Modbus drive mode one sleep (1); Ohsce_eng_serial_write ($hscecom, Ohsce_drive_cloud_modbus ($test,"RTU", "write", "00000", "0000") [ ' data '],false); 1); Ohsce_eng_serial_write ($hscecom, Ohsce_drive_cloud_modbus ($ Test, "RTU", "write", ' ', "00000", "FF00") [ ' data '],false"), Sleep (1); Ohsce_drive_cloud_modbus ($hscecom, "write", ' ", " 00000 ",
Save it as test.php
Third, run the test
Take the Windows system as an example (most of the PC is Windows)
Write a BAT (test.bat)
php %cd%/test.php
Verify that the serial port is idle and power on the device, running Test.bat
(You cannot view the video to see this diagram)
Iv.. Attached
Cloud_api/cloud_driver may have more adjustments during the test, so follow the upgrade as much as possible. Because our IDC resources are currently relatively limited, the token invocation API such as Ohscetry/guest/free/open has a relative limitation. Large-frequency users can contact us for localization services, localization will be lost and cloud synchronization upgrade, improve the advantages of real-time remediation, each follow-up needs to be localized.
Official website: http://www.ohsce.org & http://www.ohsce.com
QQ Group of Developers: 374756165
Github:https://github.com/openibc/ohsce
[Email Protected]:https://git.oschina.net/sfxh/ohsce
Manual Address: http://www.ohsce.com/index.php/book/
Cooperation & Sponsorship: 393562235 ([email protected])
Donations & Support ohsce:http://www.ohsce.com/index.php/company/
Using PHP to control Modbus-rtu devices