Application demo of PHP in RS232 serial communication Protocol [test environment winxp/php5.1.4]

Source: Internet
Author: User
Tags format array chr ini sleep socket sprintf win32
php5| Application Demo

I. Prior knowledge:

What is rs232/rs484 and its application?

QUOTE:
RS232 interface is a serial port, computer chassis rear of the 9-core (or 25-core) socket, next to the general has "| o| o| "Sample identification.
The general chassis has two, the new chassis may have only one. Laptops may not be available.
There are many industrial instruments that use it as a standard communication port. The content and format of the communication are usually attached to the user's Manual of the instrument.

Data transmission between computer and computer or computer and terminal can be used in two ways, such as serial communication and parallel communication. Because the serial communication way has the use line to be few, the cost is low, especially in the long-distance transmission, avoids the many line characteristic inconsistent and is widely used. In serial communication, both sides of communication are required to adopt a standard interface, so that different devices can be easily connected to communicate. The Rs-232-c interface (also called EIA rs-232-c) is one of the most commonly used serial communication interfaces at present. It was established in 1970 by the United States Electronics Association (EIA) Joint Bell System, modem manufacturers and computer terminal manufacturers to develop a standard for serial communications. Its full name is "the technical standard for serial binary data interchange interfaces between data terminal equipment (DTE) and Data communication devices (DCE)" which provides for the adoption of a 25-foot DB25 connector, the signal content of each pin of the connector is stipulated, and the level of each signal is also stipulated.

(1) The signal content of the interface is in fact many of the 25 leads in the rs-232-c are rarely used, in the computer and terminal communication generally use only 3-9 leads. The signal contents of the most commonly used 9-wire rs-232-c are shown in Schedule 1

(2) The electrical characteristics of the interface are negatively related to the voltage of any signal line in the rs-232-c. namely: Logic "1", -5--15v logic "0" +5-+15v. The noise tolerance is 2V. That is, the receiver can recognize the low to +3v signal as the logic "0", high to -3v signal as the logic "1"
(3) The physical structure of the interface Rs-232-c interface connectors generally use the model for the DB-25 25-pin plug, usually plug at the DCE end, the socket at the DTE end. Some devices connected to the PC Rs-232-c interface, because do not use each other's transmission control signal, only three interface lines, that is, "Send data", "Receive data" and "Signal land." Therefore, the use of DB-9 9-pin plug, transmission line using shielded twisted pair.

(4) The transmission cable length is stipulated by the RS-232C standard in the case that the code element distortion is less than 4%, transmission cable length should be 50 feet, in fact this 4% of the code element distortion is very conservative, in the actual application, about 99% of the users are in accordance with the scope of the code element distortion 10-20% work, So the maximum distance in the actual use would be far more than 50 feet, and the US Dec company had provided for the test results of schedule 2 by allowing the code to become 10%. 1th Cable for shielding cable, model for the decp.no.9107723 has three pairs of twisted-pair, each pair of 22# AWG composed of, and covered with shielding net. The 2nd cable is a cable with no shielding. The model for DECP.NO.9105856-04 is a 22#AWG four-conductor cable.

1.rs-232-c is a serial physical interface standard developed by the American Electronics Association EIA (Electronic Industry Association). RS is the English "recommended standard" abbreviation, 232 is the identification number, C indicates the number of changes. Rs-232-c bus standard with 25 signal lines, including a main channel and an auxiliary channel, in most cases mainly using the main channel, for general duplex communication, only a few signal lines can be achieved, such as a transmission line, a receiving line and a ground. The RS-232-C standard provides a data transfer rate of 50, 75, 100, 150, 300, 600, 1200, 2400, 4800, 9600, 19200 baud per second. RS-232-C Standard stipulates that the drive allows 2500pF capacitance load, communication distance will be limited by this capacitor, for example, the use of 150pf/m communication cable, the maximum communication distance of 15m; if the capacitance of the cable per meter is reduced, the communication distance can be increased. The other reason for the short transmission distance is that RS-232 is a single terminal signal transmission, there are common ground noise and can not suppress common mode interference and so on, so it is generally used for communication within 20m.

2.rs-485 bus, when the requirement communication distance of dozens of meters to thousands of meters, widely used RS-485 serial bus standards. RS-485 has the ability to suppress common-mode interference by using balanced transmission and differential reception. Coupled with the high sensitivity of the bus transceiver, can detect low to 200mV voltage, so the transmission signal can be recovered outside the kilometer. RS-485 using Half-duplex mode, at any time only a bit in the sending State, therefore, the transmission circuit must be controlled by the enabling signal. RS-485 is convenient for multiple interconnect and can save many signal lines. Application RS-485 can be networked to form distributed systems that allow up to 32 drives and 32 receivers.

