tags:

views:

428

answers:

8

hi I wondered if I can use PHP to control an external hardware connected with the parallel port or USB port ? any ideas or resources ?

A: 

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

lajuette
Not completely. There are Windows-only extensions to access printers and (indirectly) hardware through Windows's COM. Still, generally true.
Pekka
@Pekka - AFAIK these all work on virtual printer devices - not physical ports.
symcbean
@symcbean ah, all right.
Pekka
+1  A: 

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.

Filip Ekberg
There's nothing intrinsic to stop php from accessing the hardware - indeed, if permissions allow it can manipulate the mapping on the filesystem with no additional extensions/code. e.g. $of=fopen('/dev/lp0','w'); However a huge amount depends on the operating system and the device driver as to what is actually possible.
symcbean
+4  A: 

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.

Andy
A: 

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.

symcbean
+1  A: 

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/

naivists
A: 

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.

A: 

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

Col. Shrapnel
A: 

Hi! Please check this link, http://www.starlinkindia.com/classic-model.html. we have the same machine. This machines takes the employee attendance records and can keep 65000 records with itself. I have to write a PHP code that can give me access to its file where this data is stored and some other works i want like i want to connect and disconnect this according as i want.

Thanks very much.

Mirza Fariad Ahmad
If you have a question, ask a question. Don't hijack someone else's question by posting something claiming to be an *answer*.
David Dorward