Download the PHP package zeran/async-serial-port without Composer
On this page you can find all versions of the php package zeran/async-serial-port. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package async-serial-port
Zeran SerialPort
An asynchronous PHP library for serial port communication built on ReactPHP.
Introduction
Zeran SerialPort is a modern PHP library that provides asynchronous serial port communication using ReactPHP's event loop. It supports various baud rates, parity settings, and platform-specific configurations for both Linux and Windows systems.
This library is ideal for applications that need to communicate with hardware devices through serial ports, such as:
- IoT devices
- Microcontrollers (Arduino, ESP32, etc.)
- Industrial equipment
- Legacy hardware with serial interfaces
- Embedded systems
Requirements
- PHP 8.1 or higher (uses PHP enums)
- ReactPHP event loop
- PSR-3 compatible logger (optional)
- Appropriate permissions to access serial port devices
Installation
You can install the package via composer:
Basic Usage
Here's a simple example of how to use the library:
Configuration
The SerialPortConfiguration
class allows you to configure various aspects of the serial port:
Required Parameters
device
: Path to the serial port device (e.g.,/dev/ttyUSB0
on Linux orCOM1
on Windows)platform
: Operating system platform (usually you can usePHP_OS
)baud
: Baud rate (defaults to 115200)
Optional Parameters
parity
: Parity mode (PARITY_NONE
,PARITY_EVEN
,PARITY_ODD
,PARITY_MARK
, orPARITY_SPACE
)stopBits
: Number of stop bits (STOP_BITS_1
orSTOP_BITS_2
)dataBits
: Number of data bits (DATA_BITS_8
orDATA_BITS_9
)xon
: Enable/disable software flow controlocts
: Enable/disable hardware flow controlrts
: Enable/disable RTS (Request to Send) signaldtr
: Enable/disable DTR (Data Terminal Ready) signalchunkSize
: Size of read chunks in bytes
Supported Baud Rates
The library supports the following baud rates through the BaudRateEnum
:
- 9600
- 14400
- 19200
- 38400
- 56000
- 57600
- 115200
- 128000
- 230400
- 256000
Example:
Events
The library provides event-based communication through a callback function. The following events are available:
EventEnum::DATA
: Received data from the serial portEventEnum::ERROR
: An error occurredEventEnum::OPEN
: Port has been openedEventEnum::CLOSE
: Port has been closedEventEnum::WRITE
: Data has been written to the port
API Reference
AsyncSerialPort
Main class for serial port communication.
Methods
__construct(SerialPortConfiguration $configuration, LoopInterface $loop, callable $callback = null)
: Create a new serial port instanceopen()
: Open the serial portclose()
: Close the serial portwrite(string $message)
: Write data to the serial portisOpened()
: Check if the serial port is open
SerialPortConfiguration
Class for configuring the serial port parameters.
Properties
device
: Device pathplatform
: Operating system platformbaud
: Baud rate (from BaudRateEnum)parity
: Parity mode (from ParityEnum)stopBits
: Number of stop bits (from BitsEnum)dataBits
: Number of data bits (from BitsEnum)xon
: Software flow controlocts
: Hardware flow controlrts
: RTS signaldtr
: DTR signalchunkSize
: Read chunk size
Advanced Usage
Using with PSR-3 Logger
The library supports PSR-3 logging through the LoggerAwareTrait
:
Platform-Specific Considerations
Linux
On Linux, you may need to add your user to the dialout
group to access serial ports without root privileges:
You'll need to log out and log back in for the changes to take effect.
Windows
On Windows, you need to use COM port names (e.g., COM1
, COM2
, etc.) for the device path.
Troubleshooting
Common Issues
- Permission denied: Ensure your user has permission to access the serial port.
- Device not found: Verify the device path is correct. Use
ls /dev/tty*
on Linux or Device Manager on Windows. - Invalid configuration: Check that your baud rate, parity, and other settings match your device requirements.
Debugging
Enable debugging by setting a logger:
License
The MIT License (MIT). Please see License File for more information.