In the past, the communication between PC and intelligent device relies on RS232, RS485, Ethernet, etc., mainly depends on the interface specification of the equipment. However, RS232, RS485 can only represent the physical media layer and link layer of communication, if you want to achieve two-way access to data, you must write their own communications applications, but most of these procedures can not meet the Iso/osi specifications, can only achieve a single function, applicable to a single device type, the program does not have universality. In the RS232 or RS485 equipment network, if the number of equipment is more than 2 units, you must use RS485 to do communication media, RS485 network equipment to exchange information only through the "Master (Master)" Equipment relay can be realized, this main device is usually a PC, Only one main device is allowed in this device network, and all the rest are from (Slave) devices. The Fieldbus technology is based on the Iso/osi model and has a complete software support system, which can solve the problems of bus control, conflict detection and link maintenance.

Source URL: http://zhidao.baidu.com/question/4578886.html
From serial two machines to the cable guide

QUOTE:
(i) The production of serial communication cable

Whether it is a 9-hole plug, or a 25-hole plug, its serial communication cable should be connected to the following docking relations:

SG←→SG txd←→rxd Rxd←→txd rts←→cts
Cts←→rts DTR←→DSR DSR←→DTR

Based on the above docking relationship, the serial communication cable can be connected very conveniently. By the way, here are some of the implications of each of these pins:

SG English is all called signal Ground/common return, indicating signal ground;
TXD refers to the transmitted data, which indicates the sending;
Rxd refers to received data;
RTS means request to send, indicating the sending of requests;
The CTS means clear to Send, which indicates a purge request;
DTR refers to the data Terminal Ready, which indicates that the terminal is ready;
The DSR refers to the data Signal Rate Selector, which indicates that the bits are ready to be placed.

In the production of 9-core serial connection, you need 2 9-hole plugs and 1.5-meter-long, at least 7-core flat cable, pin line as shown below.

9-Hole Plug-9-hole plug pin lines are: 2-3, 3-2, 4-6, 5-5, 6-4, 7-8, 8-7.
9-Hole Plug-25-hole plug pin lines are: 2-2, 3-3, 4-6, 5-7, 6-20, 7-5, 8-4.
25-Hole Plug-25-hole plug pin lines are: 2-3, 3-2, 4-5, 5-4, 6-20, 7-7, 20-6.

Description: Here are some serial communication description pictures, please go to http://bbs.chinaunix.net/viewthread.php?tid=771726 for view


Second, the test environment construction:
1. Refer to the above "(a) Serial communication cable Production", the first two computers connected through the serial port.
Of course, you can also operate a computer above the two serial port, and do not need two PCs, if you have two serial port.
Because I used to do industrial control low-level development, so I can easily build this basic environment;
In fact, as long as: Rxd txd docking, Gnd straight, commonly known as three-wire connection.
This does not make any answer.

2. The construction of PHP operating environment:
OS: Windows XP (other Windows systems not tested)
Php5.1.4:http://cn.php.net/get/php-5.1.4-win32.zip/from/a/mirror
Pecl5.1.4:http://cn.php.net/get/pecl-5.1.4-win32.zip/from/a/mirror
Decompression PHP5.1.4, put the Php_dio.dll PECL5.1.4 in the PHP5.1.4 under the EXT directory
Paste the php.ini-dist copy of the PHP5.1.4 into the php.ini and open the settings and save:

Code:[copy to Clipboard]extension=php_dio.dll
This does not make any answer.


Third: reference materials:
1. Reference Manual Documentation:
http://cn.php.net/manual/zh/ref.dio.php
http://cn.php.net/manual/zh/ref.exec.php
If there are any functions in the code that you have not seen, or have not used, or are unfamiliar with, please visit the URL above.
The main use of PHP direct IO functions.


Four: Demo code:

