Using PHP to Control MODBUS-RTU Equipment

Keywords: PHP Windows github git

MODBUS-RTU equipment is one of the most common types in industrial control, ITU and intelligent projects. It mostly uses RS485/232 bus communication mode. MODBUS-RTU protocol has a long history, maturity and reliability. What we are talking about today is using PHP language to apply OHSCE framework for general control of MODBUS-RTU devices.
I. preparation
PHP 5.4 and above

OHSCE V0.1.26(FIX20170120) and above

A computer (serves as a host computer)

MODBUS Controller

Relevant supporting parts

II. Writing Control Procedures

First, make sure that your OHSCE has enabled CLOUD_API and MODBUS_CLOUD_DRIVER functions, which are enabled by default in V0.1.26 and later versions. And make sure your PC can reliably link to API.CLOUD.OHSCE.COM. OHSCE introduced CLOUD_API and CLOUD_DRIVER functions after V0.1.25_B_2 version, which can separate the upgrade frequency of components, do not need to upgrade OHSCE main program frequently, selectively upgrade CLOUD_API and install CLOUD_DRIVER to use the latest functions and drivers as long as network connectivity is guaranteed without updating the main program (provided that the required functions are the lowest to support). To the running OHSCE main version. To achieve the purpose of upgrading frequency: OHSCE < OHSCE_CLOUD_API < OHSCE_CLOUD_DRIVER. With the recommendation of normal testing, there will be plenty of protocol support and private driver support for specific devices.

To initialize your OHSCE framework environment at the beginning of your program, it's very simple to introduce OHSCE's automatic loading files.

<?php
/*
OHSCE_V0.1.26_B
 High reliability PHP communication framework.
HTTP://WWW.OHSCE.ORG
@Author: Lin Youzhe 393562235@QQ.COM
 All rights reserved by the author shall be used in accordance with the authorization agreement.
*/
ini_set('memory_limit',"64");    //Resetting the memory size available to php is 64M
set_time_limit(0);               //Program does not timeout      
ob_implicit_flush(1);            
include('loadohsce.php');        //Introducing OHSCE to load files automatically

OHSCE auto-loading files allow you to save OHSCE as a folder named ohsce and introduce loading files into the higher directory to make your program code cleaner.

    |U PRO

    |-/OHSCE/

    |-loadohsce.php

Now consult the instructions of the device to find out the address of the register/coil 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 omnipotent composite control collector, but only demonstrated the single-way control of the relay (DO1) with an address of 100000H. That's the way to connect the lights in the picture.

In the example, we use COM7 to communicate. In fact, we use RS485 bus instead of a USB bus to communicate. One address of hanging station is 01, baud rate is 9600, check no (N), data bit 8, stop 1.

Ohsce_eng_serial_creat($hscecom,"com7",1,0,9600,'n',8,1); //We created a serial port resource and stored it side by side.$hscecom In variables, the serial port is com7,Permission is1(Write and read),mode0(Default), baud rate9600,No validation, data bits8,Stop bit1

For a more detailed description of the function, see the manual: Ohsce_eng_serial_creat

[Note here that some WINDOWS friends must verify that the timeout mechanism of your serial port has been opened and set the appropriate timeout time. Otherwise, it will lead to long blocking of the function.] _________

[Under LINUX is / dev/ttyXX]

The next step is to confirm our logic. In the demonstration, we simply demonstrate two breaths, i.e., closed and closed.

Ohsce_eng_serial_open($hscecom);     //Opening and Occupying Serial Port Resources
$test=null;
ohsce_drive_cloud_modbus($hscecom,"RTU","write",'01',"00000","FF00");//Call Cloud MODBUS Driver Way I Use this Way to Close Once
sleep(1);
Ohsce_eng_serial_write($hscecom,ohsce_drive_cloud_modbus($test,"RTU","write",'01',"00000","0000")['data'],false);//Call Cloud MODBUS Driver Mode II to use this mode to open once
sleep(1);
Ohsce_eng_serial_write($hscecom,ohsce_drive_cloud_modbus($test,"RTU","write",'01',"00000","FF00")['data'],false);//Call Cloud MODBUS Driver Mode II to use this mode for one closure
sleep(1);
ohsce_drive_cloud_modbus($hscecom,"RTU","write",'01',"00000","0000");//Call Cloud MODBUS Driver Mode 1 Use this mode to open once
Ohsce_eng_serial_close($hscecom);//Don't forget to close serial resources

In this example, we demonstrate two methods of calling MODBUS cloud-driven in OHSCE, one is full-automatic, including sending, receiving feedback, checking and judging the results as a whole, and the other is one-step invocation, such as close-up without considering the success of device feedback.

MODBUS_CLOUD_DRIVER is located in the Manual/Component & Plug-in/DRIVER/ Rapid 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
 All rights reserved by the author shall be used in accordance with the authorization agreement.
*/
ini_set('memory_limit',"88M");//Resetting the memory size available to php is 64M
set_time_limit(0);
ob_implicit_flush(1);
include('loadohsce.php');
sleep(10);
echo 'OK?';
sleep(2);
Ohsce_eng_serial_creat($hscecom,"com7");
Ohsce_eng_serial_open($hscecom);
$test=null;
ohsce_drive_cloud_modbus($hscecom,"RTU","write",'01',"00000","FF00");//Calling Cloud MODBUS Driver Mode 1
sleep(1);
Ohsce_eng_serial_write($hscecom,ohsce_drive_cloud_modbus($test,"RTU","write",'01',"00000","0000")['data'],false);//Calling Cloud MODBUS Driver Mode II
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

Running test

Take WINDOWS system as an example (most of the upper 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. Run TEST.BAT

 

(Can't view the video to see the sketch)

IV. Attachment

During testing, CLOUD_API/CLOUD_DRIVER may have a lot of adjustments, so upgrade these two components as much as possible. Because our IDC resources are relatively limited, there will be relative restrictions on TOKEN API calls such as OHSCETRY/GUEST/FREE/OPEN. Large frequency users can contact us for localization services. Localization will lose the advantage of synchronizing with cloud upgrades and improving real-time correction. Every follow-up needs to be localized.

Official website: http://www.ohsce.org & http://www.ohsce.com

Developer QQ group: 374756165

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

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

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

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

Contribution-Support OHSCE: http://www.ohsce.com/index.php/company/

Posted by xconspirisist on Thu, 21 Mar 2019 08:27:53 -0700