php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #66943 Can't open COM10 or higher serial ports
Submitted: 2014-03-22 22:19 UTC Modified: 2020-09-07 12:52 UTC
Votes:4
Avg. Score:3.2 ± 0.4
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: mattficken@php.net Assigned:
Status: Open Package: Streams related
PHP Version: Irrelevant OS: Windows
Private report: No CVE-ID: None
 [2014-03-22 22:19 UTC] mattficken@php.net
Description:
------------
PHP on Windows can't open (using fopen()) serial ports greater than COM9:, fe fopen("COM10:" or fopen("COM11:" fail.

This occurs regardless of the mode passed to fopen. "r+b" is the correct mode for serial on Windows. "r+" allows reading and writing. "b" sets it to binary mode, important because: Windows converts non-ASCII (>127) bytes if the stream is in text mode.

I am using Serial over USB (FTDI driver) to communicate with a microcontroller (Arduino).

In Serial over USB (not real serial) on Windows you can rename COM ports, so users can work around this problem by renaming COM ports to unused ports like COM7 or COM8, etc... for instructions, see my PR below for the PhpSerial class.


I have modified the PhpSerial class to work on Windows (for both read and write), see https://github.com/Xowap/PHP-Serial/pull/3  as an example of how to use serial ports from PHP.


I suspect the cause is in the realpath() parser, checking for COM*: with a one integer but not two (not COM**: )


Test script:
---------------
// connect Serial-over-USB device to Windows PC, such as an Arduino Uno
//         (which uses an FTDI controller, get the FTDI driver for Windows
//          from Sourceforge or Arduino)
fopen("COM10:", "r+b");

Expected result:
----------------
fopen() returns a resource handle

Actual result:
--------------
"failed to open stream: invalid argument"

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-04-23 22:33 UTC] mattficken@php.net
Instead of using the COMn: notation, using the \\.\COMn notation avoids this issue.

I have a PR for PhpSerial which uses this so it can open 2 digit Com ports (COM 11, COM12, etc...), see https://github.com/Xowap/PHP-Serial/pull/6


Thanks to Dennis Ahrens for figuring this out.
 [2020-09-07 12:52 UTC] cmb@php.net
-Type: Bug +Type: Documentation Problem
 [2020-09-07 12:52 UTC] cmb@php.net
This is a documented limitation of CreateFile[1]:

| To specify a COM port number greater than 9, use the following
| syntax: "\.\COM10".

Thus changing to doc problem.

[1] <https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea#communications-resources>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Oct 06 12:01:27 2024 UTC