Sending data to the serial port in OSX via PHP (new subject)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Sending data to the serial port in OSX via PHP (new subject)

Post by robster »

Hi all,

The new till arrived today! Very exciting! It opens on ANY data being sent to the com1 port (where it's connected).

The test machine is windows, the production machine is osx.

Is there a PHP command that I can use to access (send data) to the com port that is cross platform?
The settings are: MODE COM1: BAUD=9600 PARITY=N DATA=8 STOP=1

It's all ready to go, I just have to fill in the blanks in a function called open_till() ;)



Any advice, links or discussion appreciated.

Rob
Last edited by robster on Wed Oct 12, 2005 12:32 am, edited 2 times in total.
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

cancel that!

Code: Select all

//*****************
	//Pos OPEN THE TILL
	//*****************
	function pos_open_till()
	{
	`mode com1: BAUD=9600 PARITY=N data=8 stop=1 xon=off`;
	   $fp = fopen ("COM1:", "w+");
	   if (!$fp) {
		   echo "Uh-oh. Port not opened.";
	   } else {
		   $e = chr(27);
		   $string  = ""; //don't need to send anything, it's just a null string
		   $string .= $e;
		   fputs ($fp, $string );
		   fclose ($fp);

	   	 //echo "till open<br>";
	   }
	}
I found fopen function, very nice indeed.

I have a real life object (a till) that now opens via my php script, it's SO damn cool, you have no idea how sad that sounds, but it's excellent to see a real life object respond to my code ;)

Rob
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

i'm assuming you are writing a POS system. been there, done that, wrote the book. the app is not hard at all, but like you, the problem resided in opening the drawer. there is actually a quicker way (if i can find the code) of doing this. I'll try to find it. anyways, wether you are or are not writing a pos, you should look into printing on reciept machines if you want to seriously begin pulling your brains out. it's insane. anyways, have fun
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

mmm, seems you're right!
It opens a treat on the xp machine, ding, till opens, very fun stuff (yes, it's for a POS system).

I took it to the hair salon and installed it on the mac (running OSX 10.2 I believe) and the standard php and mysql install that comes with (or that you install) and it won't open the till.

Here's the lowdown:

The mac has no serial port.
To overcome this a usb2serial adapter was purchased, installed and is running
To test this, I connected the till and told the mac via networking that the modem was infact a null modem cable connected to com1
I tried to connect via this 'modem' and the till opened. Bingo, it works.

I then go to my code and try and open till, and it does NOT open.

I check the settings (and here's where some mac advice could come in handy), it turns out the serial port installs itself in networks under an entry called USBSERIAL.

Looking in the /DEV/ folder on the mac there is an entry now called tty.usbserial.

I've tried changing the code above so many variations of this:

Code: Select all

//*****************
    //Pos OPEN THE TILL
    //*****************
    function pos_open_till()
    {
    `mode com1: BAUD=9600 PARITY=N data=8 stop=1 xon=off`;
       $fp = fopen ("USBSERIAL:", "w+");
       if (!$fp) {
           echo "Uh-oh. Port not opened.";
       } else {
           $e = chr(27);
           $string  = ""; //don't need to send anything, it's just a null string
           $string .= $e;
           fputs ($fp, $string );
           fclose ($fp);

            //echo "till open<br>";
       }
    }
or

Code: Select all

//*****************
    //Pos OPEN THE TILL
    //*****************
    function pos_open_till()
    {
    `mode com1: BAUD=9600 PARITY=N data=8 stop=1 xon=off`;
       $fp = fopen ("SERIAL:", "w+");
       if (!$fp) {
           echo "Uh-oh. Port not opened.";
       } else {
           $e = chr(27);
           $string  = ""; //don't need to send anything, it's just a null string
           $string .= $e;
           fputs ($fp, $string );
           fclose ($fp);

            //echo "till open<br>";
       }
    }
etc etc but to no avail.


I'm really sort of, stumped. Come to an end of my ideas :( I expected a few hickups, but this is really a bit of a downer. I'll get there though, but it seems I will need the advice of someone who can point me in the right direction.

So yes please, any advice appreciated.

:)

Rob
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

I have been looking around and it seems an option may be to open the port via applescript. (hence the subject change for this thread).

First of all I know not how to do this but will investigate.
Secondly, can I call an applescript from PHP on OSX?

Thanks again,

Rob
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

unfortunately, i'm only a windows guy so i don't really know. havne't had time to find the eject script i was telling you about, but i'll find it
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

robster wrote:I have been looking around and it seems an option may be to open the port via applescript. (hence the subject change for this thread).

First of all I know not how to do this but will investigate.
Secondly, can I call an applescript from PHP on OSX?

Thanks again,

Rob
If an applescript is anything like a bash script in *nix I guess you can just use the exec() or system() commands (or use backticks like you did above). Should work ;)

I have no mac experience at all though :?
User avatar
chrys
Forum Contributor
Posts: 118
Joined: Tue Oct 04, 2005 9:41 am
Location: West Roxbury, MA (Boston)

Post by chrys »

*blink*

I never knew you could use PHP to communicate with a computer's hardware... isn't this dangerous?

Where can I read more about this?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

chrys wrote:*blink*

I never knew you could use PHP to communicate with a computer's hardware... isn't this dangerous?
Only as dangerous as the person developing it ;)
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

I was playing around with something similar today, I was using the 'talk' and 'wall' commands, which allows you to access various I/O ports.
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

could you describe that a little more?

Thanks :)


(and yes, with me doing it, it's dangerous! :twisted: )

Rob
Post Reply