W&T Interfaces
for TCP/IP, Ethernet, RS-232, RS-485, USB, 20mA
Glass and plastic fiber optic, http, SNMP, OPC, I/O digital, I/O analog, ISA, PCI, ...?
      Products
Home Contact   Distributors worldwide
Languages:
DE

US

ES

IT

RU
Climate measurement
Web-Thermograph
WuTooth
Web-IO
Digital
Analog
Special
Data Server
Motherbox 2
pure.box 2
Network Memory
TCP/IP-Ethernet servers
Com-Server (serial)
USB Server
Serial interfaces
USB, RS232, RS485, 20mA
Isolators & fiber optics
USB, RS232, RS485, 20mA
PC cards
PCI Express, PCI and
ISA bus
...
Printer interfaces
Accessories
Downloads
Old
Shopping basket Your shopping basket

 
      Technical knowledge
Books, articles, glossaries...
Technical background info
Applications for Com-Server,
USB Server
, Web-IO,
Web Thermometer,
Motherbox and pure.box
 

Application:

Access to serial port using PHP



 Additional links: Product overview Application overview Print version


In the following we explain how you can access the serial port on the pure.box 2 using a simple PHP script, for example to control a weather station:

In many cases permanent communication with a PC having the controlling software is necessary when operating a serial terminal device. For example with a weather station having a serial connection in which a PC needs to be used as a data logger around the clock to record a continuous series of measurement. This often used configuration can be implemented at great cost, energy and space savings using the pure.box 2.

For example to poll and receive the current temperature once a minute requires just this small PHP script, which must be started cyclically by the pure.box 2.

<?php

$fd = dio_open('/dev/ttyS1', O_RDWR);
dio_tcsetattr($fd, array(
   'baud' => 9600,
   'bits' => 8,
   'stop' => 1,
   'parity' => 0,
   'flow_control' => 0,
   'is_canonical' => 0
));

dio_write($fd, 'temperatur?');

$input = '';
while(strlen($input) <= 10)
   $input .= dio_read($fd, 1);
echo $input;

dio_close($fd);

?>

After the script is started the following actions are performed:

  • The serial port on the pure.box 2 is opened for read and write access.
  • The serial format is set to 9600 baud, 8 data bits, 1 stop bit, no parity, no hardware handshake.
  • By setting the parameter is_canonical to 0 each character received on the serial port is sent directly to the PHP. If this parameter is set to 1, transmission only takes place after receipt of a line break character.
  • To request the current temperature the string "temperature?" is sent to the connected device over the serial interface.
  • 10 characters are received and then output.
  • Finally the serial port is closed again.

The received temperature value can be then stored for example in the MySQL or SQLite database located in the box or passed to a processing server over the network.

Load this script via FTP or SMB to the folder websites in the pure.box 2, or you can run it for test purposes from the browser: http://<IP of the pure.box 2>/websites/<filename of the script>

You configure the cyclical execution by the pure.box 2 after uploading in Web Based Management. To do this, navigate in the menu tree to My files and in Properties for the file enter a time-based execution.

The above script assumes that the reply has a fixed length. If that is not the case, the read routing can be exited after receiving a specified terminal character.

To also account for the case that no or an incomplete reply is sent, a timeout must be integrated which automatically ends the read process after this time has expired.

<?php

$TIMEOUT = 10;

$fd = dio_open('/dev/ttyS1', O_RDWR | O_NONBLOCK);
dio_tcsetattr($fd, array(
  'baud' => 9600,
  'bits' => 8,
  'stop' => 1,
  'parity' => 0,
  'flow_control' => 0,
  'is_canonical' => 0
));

dio_write($fd, 'tempertur?');

$input = '';
$time_start = time();
while($time_start + $TIMEOUT > time()) {
  $input .= dio_read($fd);
  usleep(100000);
}
echo $input;

dio_close($fd);

?>

In contrast to the first script example the port here is opened non-blocking. Whereas in the first example execution of the script in the line $input .= dio_read($fd, 1); pauses until a character has been read, in the second example the line $input .= dio_read($fd); is skipped if there is no serially received character in the input buffer. Exit from the read loop is here time-based. The duration of the read process is specified in the variable $TIMEOUT .

Guaranteed exit from the while loop allows error handling and ensures consistent performance.

 
  • Mister Wong
  • Google Bookmarks
  • Twitter
  • Facebook
  • Digg
   Imprint
We are here for you personally! Wiesemann & Theis GmbH Tel.: +49 202/2680-110 (M-F 8:00 - 5:00)
Porschestr. 12 fax: +49 202/2680-265
42279 Wuppertal Individual e-mail

© Wiesemann & Theis GmbH, subject to error and alteration: Since we can make errors, none of our statements should be used without verification. Please report any mistakes or misunderstandings so that we can be aware of them and respond appropriately as quickly as possible.