Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read port never get response on Windows #23

Open
jaysongyn opened this issue Feb 7, 2017 · 3 comments
Open

Read port never get response on Windows #23

jaysongyn opened this issue Feb 7, 2017 · 3 comments

Comments

@jaysongyn
Copy link

$serial->deviceSet("COM2"); $serial->confBaudRate(115200); $serial->deviceOpen(); $serial->sendMessage("AT+CMGF=1\n\r"); $serial->sendMessage("AT+cmgs=\"6299999999\"\n\r"); $serial->sendMessage("test \n\r"); $serial->sendMessage(chr(26)); sleep(7); var_dump($serial->readPort());

@kamaroly
Copy link

Did you find a solution for this ?

@jaysongyn
Copy link
Author

@kamaroly They have comment on the code " // Windows port reading procedures still buggy" When I try to read I never get awnser. What I did instead using
do { if ($i > $count) { $content .= fread($this->_dHandle, ($count - $i)); } else { $content .= fread($this->_dHandle, 128); } } while (($i += 128) === strlen($content));
`

$content = fread($this->_dHandle,56);
`
I set length to 56.

This is not the best solution, because if you have response bigger than 56 length you can't read, but it solve my problem.

To test if my device is working and connect to com port I use PUTTY.

@ttk
Copy link

ttk commented Jul 10, 2019

I've done some testing under Windows and I can confidently say that using the built-in PHP file system functions such as: fread and fgets are unreliable when dealing with the COM port. In some cases, it worked when the COM port was opened and closed by another program (such as putty or a serial monitor). In most cases, it would simply not read any data (most likely it's buffering internally).

The proper way to communicate with the COM port on Windows is to use the Direct IO functions. This is an extension you need to install first, before you can use it. Unfortunately I couldn't find a serial php library that uses the Direct IO API. Instead I just used the API directly.

For more info, see the discussion on StackOverflow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants