Serial port php windows

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Multi-platform convenience class to access the serial port from PHP

License

Xowap/PHP-Serial

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

Readme.md

PHP Serial was written at a time where I did not know any other language than PHP and I started to get seriously bored with its abilities.

I somehow got hold of a « Citizen C2202-PD » point-of-sale display, and I wanted to play around with it. I also managed to get the documentation of it, and created a convenience class to access the serial port though the Linux file.

Afterwards, I posted it to PHP Classes, and this probably is what brought it any visibility.

 include 'PhpSerial.php'; // Let's start the class $serial = new PhpSerial; // First we must specify the device. This works on both linux and windows (if // your linux serial device is /dev/ttyS0 for COM1, etc) $serial->deviceSet("COM1"); // We can change the baud rate, parity, length, stop bits, flow control $serial->confBaudRate(2400); $serial->confParity("none"); $serial->confCharacterLength(8); $serial->confStopBits(1); $serial->confFlowControl("none"); // Then we need to open it $serial->deviceOpen(); // To write into $serial->sendMessage("Hello !");

Interestingly enough, this piece of code that is widely untested has created a lot if interest ever since it was created, and especially nowadays with everybody toying around with Arduinos and Raspberry Pis. I receive about 1 email every month asking for help with the code or sending patches/suggestions.

I think that it is time for me to remove the dust off this project and to give it a full visibility on modern tools, aka GitHub.

There is lots of bugs. I know there is. I just don’t know which are they.

  • Linux: the initially supported platform, the one I used. Probably the less buggy one.
  • MacOS: although I never tried it on MacOS, it is similar to Linux and some patches were submitted to me, so I guess it is OK
  • Windows: it seems to be working for some people, not working for some others. Theoretically there should be a way to get it done.

I have a few concerns regarding the behaviour of this code.

  • Inter-platform consistency. I seriously doubt that all operations go the same way across all platforms.
  • Read operations. Reading was never needed in my project, so all the tests I did on that matter were theoretic. I was also quite naive, so the API is probably not optimal. What we need is to re-think reading from scratch.
  • Configuration done by calling functions. This is so Java. It would be much better to be able to pass a configuration array once and for all. Furthermore, I suspect that the order of call matters, which is bad.
  • Auto-closing the device. There is an auto-close function that is registered at PHP shutdown. This sounds quite ridiculous, something has to be done about that.
  • Use exceptions. Currently there is an heavy use of the errors system to report errors (2007 baby), but this is seriously lame. They have to be replaced by actual exceptions.

I have about 0 time to code or test this project. However, there is clearly a need for it.

As in all open-source projects, I need people to fit this to their needs and to contribute back their code.

  • Address the concerns listed above, and find new ones.
  • Create a reproducible test environment for each OS, and prove that each feature works (basically, unit-testing).
  • Report of use cases, bugs, missing features, etc.

If you feel like doing any of those, do not hesitate to create an issue or a pull-request, I’ll gladly consider consider it 🙂

PHP Serial Copyright (C) 2007-2014 PHP Serial’s contributors (see CONTRIBUTORS file)

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

About

Multi-platform convenience class to access the serial port from PHP

Источник

Read Serial Port with PHP

Read serial port with PHP ? yea some time we may face such a requirement, reading serial devices from web browsers. bit complicated right ? Yea, there are few way to accomplish this with the technologies available now.

We all know that browser is Sanbox mode and its restricted to access system hard drives or any serial devices. So How we can read the serial port using PHP (server side script executed with a client browser). Yea that bit crazy. We can check few other options available to do the things.

The above two methods have its own advantages and limitations Active x only support with Windows platform, Java is cross platform so it can be used , there is also requirement like JDK should be installed on the client PC , the applet should be signed and verified. etc.

So if we can do that (install JDK on client PC) so we can install an apache server with PHP on client PC too right ? so we can develop it with PHP itself. no need of struggling with applet signed process.

Yep that is what I’m doing here, my requirement is to read the weigh machine out put on the web browser. so the PC connected to the serial port device. I’m working on linux machine so I can do the serial port reading with PHP itself , In windows there is some issue with reading serial port using PHP , it can’t be achieved quick , have to work a lot, In my case its fine too , bcoz client PC’s are Linux so I’m good to go with PHP.

If you’re using Windows , I will post another article soon with windows serial port reading using PHP and Perl mix.

ok lets check the code in PHP, also my plan to execute the code as follows.

I will put the code in a folder called serialport inside /var/htm/ so I can access it like localhost/serialport/index.php it will return the data , In my web page I can use an Ajax call to the above url and return the result inside text box of my web apps.

There are few things you have to keep in mind In linux the port name usually like ttyUSB0 or ttyS0

first you can connect the device with the system then test the device is working fine or not using the terminal by putting the command.

If the port name is correct it will read and display the values in the terminal. just Ctl + Z to stop that.

Now few thing while working with apache server on Linux machine , the default apache user named www-data should be in dialout group then only it can access the port. So please make sure the user already in the dialout group. else you can add that by using following command.

 sudo usermod -a -G dialout www-data 

you can list the current group of the user with following script.

Now the code its more simple than this setup!.

The serial port data will read continuously and out put it with a space between each values so what I did is find the maximum number of time repeated value, that must be the value shown in the device. and output it.

 ini_set("display_errors", "1"); error_reporting(E_ALL); header('Access-Control-Allow-Origin: *'); include "php_serial.class.php"; // Let's start the class $serial = new phpSerial(); // First we must specify the device. This works on both Linux and Windows (if // your Linux serial device is /dev/ttyS0 for COM1, etc.) $serial->deviceSet("/dev/ttyUSB0"); // Set for 9600-8-N-1 (no flow control) $serial->confBaudRate(9600); //Baud rate: 9600 $serial->confParity("none"); //Parity (this is the "N" in "8-N-1") $serial->confCharacterLength(8); //Character length (this is the "8" in "8-N-1") $serial->confStopBits(1); //Stop bits (this is the "1" in "8-N-1") $serial->confFlowControl("none"); // Then we need to open it $serial->deviceOpen(); // Read data $read = $serial->readPort(); // Print out the data $data = preg_split('/\s+/', $read); //print_r($data); // red and split the data by spaces to array $array = array_count_values($data); // count the array values $values = array_keys($array, max($array)); // count the maximum repeating value echo $values[0]; // If you want to change the configuration, the device must be closed. $serial->deviceClose(); 

you can download the entire code as zip file too, in the above code I used a class php serial port that is also available with zip file. This only works with Linux not with windows. you can’t read the data of serial port using above class in windows , windows supports only writing to serial port from PHP directly .

Read serial port with PHP is so simple in Linux machine for windows will write another article soon. just download the read serial port with php script just below. and extract it to your /var/www/html/ folder. It had an index.html file using jQuery to make the ajax call too the PHP file that read the serial port data and return to web page.

If you’re trying this code with remote machine make sure you added this script at top of the section.

 header('Access-Control-Allow-Origin: *'); 

Источник

Читайте также:  METANIT.COM
Оцените статью