LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-24-2010, 05:08 PM   #1
wkellen
LQ Newbie
 
Registered: Aug 2010
Posts: 5

Rep: Reputation: 0
PHP Serial port


Hello all,

I had a web server running from a Windows 7 computer and recently moved everything to an Ubuntu server. All is set up and running smoothly. I do have one page that actually interfaces with the serial port. The only thing I do with it is to set the DTR pin to "true" which applies +12v to the pin. This activates a relay. It is very basic and simple. After moving everything to Linux, this page will no longer work. I knew this would happen. I know the page had code in it to call out "Com1" which would change to "ttyS0". It also had "set_serDTR(True);" in the code. I can't figure out how to get PHP to control the DTR pin on a Linux serial port. Can anyone point me in the right direction?

Thanks,

Wayne
 
Old 08-24-2010, 06:18 PM   #2
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 14,246

Rep: Reputation: 2475Reputation: 2475Reputation: 2475Reputation: 2475Reputation: 2475Reputation: 2475Reputation: 2475Reputation: 2475Reputation: 2475Reputation: 2475Reputation: 2475
Quote:
Originally Posted by wkellen View Post
Hello all,
I had a web server running from a Windows 7 computer and recently moved everything to an Ubuntu server. All is set up and running smoothly. I do have one page that actually interfaces with the serial port. The only thing I do with it is to set the DTR pin to "true" which applies +12v to the pin. This activates a relay. It is very basic and simple. After moving everything to Linux, this page will no longer work. I knew this would happen. I know the page had code in it to call out "Com1" which would change to "ttyS0". It also had "set_serDTR(True);" in the code. I can't figure out how to get PHP to control the DTR pin on a Linux serial port. Can anyone point me in the right direction?
Not without seeing the code, no. However, it's not just ttyS0...it's /dev/ttyS0. Have you tried that?
 
Old 08-24-2010, 06:28 PM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 11,824

Rep: Reputation: 727Reputation: 727Reputation: 727Reputation: 727Reputation: 727Reputation: 727Reputation: 727
Just searching for php serial port class came up with this link. Which should be similar to what you used with windows.
http://www.phpclasses.org/package/36...rial-port.html
 
Old 08-24-2010, 06:42 PM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
You will probably have to adjust the permission level on /dev/ttyS0, as your web server probably does not run as root. To actually control the pins, you might find setSerialSignal useful (from PHP, use 'system()' or whatever PHP provides to launch other programs).
--- rod.
 
Old 08-25-2010, 09:55 AM   #5
wkellen
LQ Newbie
 
Registered: Aug 2010
Posts: 5

Original Poster
Rep: Reputation: 0
On Windows 7 I had to add the php_ser++.dll file to my PHP folder. The code I put in my web page was:

<?php

$module = 'win_serial';

if (extension_loaded($module))
{

$str = "Module loaded";
}

else

{

$str = "Module $module is not compiled into PHP";

die("Module $module is not compiled into PHP");

}

ser_open( "COM1", 115200, 8, "None", "1", "None" );

ser_setDTR( True );

?>

I haven't actually tried the page, because I knew I need to make changes, I just didn't know exactly what I needed to do. I will look into your suggestions.

Thanks for all the help.
 
Old 08-25-2010, 03:01 PM   #6
wkellen
LQ Newbie
 
Registered: Aug 2010
Posts: 5

Original Poster
Rep: Reputation: 0
Ok, I think I have a game plan for when I get to work on it again.

First: I need to allow everyone permissions on /dev/ttyS0

Second: I need to compile setSerialSignal.c

Third: I need to add the php code to my web page.

<?php

exec("setSerialSignal /dev/ttyS0 1 0");

?>

I am guessing that 0 0 sets the DTR pin one way and 1 0 sets it the other. (+ and -)

This is too easy. I have to be missing something. Does this look like it will work?

Thanks for pointing me to the setSerialSignal theNbomr.
 
Old 08-25-2010, 03:13 PM   #7
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Just by scanning the embedded strings for usage messages, I think the arguments to setSerialSignal are the states of DTR and RTS, respectively. 1 == ON, 0 = OFF.

As far as I can tell, the rest of your plan looks solid. BTW, I tried building setSerialSignal, and to get a clean build, I had to add
Code:
#include <stdio.h>
#include <stdlib.h>
--- rod.
 
Old 08-25-2010, 08:53 PM   #8
wkellen
LQ Newbie
 
Registered: Aug 2010
Posts: 5

Original Poster
Rep: Reputation: 0
Success!

I followed the plan and all is well. Well, almost.

I shut my server down and moved it to it's permanent location. When I restarted it, it no longer worked. After chasing wires for a while, I remembered the permissions on the serial port. I logged into it and did a "sudo chmod 777 /dev/ttyS0" and it was up and running again. Now, how do I make the serial port permissions permanent?

Thanks to all for your help. Especially you, Rod.
 
Old 08-25-2010, 09:42 PM   #9
HasC
Member
 
Registered: Oct 2009
Location: South America - Paraguay
Distribution: Debian 5 - Slackware 13.1 - Arch - Some others linuxes/*BSDs through KVM and Xen
Posts: 329

Rep: Reputation: 55
Quote:
Originally Posted by wkellen View Post
Now, how do I make the serial port permissions permanent?
edit udev rules, maybe?
 
Old 08-25-2010, 10:10 PM   #10
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Setting the appropriate udev rules would be the definitive solution. A cheesy alternative would be adding a 'chmod ' line to /etc/rc.local
--- rod.
 
Old 09-01-2010, 09:22 AM   #11
wkellen
LQ Newbie
 
Registered: Aug 2010
Posts: 5

Original Poster
Rep: Reputation: 0
Thank you all. I will look into the "udev rules" to solve my permissions problem. Other than that everything works as it is supposed to work.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
linux serial port to router console port connection? frankie_fix Linux - General 3 02-26-2007 09:32 PM
Parallel Port & Serial Port device identification helpmeforlinux Linux - Hardware 3 01-02-2007 01:15 AM
timedia 4 port serial port i/o card configuration binu_edl Linux - Hardware 0 03-09-2006 09:39 AM
IBM AIX 5.2/5.3 serial port/tty0 port vinod_balani AIX 2 02-19-2006 03:38 PM
Using serial port card(PCMCIA) with IPAQ running Linux, can't find ttyS0 port d2army Linux - Laptop and Netbook 0 11-12-2005 08:07 PM


All times are GMT -5. The time now is 06:10 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration