Click here to Skip to main content
15,894,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
PHP
// Change Parms on Comport: Linux: Windows
$command = (PHP_OS == 'Linux') ? 'mode /dev/ttyS1: BAUD=9600 PARITY=N data=8 stop=1 XON=off TO=on' : 'mode COM1: BAUD=9600 PARITY=N data=8 stop=1 XON=off TO=on';

system($command, $retVar);    

if($retVar == '127'){
    echo("Command not found:$command<br>");
    // May want to exit here.
}

//Create the resource id:  Linux: Windows
$path = (PHP_OS == 'Linux') ? '/dev/ttyS1' : 'COM1';
$fp = fopen($path, 'r+');

if(!$fp){ 
  echo"Port not accessible";
  exit();
} else {
  echo"Port COM1 opened successfully";
}

//Read from port:
$buffer = fgets($fp);

echo "<br>Data read from buffer: $buffer";


What I have tried:

As I am not able access the COM port Value with references to Linux Server and windows server.So explain me what all setting need to made with references to Linux & windows Server.As this below code is working properly in localhost.

As I am getting the following error-
Command Not found:mode /dev/ttyS1: BAUD=9600 PARITY=N data=8 stop=1 XON=off TO=on
Port not accessible
Posted
Updated 1-Aug-16 20:46pm

1 solution

There is no mode command with Linux. The corresponding Linux command is setserial(8): get/set serial port info - Linux man page[^].

With PHP use PHP: Direct IO - Manual[^] to setup and communicate with serial ports. To set the port attributes see PHP: dio_tcsetattr - Manual[^].

General information on serial ports with Linux can be found in the Serial HOWTO[^].

[EDIT]
If you want to use fopen() with Linux, see this comment: PHP: fopen - Manual[^]
[/EDIT]
 
Share this answer
 
v2
Comments
Shivaraj patil 2-Aug-16 4:48am    
As the above code is working properly in localhost.
can we access the COM values using fopen functions?
$path = (PHP_OS == 'Linux') ? '/dev/ttyS1' : 'COM1';
$fp = fopen($path, 'r+');
$buffer = fgets($fp);

echo "<br>Data read from buffer: $buffer";

if(!$fp){
echo"Port not accessible";
exit();
} else {
echo"Port COM1 opened successfully";
}
As I am not getting permission to access the COM Port values when I host this in Linux server
Jochen Arndt 2-Aug-16 5:08am    
I have updated my answer with a link on how to use fopen() for serial ports.

But you must understand that serial ports are handled differently by Windows and Linux.

If your PHP script has not the permissions to configure the serial port you can do that somewhere else (e.g. in a startup script). If you don't have root access to the server, ask the administrator for help.
Shivaraj patil 2-Aug-16 6:22am    
can we check the root access or permission of serial COM port through PHP Script in Linux Web hosting server?
Jochen Arndt 2-Aug-16 7:26am    
Do you have some kind of shell remote login to your server (e.g. using SSH). If yes, use that and use su / sudo to become root (password required).

When logged in, execute "ls -l /dev/ttyS*" (can be also done as normal user). That will show you the owner and group of the serial devices. In most cases it will show owner "root" and group "dialout" where the group has read and write access.

The simplest solution to get access to the serial ports is adding the user that is executing your PHP script to the shown group (dialout).

Execute as root:

usermod -a -G dialout user_name_excuting_the_script

If you don't have shell access, you can execute the above commands from your PHP script using the system() function. But those that require to be executed by root will of course fail.

If your server has some kind of web interface for administration it may contain options for user management. Then you can try to add the user executing the script to the dialout group there.

If you don't know what I'm talking about ask the administrator of the server for help explaining him that your script needs access to serial ports.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900