开发者

Can I control hardware via PHP Language?

I wondered if I can use PHP to control an external hardware connected with the parallel port or USB port ? any ide开发者_运维百科as or resources ?


This is a useful class for serial attatched devices on Linux: PHP Serial

Serial ports are often used to communicate with peripheral devices, such as: modems, POS terminals, special printers, etc..

This class can be used to communicate and configure peripherals connected to a serial port under Linux, simplifying the development of applications that need access serial devices.


You might want to look into php exec. PHP doesn't allow Direct access to Hardware through the API, you need to call sub-programs to do that.


If you can write a program in C++ that communicates with that device, you can create a PHP extension: http://www.devarticles.com/c/a/Cplusplus/Developing-Custom-PHP-Extensions-Part-1/


You have to write a program that you can call via system calls. PHP code can't access your hardware directly.


As per my comment on Filip Ekberg's answer....maybe.

It depends on your OS and the level of access required - should it be bi-directional? Are you trying to use the control lines for purposes other than flow control? Do you know the details of the communications protocol?

C.


If you intend to run this on a *NIX system, you may want to look at Plan 9 From User Space. With these libraries, you could write a C program that make your hardware appear as a part of your file system, i.e. you mount it as you would mount a USB drive, for example. Once mounted, you can use PHP's function for manipulating files to control your hardware.

If you don't know what Plan 9 is, this may not be very clear. Let me illustrate by an dummy example : your hardware is an electronic board displaying the number of logged in user via a 7 segment display. You use Plan 9 from user space to write a program that mounts 2 files :

/somewhere/input

/somewhere/output

Writing "42" (as you would do with a text file) to input will make your board display 42. Reading output will for example tell you for how long this number did not change.

This may not be the easiest way to achieve your goal, as learning to use the Plan 9 libraries is not very easy (although people on IRC are very nice and helpful), but it is in my opinion the most elegant way.

With this, you would also be able to control your hardware from any other language, for all languages I know can manipulate files.


Well, it was an LPT port and a binary, both for Lin and Win and small PHP code to call this binary:

http://www.epanorama.net/circuits/parallel_output.html


You might wanna look into Node js with socket io npm package.. I have built a smiliar system where I can control my hardware directly through web api interface. My linux machine is Raspberry Pi that is controlling a hardware attached to ots I/O. My Node js webserver is in raspberry pi, Node js not only listen web instruction from front end javascript but can also control hardware through npm hardware I/O packages.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