Code:[copy to clipboard]<?php
// --------------------------------------------------------------------------
File name:RS232_Server.php
DESCRIPTION:RS232 Demo Host computer program
requirement:php 5.1.4 (CLI) (http://www.php.net)
//
Copyright (C), Honestqiao, 2006, all Rights Reserved.
//
Author:honestqiao (honestqiao@hotmail.com/qq:5601680)
//
Program Introduction:
This program and rs232_client constitute a complete demo system, showing the application of PHP in RS232 serial communication.
A basic but complete RS232 Communication Protocol (HQB232) is implemented in the program, and the communication protocol format is as follows:
Protocol content:
C->s 01//Request Newsletter
S->c 02//Response Newsletter
C->s LEN Data//len represents the data length, which represents the actual data
C->s 03//End Newsletter
Description: s means the upper machine C means the lower machine
HQB232 represents the underlying (base) RS232 Communication protocol that Honestqiao demonstrates, including the Protocol
Request and response, the structure of the data frame.
Demo process for communication requests and responses, 10 data frames sent, communication end
The structure of the data frame is the current ordinal number, microtime (), and the random string
//
Welcome to explore the application of PHP in RS232 serial port communication.
// --------------------------------------------------------------------------
Set_time_limit (0);
EXEC (' mode com1:baud=115200 data=8 stop=1 parity=n xon=on ');

$FD = Dio_open (' COM1: ', O_RDWR);
if (! $fd)
{
Die ("Error when Open COM1");
}

$FF = Dio_stat ($FD);p rint_r ($FF);
echo "HQB232 SERVER is listenning on com1\n";
Read
$len = 2;
$t =0;while (($t + +) <1000)
{
$data = Dio_read ($FD, $len);
if ($data) {
if ($data ==chr (0). chr (1)) {
echo "s_recv:01\n";
echo "s_send:02\n";
Dio_write ($FD, Chr (0). chr (2));
Break
}
}
}

Read
$len = 2;
$t =0;while (($t + +) <1000)
{
$len = 2;
$data = Dio_read ($FD, $len);
if ($data ==chr (0). chr (3)) {
echo "s_recv:03\n";
Break
}
ElseIf ($data) {
$len = Intval ($data);
$data = Dio_read ($FD, $len);
if ($data) {
echo "S_RECV: ($len) $data \ n";
}
}
}
Dio_close ($FD);
?>


Code:[copy to clipboard]<?php
// --------------------------------------------------------------------------
File name:RS232_Client.php
DESCRIPTION:RS232 Demo Lower Machine Program
requirement:php 5.1.4 (CLI) (http://www.php.net)
//
Copyright (C), Honestqiao, 2006, all Rights Reserved.
//
Author:honestqiao (honestqiao@hotmail.com/qq:5601680)
//
Program Introduction:
This program and Rs232_server constitute a complete demo system, showing the application of PHP in RS232 serial communication.
A basic but complete RS232 Communication Protocol (HQB232) is implemented in the program, and the communication protocol format is as follows:
Protocol content:
C->s 01//Request Newsletter
S->c 02//Response Newsletter
C->s LEN Data//len represents the data length, which represents the actual data
C->s 03//End Newsletter
Description: s means the upper machine C means the lower machine
HQB232 represents the underlying (base) RS232 Communication protocol that Honestqiao demonstrates, including the Protocol
Request and response, the structure of the data frame.
Demo process for communication requests and responses, 10 data frames sent, communication end
The structure of the data frame is the current ordinal number, microtime (), and the random string
//
Welcome to explore the application of PHP in RS232 serial port communication.
// --------------------------------------------------------------------------
Set_time_limit (0);
EXEC (' mode com2:baud=115200 data=8 stop=1 parity=n xon=on ');

$FD = Dio_open (' COM2: ', O_RDWR);
if (! $fd)
{
Die ("Error when Open COM2");
}

$FF = Dio_stat ($FD);p rint_r ($FF);
echo "HQB232 CLIENT is start on com2\n";
Dio_write ($FD, Chr (0). chr (1)); echo "c_send:01\n";
$len = 2;
$t =0;while (($t + +) <1000)
{
$data = Dio_read ($FD, $len);
if ($data ==chr (0). chr (2)) {
echo "c_recv:02\n";
Break
}
}
$len = 2;
$t =0;while (($t + +) <10)
{
$sdata = sprintf ("%03d", $t). "=" . Microtime (). " (" . Randomkeys (rand (0,35)). ")";
$slen = strlen ($sdata);
$stxlen = sprintf ("%02d", $slen);
Dio_write ($FD, "$stxlen");
Dio_write ($FD, "$sdata"); echo "C_send: ($stxlen) $sdata \ n";
Sleep (1);
}
Dio_write ($FD, Chr (0). chr (3)); echo "c_send:03\n";
Dio_close ($FD);

function Randomkeys ($length)
{
$pattern = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for ($i =0; $i < $length; $i + +)
{
$key. = $pattern {rand (0,35)};
}
return $key;
}
?>

V. Presentation process:
Host computer: Start-> run->cmd
Then go to the PHP5.1.4 directory and execute:
Php-f rs232_server.php

Next machine: Start-> run->cmd
Then go to the PHP5.1.4 directory and execute:
Php-f rs232_client.php


The actual results are as follows:

Code:[copy to Clipboard]f:\usr\local\php5>php-f e:\RS232_Server.php
Array
(
[Device] => 3
[Inode] => 0
[Mode] => 8192
[Nlink] => 1
[UID] => 0
[GID] => 0
[Device_type] => 3
[Size] => 0
[Atime] => 0
[Mtime] => 0
[CTime] => 0
)
HQB232 SERVER is listenning on COM1
S_recv:01
S_send:02
S_RECV: (001=0.19238200) 1150031774 (KRRUV981GPF1PMI1FZ4QZ09E9)
S_RECV: (a) 002=0.19144800 1150031775 (Jrhw41mphuy0ui)
S_RECV: (003=0.19148500) 1150031776 (lqq560p6v17r)
S_RECV: (004=0.19148900) 1150031777 (3CT02XMC)
S_RECV: (28) 005=0.19212700 1150031778 ()
S_RECV: (006=0.19162500) 1150031779 (2ILQF)
S_RECV: (007=0.19167800) 1150031780 (VWMDFC)
S_RECV: (008=0.19168700) 1150031781 (IDB0LAIX4A6RYXZ5NB7U74IAZA601IT6SV)
S_RECV: (009=0.19170100) 1150031782 (R9YPGTVU4J7W1U141QX6IM20AJY7)
S_RECV: (010=0.19171000) 1150031783 (wq5mp1sdfuet1tp0x3vk67n47)
s_recv:03

F:\usr\local\php5>


Code:[copy to Clipboard]f:\usr\local\php5>php-f e:\RS232_Client.php
Array
(
[Device] => 3
[Inode] => 0
[Mode] => 8192
[Nlink] => 1
[UID] => 0
[GID] => 0
[Device_type] => 3
[Size] => 0
[Atime] => 0
[Mtime] => 0
[CTime] => 0
)
HQB232 CLIENT is start on COM2
C_send:01
C_recv:02
C_send: (001=0.19238200) 1150031774 (KRRUV981GPF1PMI1FZ4QZ09E9)
C_send: (a) 002=0.19144800 1150031775 (Jrhw41mphuy0ui)
C_send: (003=0.19148500) 1150031776 (lqq560p6v17r)
C_send: (004=0.19148900) 1150031777 (3CT02XMC)
C_send: (28) 005=0.19212700 1150031778 ()
C_send: (006=0.19162500) 1150031779 (2ILQF)
C_send: (007=0.19167800) 1150031780 (VWMDFC)
C_send: (008=0.19168700) 1150031781 (IDB0LAIX4A6RYXZ5NB7U74IAZA601IT6SV)
C_send: (009=0.19170100) 1150031782 (R9YPGTVU4J7W1U141QX6IM20AJY7)
C_send: (010=0.19171000) 1150031783 (wq5mp1sdfuet1tp0x3vk67n47)
c_send:03

F:\usr\local\php5>

VI: Description
1. EXEC (' mode com1:baud=115200 data=8 stop=1 parity=n xon=on ');
EXEC (' mode com2:baud=115200 data=8 stop=1 parity=n xon=on ');

Mode is the command under CMD to set the parameters of the serial port, because Dio_tcsetattr () is not implemented under Windows, so you need to use the mode command of CMD to set up, for specific usage, see: Help mode

2. The communication protocol is conducted using ASCII text and does not use the Hex 16 system. Because it is a demo program, the protocol is very simple, but the basic content of a protocol has.

3. The sleep of the Subordinate Machine program (1), this is the common situation in the communication, the data transmission and the host machine processing need certain time.

4. Demo for RS232, and so on, you can also use other occasions. For example: simple modification, applied to HTTP communication.

5. The establishment of hardware environment, the establishment of PHP environment, do not do any troubleshooting.

6. Welcome to explore.

7. Thanks Mikespook for the additional, if it is *nix, then:
'/dev/ttys0 ' represents the first COM port, and so on, and the port's settings should be used after Dio_open:
Dio_tcsetattr ($FD, Array (
' Baud ' => 9600,
' Bits ' => 8,
' Stop ' => 1,
' Parity ' => 0
));



Related Article

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.